Protobuf
A data description language, a compact binary message format suitable for data transmission and data storage.
Message length: protobuf-packed data does not include length information by default, requiring applications to handle message splitting when sending and receiving messages themselves.
Therefore, the message header will have a fixed-length headerLen that describes the message length.
Message type: no built-in message type, requiring the sender to pass type information to the receiver, who then creates specific Message objects based on the received message type.
Protobuf has automatic message type reflection functionality, which can create Message objects of corresponding types based on type names. When a program adds a new protobuf message, this part of code does not need modification and does not require registering message types.
Limitations of JSON Encoding
Sacrifices certain performance for readability (object memory allocation size is uncertain), lacks type descriptions, and contains significant encoding redundancy.
Advantages of Protobuf
- Pre-defines data field types: deterministic memory allows for fast, one-time fixed-size memory allocation
- Transmits message unit names as message names rather than specific field names (compact storage), with both client and server storing these data field contents
- Data fields are assigned unique typeIDs for marking, with both parties pre-agreed on IDs to further compress storage
Actual transmitted message data
---------------------------------
|Header: records data length |
|---------------------------------|
|Message name: used to find |
|corresponding msg object |
|---------------------------------|
|Actual data: stored in binary |
---------------------------------In proto 3, fields are initialized to zero values by default (similar to Go structs), removing required and optional keywords.