text stringlengths 0 2.35k |
|---|
**Jon Calhoun:** So Akshay, we first started talking back whenever Mat did an episode on - what was it, bloat that can come with software? And protocol buffers came up because there's something that when you import them, they can lead to a lot of extra code being imported sort of behind the scenes, without realizing it... |
**Akshay Shah:** That's a great question. Protocol buffers are actually -- they're two separate things, that are closely related, but it's important to have a little bit of airspace between them. The first thing they are is they're a little language for writing schemas for your data. And the second thing is that it's a... |
**Jon Calhoun:** So I guess if we're looking at them as a tool to make programmers more efficient, was that the only reason they were created, was just that efficiency? |
**Akshay Shah:** I think no, but I think certainly today that's the most compelling thing about them. So if you imagine, we're mostly Go programmers and Go has really found a home in building network services, and building microservices... So I imagine that a lot of Go programmers have written a REST API, or at least w... |
Google was running into this in the early 2000s. At the time, XML was really in vogue, and they were looking for something that was simpler, that was more productive for programmers, and as an important concern for Google, but maybe not so much for the rest of us, something that was more efficient for computers... And ... |
**Jon Calhoun:** \[06:12\] So I guess I'm thinking of this, if it's something Google invented for Google scale, is this one of those technologies that is only a good fit if you're a Google? Or what types of applications does this fit well for? |
**Akshay Shah:** Yeah, it's certainly compelling if you're at Google scale; I mean, clearly, they're still protobuf from top to bottom... And most other large -- if you look at a lot of these large tech companies, they have some equivalent protocol buffers. Sometimes it's actually a protobuf, sometimes it's a very simi... |
But for any Gopher, I think you want to avoid redoing the same error-prone work over and over again. That's why we're not calling the pthread APIs directly, we're not freeing memory manually... These are just tedious, error-prone tasks. |
So if you're building an API and you have clients, writing a protobuf schema is basically the same amount of work as writing a Go struct. It looks pretty similar; it's a name, a curly brace, some field names and some types. There's some numbers for the fields, but other than that, it's basically the same thing. You run... |
**Johnny Boursiquot:** What are you trading off? This sounds all good, but there's always a trade-off. |
**Akshay Shah:** Of course there is, yeah. So I think in Go, you're not trading off a ton, because you would have been handwriting these structs one way or another. I don't see much Go code that's really dealing with JSON as like map string any, as part of writing an API. In other languages, you do trade off the abilit... |
In Go, sometimes the protobuf representation of a type in JSON is not what you might expect. So if you serialize an int64 to JSON with protocol buffers, what you get as a string. And that's because many JSON runtimes treat numbers as floats, and so there isn't 64 bits of space available, so the only safe way to send th... |
You have to deal with the protobuf toolchain, which often has some rough edges... And we can talk about all that stuff in detail if you want... But especially in Go, I don't think the trade-offs are that -- you're getting a lot for free, and you're only giving up a little bit. |
**Jon Calhoun:** When you mentioned the trade-off of not being able to have the more flexible API, I guess, I also view that as a win. It's kind of a trade-off, but also a win, because I've also worked with APIs where trying to write a library in Go that works with it is very challenging, because you can tell the API w... |
**Akshay Shah:** \[10:07\] That's right. Yeah. One of my co-workers often describes protobuf as static typing for data. And it has basically the same trade-offs as static typing for programming languages. You have to declare your types upfront, changing them can be a little bit harder - you have to think about forward ... |
**Jon Calhoun:** This kind of reminds me of -- there's a couple of tools around JSON APIs where you basically define a schema and it helps generate libraries for various languages... I think - was Swagger one of those? |
**Akshay Shah:** Swagger is a little bit more extensive. Swagger -- at least as we've talked about them, right? Protocol buffers are just about structs. They're about data shapes. |
**Jon Calhoun:** Okay. |
**Akshay Shah:** Swagger covers that, but it also covers the shape of your REST API. Maybe this whole show is gonna be an unpopular opinion, but my view of like -- if you look at a Swagger schema, or you look at a JSON schema, you look at it and you're like "Oh my God, am I really supposed to be writing this by hand?" ... |
**Jon Calhoun:** I haven't used Swagger a lot, so I can't speak to that... But I can definitely say, I've seen a lot of companies that were writing APIs that needed -- basically, it's a public API, so they wanted to provide libraries for various languages... And you would think Swagger was a perfect fit, but because fo... |
**Akshay Shah:** I'm not sure. Swagger, the new name is OpenAPI; there have been a couple of version, so they're on OpenAPI v3 now... And it includes most of JSON schema, which is the data struct part of the language. We can talk about the network API parts of it later... I think it's a better fit for comparing to gRPC... |
**Johnny Boursiquot:** See, now I have to buy another tool, and learn how to use that tool, and maybe it has its own quirks... So yeah, for vendors it's great. They're like "Yeah, we'll sell you something to do all this for you." But yeah, now I have another dependency. |
**Akshay Shah:** \[14:01\] Exactly. And if you look at the specification, there are parts of this language that 0 I understand why in a really loosely federated world of the web, some of these things make sense. But if you're a company or a person publishing a schema, they're a little off-putting to me as a Go programm... |
Protobuf is much more on that side of the world... Part of why I think that's so appealing in Go is that protocol buffers in Go share a lot of DNA from Google. A protobuf message looks a lot a Go struct. And so if you are a Go developer and you're thinking, "Which of these should I do?", the amount of effort it takes t... |
So John, for the situation you are describing, you can say, "This field is one of the following types." And that generates code that's a little bit awkward, kind of unavoidably... But you can express this. You can also express the idea like "This field is just a thing. And I have no idea what it is, we'll find out at r... |
**Jon Calhoun:** Yeah. So if we're looking at protocol buffers, I think it's commonly compared with things JSON, or maybe GraphQL... Would you compare it to GraphQL? |
**Akshay Shah:** Mm-hm. |
**Jon Calhoun:** Okay. So if we're looking at those things and we're trying to expose something to, let's say, the public; we wanted to build an API that has public consumers not just internally, are protocol buffers a good fit for that type of situation? |
**Akshay Shah:** I think they are, with one caveat. I think protobuf is simple, especially if you use protobuf schemas to accept and send JSON. This is really easy for other people to use, because they don't really have to know about the protobuf part at all. If they would to continue handwriting classes to generate JS... |
The one caveat is that historically, the tools to work with protocol buffers are kind of rough. They're open sourced kind of directly from the way Google uses them... And within Google, they're part of this really sophisticated, unified build system, and monorepo, and all this other stuff. So the protobuf tools are thi... |
\[17:49\] Outside of Google - well, we just have the protobuf piece, but not the rest of the stack. And so if you're building an external-facing API, and you're working in Go, and you've got all this protobuf stuff figured out, but your client is like a Node client, and you go to them and say, "Well, step one is you ne... |
So I think there's a lot of space to build tools that make that easier and more approachable. There's no reason why, given a protobuf schema, your instructions to the client can't be "Hey, don't worry about any of the protobuf stuff. From your perspective, I handwrote a client for you, and it's an npm package. Just dow... |
**Johnny Boursiquot:** So we've talked about the efficiency of not having to hand-wrangle all these things, but we haven't talked really about -- we've mentioned it, but never really talked about sort of the efficiency of transport, right? Basically, when you have that binary format; can you sort of clarify what the ma... |
**Akshay Shah:** Yeah. Typically, binary formats have a bunch of optimizations available to them, that text-based formats could technically use, but rarely do. So if you imagine how a computer is parsing a JSON object, usually you're kind of like advancing one rune at a time, and then maintaining some lookback, and you... |
And typically, protobuf goes out of its way to try and make that binary small. So it does a bunch of tricks to minimize the size of things. Ultimately, these are all really clever tricks, but JSON is also really widely used. You can make JSON incredibly fast. There's this CS professor in Canada named Daniel Amir, who d... |
So I think once you're using a protobuf schema, you have this binary format at your fingertips, and so you might as well use it. I mean, even if the practical perf improvement for your particular use case is 20%... 20% is kind of a lot. You might as well just grab it and get that benefit if it's sitting right in front ... |
**Johnny Boursiquot:** No, no, that makes sense. So while you were talking, it occurred to me, or I just recalled that the Go standard library has an encoding mechanism which supports binary transmitting... |
**Akshay Shah:** Yes. Marshal Binary? |
**Johnny Boursiquot:** Yes, there is that, but I'm also thinking of the Gob package. |
**Akshay Shah:** Yes. |
**Johnny Boursiquot:** Let's talk about the Gob package. Why is it that we're not all using Gob? Is it that we're not all Go developers? \[laughs\] |
**Akshay Shah:** \[21:57\] I mean, that's partly it, right? But the Gob package - my recollection, at least, is that the Gob package makes some very important -- there's some very important caveats in the package docs for Gob. One of them is that - this is just my recollection; I can check real quick, but... From what ... |
Protobuf is language-agnostic, it has a specification that's public, and it has just like a tremendous number of miles put on it. And I don't think you get any of that from Gob. |
**Johnny Boursiquot:** Yeah, fair enough. |
**Akshay Shah:** Gob is super-convenient though... |
**Johnny Boursiquot:** It is. |
**Akshay Shah:** You can just marshal a thing, and that's really nice... I would love it if you could do that with protocol buffers, but you just can't. |
**Johnny Boursiquot:** You need Go on either side of the fence, obviously... That is one of the drawbacks, right? You don't have that cross-language support unless everybody starts implementing a Gob encoder and decoder and whatnot. |
**Akshay Shah:** And parsing a Gob... The schema is a struct, right? So parsing that requires parsing Gob, right? Which is kind of a tall order. It's not totally obvious, to me at least... I think it would be challenging to write a tool, for example, that looks at a dif between two Go files, and says, "Hey, stop right ... |
**Johnny Boursiquot:** Trivial. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.