Protobuffers Are Wrong
I guess I&#x27;ll copy&#x2F;paste the comment I made last time this was posted: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=18190005" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=18190005</a><p>--------<p>Hello. I didn&#x27;t invent Protocol Buffers, but I did write version 2 and was responsible for open sourcing it. I believe I am the author of the &quot;manifesto&quot; entitled &quot;required considered harmful&quot; mentioned in the footnote. Note that I mostly haven&#x27;t touched Protobufs since I left Google in early 2013, but I have created Cap&#x27;n Proto since then, which I imagine this guy would criticize in similar ways.<p>This article appears to be written by a programming language design theorist who, unfortunately, does not understand (or, perhaps, does not value) practical software engineering. Type theory is a lot of fun to think about, but being simple and elegant from a type theory perspective does not necessarily translate to real value in real systems. Protobuf has undoubtedly, empirically proven its real value in real systems, despite its admittedly large number of warts.<p>The main thing that the author of this article does not seem to understand -- and, indeed, many PL theorists seem to miss -- is that the main challenge in real-world software engineering is not writing code but changing code once it is written and deployed. In general, type systems can be both helpful and harmful when it comes to changing code -- type systems are invaluable for detecting problems introduced by a change, but an overly-rigid type system can be a hindrance if it means common types of changes are difficult to make.<p>This is especially true when it comes to protocols, because in a distributed system, you cannot update both sides of a protocol simultaneously. I have found that type theorists tend to promote &quot;version negotiation&quot; schemes where the two sides agree on one rigid protocol to follow, but this is extremely painful in practice: you end up needing to maintain parallel code paths, leading to ugly and hard-to-test code. Inevitably, developers are pushed towards hacks in order to avoid protocol changes, which makes things worse.<p>I don&#x27;t have time to address all the author&#x27;s points, so let me choose a few that I think are representative of the misunderstanding.<p>&gt; <i>Make all fields in a message required. This makes messages product types.</i><p>&gt; <i>Promote oneof fields to instead be standalone data types. These are coproduct types.</i><p>This seems to miss the point of optional fields. Optional fields are not primarily about nullability but about compatibility. Protobuf&#x27;s single most important feature is the ability to add new fields over time while maintaining compatibility. This has proven -- in real practice, not in theory -- to be an extremely powerful way to allow protocol evolution. It allows developers to build new features with minimal work.<p>Real-world practice has also shown that quite often, fields that originally seemed to be &quot;required&quot; turn out to be optional over time, hence the &quot;required considered harmful&quot; manifesto. In practice, you want to declare all fields optional to give yourself maximum flexibility for change.<p>The author dismisses this later on:<p>&gt; <i>What protobuffers are is permissive. They manage to not shit the bed when receiving messages from the past or from the future because they make absolutely no promises about what your data will look like. Everything is optional! But if you need it anyway, protobuffers will happily cook up and serve you something that typechecks, regardless of whether or not it&#x27;s meaningful.</i><p>In real world practice, the permissiveness of Protocol Buffers has proven to be a powerful way to allow for protocols to change over time.<p>Maybe there&#x27;s an amazing type system idea out there that would be even better, but I don&#x27;t know what it is. Certainly the usual proposals I see seem like steps backwards. I&#x27;d love to be proven wrong, but not on the basis of perceived elegance and simplicity, but rather in real-world use.<p>&gt; <i>oneof fields can&#x27;t be repeated.</i><p>(background: A &quot;oneof&quot; is essentially a tagged union -- a &quot;sum type&quot; for type theorists. A &quot;repeated field&quot; is an array.)<p>Two things:<p>1. It&#x27;s that way because the &quot;oneof&quot; pattern long-predates the &quot;oneof&quot; language construct. A &quot;oneof&quot; is actually syntax sugar for a bunch of &quot;optional&quot; fields where exactly one is expected to be filled in. Lots of protocols used this pattern before I added &quot;oneof&quot; to the language, and I wanted those protocols to be able to upgrade to the new construct without breaking compatibility.<p>You might argue that this is a side-effect of a system evolving over time rather than being designed, and you&#x27;d be right. However, there is no such thing as a successful system which was designed perfectly upfront. All successful systems become successful by evolving, and thus you will always see this kind of wart in anything that works well. You should want a system that thinks about its existing users when creating new features, because once you adopt it, you&#x27;ll be an existing user.<p>2. You actually <i>do not want</i> a oneof field to be repeated!<p>Here&#x27;s the problem: Say you have your repeated &quot;oneof&quot; representing an array of values where each value can be one of 10 different types. For a concrete example, let&#x27;s say you&#x27;re writing a parser and they represent tokens (number, identifier, string, operator, etc.).<p>Now, at some point later on, you realize there&#x27;s some additional piece of data you want to attach to every element. In our example, it could be that you now want to record the original source location (line and column number) where the token appeared.<p>How do you make this change without breaking compatibility? Now you wish that you had defined your array as an array of messages, each containing a oneof, so that you could add a new field to that message. But because you didn&#x27;t, you&#x27;re probably stuck creating a parallel array to store your new field. That sucks.<p>In every single case where you might want a repeated oneof, you always want to wrap it in a message (product type), and then repeat that. That&#x27;s exactly what you can do with the existing design.<p>The author&#x27;s complaints about several other features have similar stories.<p>&gt; <i>One possible argument here is that protobuffers will hold onto any information present in a message that they don&#x27;t understand. In principle this means that it&#x27;s nondestructive to route a message through an intermediary that doesn&#x27;t understand this version of its schema. Surely that&#x27;s a win, isn&#x27;t it?</i><p>&gt; <i>Granted, on paper it&#x27;s a cool feature. But I&#x27;ve never once seen an application that will actually preserve that property.</i><p>OK, well, I&#x27;ve worked on <i>lots</i> of systems -- across three different companies -- where this feature is essential.