Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google protobuf: if I add field from one side will it break another one?

I have two micro services. First service encodes messages using google protobuf (google docs) and sends to second one. Second one decodes this message and uses the data.

Now I need to add a field to this message object. If I do it at one side only will it break another side?

For example, if I add something to json this will break nothing. Is it really the same with google protobuf?

like image 374
Vitalii Avatar asked Aug 24 '17 06:08

Vitalii


People also ask

Is protobuf backwards compatible?

The proto-backwards-compatibility plugin is a Maven plugin to run a backwards compatibility check on a set of protobuf IDL files. The plugin can be integrated into various phases of a maven build to check that any changes to a set of . proto files are backwards compatible.

Can you remove field from protobuf?

Removing fields is fine, although you might want to mark it reserved so that nobody reuses it in an incompatible way. New code with old data (with the field) will silently ignore it; old code with new data will just load without the field populated, since everything in proto3 is implicitly optional .

Are repeated fields ordered in protobuf?

Yes, repeated fields retain the order of items. From Google's Protocol Buffers encoding specification: The order of the elements with respect to each other is preserved when parsing, though the ordering with respect to other fields is lost.

Does protobuf support polymorphism?

Bookmark this question. Show activity on this post. I'm trying to long-term serialize a bunch of objects related by a strong class hierarchy in java, and I'd like to use protocol buffers to do it due to their simplicity, performance, and ease of upgrade. However, they don't provide much support for polymorphism.


2 Answers

Cited from Goole Protobuf documentation on extending the extending the Protobuf from here:

you must not change the tag numbers of any existing fields.
you must not add or delete any required fields.
you may delete optional or repeated fields.
you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).

In your case, if your change on one side did not violate any of the above rules, it will not break the other side.

like image 177
Yu N. Avatar answered Oct 22 '22 06:10

Yu N.


From the docs (see here: https://developers.google.com/protocol-buffers/docs/overview):

You can add new fields to your message formats without breaking backwards-compatibility; old binaries simply ignore the new field when parsing. So if you have a communications protocol that uses protocol buffers as its data format, you can extend your protocol without having to worry about breaking existing code.

like image 25
It'sPete Avatar answered Oct 22 '22 08:10

It'sPete