Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Protocol buffer as general Data object?

We're introducing protocol buffers as the new transport for some back end RPC services. Because there's resistance to manually shuttling data between different forms of similar objects, I can forsee the Protocol Buffer instances being passed up the stack a bit higher than just to the RPC server interface.

Is this something that I should try to avoid? Is it safe to treat a protocol buffer object like a plain data holder, with the nice convenience that it can quickly and efficiently be transformed into and out of binary?

The other reason I see it as being a nice way to generate data objects is that the notion of required/optional fields and the automatically generated builder interface.

like image 602
Mark Renouf Avatar asked Nov 25 '09 19:11

Mark Renouf


People also ask

What are protocol buffers used for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

What is protocol buffer data?

What are protocol buffers? Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

Why are protocol buffers better than JSON?

Protocol buffers are much faster than JSON. JSON is lightweight and is faster than other serialization techniques like pickling. Protobuf schemas are encoded along with data; it ensures that signals don't get lost between applications. Language interoperability.

How do I send Protobuf over HTTP?

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.


1 Answers

Well, they're not terribly convenient to use that way as they're immutable - you could pass the builders around, but that makes for rather long type names. It also means you're limited to the data types supported by protocol buffers (and your own messages).

It's safe to do this, but it doesn't always create the nicest of designs. On the other hand, sometimes it's just what the doctor ordered :)

I suggest you experiment - there's no "one size fits all" here.

like image 171
Jon Skeet Avatar answered Sep 21 '22 18:09

Jon Skeet