why use protocol buffers in java

715 views Asked by At

Recently,I read code of HBase.I find client use protobuf to communicate with server in HBase's code.

Java has "Serializable". why not use it?

2

There are 2 answers

0
Jon Skeet On BEST ANSWER
  • Efficiency: protocol buffers are generally much more efficient at transmitting the same amount of data than Java binary serialization
  • Portability: Java binary serialization is not widely implemented outside Java, as far as I'm aware (unsurprisingly)
  • Robustness in the face of unrelated changes: unless you manually specify the serializable UUID, you can end up making breaking changes without touching data at all in Java. Ick.
  • Backward and forward compatiblity: old code can read data written by new code. New code can read data written by old code. (You still need to be careful, and the implications of changes are slightly different between proto2 and proto3, but basically protobuf makes this a lot easier to reason about than Java.)
  • It's a lot easier to accidentally introduce non-serializable members into Java binary serialization, as proto descriptor files are all about serialization... you can't refer to an arbitrary class, etc.

I've worked on projects using protocol buffers, and I've worked on projects using Java binary serialization - and I would be very reluctant to use the latter again...

0
Shaun On

Protocol Buffers is an open serialization protocol. You could write a client in C++ or C# and still be able to communicate with the server if both ends are using the same Protocol Buffer schema. Java Serializable is Java only