Getting Started with Protocol Buffers

Getting Started with Protocol Buffers

You should use Google's Protocol Buffers to store the messages sent between servers in the Raft projects: they handle a bunch of serialization and deserialization issues that you would otherwise have to address yourself. Here are a few bits and pieces of information to help you get started with protobufs.

  • I recommend that you use proto3 (the newest version of protobufs) since it provides additional facilities for defining messages, such as oneof.

  • The root of the documentation for protobufs is at https://developers.google.com/protocol-buffers.

  • The first step is to learn about the special-purpose language for defining message formats (.proto files). For an introduction to this, see the Language Guide for proto3.

  • Next, you'll need to learn about using protoc to compile the .proto files into C++ header and class files. That is described in C++ Protocol Buffer Basics.

  • Next, you'll need to learn about instantiating protobuf objects, and the methods available for reading and writing fields in them. This is also described in C++ Protocol Buffer Basics.

  • Finally, you'll need to learn how to serialize protobufs (e.g., into strings) for transmitting over the network, and how to deserialize back from network data to protobuf objects. This is done with methods such as SerializeToString and ParseFromString. There are also many other options available, depending on how you want to structure your code. The best way to learn about these methods is with the C++ API documentation. In particular, the serialization and deserialization methods are all part of the MessageLite class, which is a superclass of all the protobuf classes.