Remix.run Logo
klodolph 7 days ago

Protobuffers suck as a core data model. My take? Use them as a serialization and interchange format, nothing more.

> This puts us in the uncomfortable position of needing to choose between one of three bad alternatives:

I don’t think there is a good system out there that works for both serialization and data models. I’d say it’s a mostly unsolved problem. I think I am happy with protobufs. I know that I have to fight against them contaminating the codebase—basically, your code that uses protobufs is code that directly communicates over raw RPC or directly serializes data to/from storage, and protobufs shouldn’t escape into higher-level code.

But, and this is a big but, you want that anyway. You probably WANT your serialization to be able to evolve independently of your application logic, and the easy way to do that is to use different types for each. You write application logic using types that have all sorts of validation (in the "parse, don't validate" sense) and your serialization layer uses looser validation. This looser validation is nice because you often end up with e.g. buggy code getting shipped that writes invalid data, and if you have a loose serialization layer that just preseves structure (like proto or json), you at least have a good way to munge it into the right shape.

Evolving serialized types has been such a massive pain at a lot of workplaces and the ad-hoc systems I've seen often get pulled into adopting some of the same design choices as protos, like "optional fields everywhere" and "unknown fields are ok". Partly it may be because a lot of ex-Google employees are inevitably hanging around on your team, but partly because some of those design tradeoffs (not ALL of them, just some of them) are really useful long-term, and if you stick around, you may come to the same conclusion.

In the end I mostly want something that's a little more efficient and a little more typed than JSON, and protos fit the bill. I can put my full efforts into safety and the "correct" representation at a different layer, and yes, people will fuck it up and contaminate the code base with protos, but I can fix that or live with it.

ziml77 6 days ago | parent [-]

> My take? Use them as a serialization and interchange format, nothing more.

Isn't that exactly what they're intended for? I'm confused how anyone would even think to use them any other way.

tiew9Vii 5 days ago | parent | next [-]

Not specific to protobufs but a lot of people/projects especially if doing MVC, push the models in the API layer all the way down the stack and they become the domain, instead of having a loose coupling between the domain and serialization format. In the old days we used to have DTO's for separation but they went out of fashion.

blarus78 5 days ago | parent | prev | next [-]

Agreed, it's interesting to see so many people complaining when they are just misunderstanding / misusing protobufs entirely. Sure the implementation could be better but it's not a huge problem.

klodolph 6 days ago | parent | prev [-]

Like the author said, their usage in practice often creeps outside that.