🔌 gRPC Advanced

What is the gRPC Protobuf serialization format and why is it efficient?

Answer

Protobuf uses a compact binary encoding based on field numbers and wire types. Each field is encoded as: (field_number << 3) | wire_type followed by the value. Wire types: 0 (varint — integers, bools, enums), 1 (64-bit — double, fixed64), 2 (length-delimited — strings, bytes, embedded messages), 5 (32-bit — float, fixed32). Varint encoding compresses small integers efficiently: numbers 0–127 encode in 1 byte, 128–16383 in 2 bytes — perfect for typical integer values. Fields with default values are omitted entirely — a zero integer is not serialized, dramatically reducing message size for sparse messages. No field names on the wire — only field numbers are encoded, saving bytes and enabling faster parsing. No schema negotiation per message — the schema is compiled into the client/server code. Combined, these choices explain why Protobuf messages are 3–10x smaller than equivalent JSON and 5–100x faster to parse, especially for high-volume, small messages.