How does gRPC handle serialization compared to JSON?
See how gRPC serializes data with Protocol Buffers versus JSON — binary vs text, payload size, speed, schema compatibility, examples and interview answers.
Expected Interview Answer
gRPC serializes data using Protocol Buffers, a compact binary format defined by a schema, whereas JSON is a self-describing, human-readable text format. Protobuf produces smaller payloads and faster encoding/decoding because field names are replaced by numeric tags and values are packed as binary.
In JSON every message repeats field names as text and numbers are stored as ASCII, so payloads are larger and parsing is slower. Protobuf uses a shared .proto schema so the wire format carries only field numbers and typed binary values, not names. This yields smaller messages, faster (de)serialization, and strong typing with built-in backward/forward compatibility via field numbers — at the cost of not being human-readable and requiring the schema to interpret.
- Smaller payloads than equivalent JSON
- Faster encoding and decoding
- Strong typing enforced by the schema
- Backward/forward compatibility via field numbers
- Language-neutral generated code
AI Mentor Explanation
JSON is like a full radio commentary that names every player, position, and event in plain words each ball, easy for anyone to follow but slow and wordy. Protobuf is the terse scorecard notation both statisticians agree on beforehand — numbers and symbols only — compact and instantly parsed by anyone who holds the same key.
Step-by-Step Explanation
Step 1
Define the schema
Write a .proto file declaring message types with typed fields and unique field numbers.
Step 2
Compile
protoc generates serialization code for your language from the schema.
Step 3
Serialize to binary
At runtime, values are encoded as tag (field number + wire type) plus packed binary value — no field names on the wire.
Step 4
Transmit compactly
The small binary payload is sent over HTTP/2, using less bandwidth than the JSON equivalent.
Step 5
Deserialize with the schema
The receiver uses the same .proto to map field numbers back to typed values.
What Interviewer Expects
- Naming Protocol Buffers as gRPC's default serialization
- Binary vs text and why binary is smaller/faster
- Role of field numbers for compatibility
- Trade-off: not human-readable, needs the schema
- When JSON might still be preferred (debuggability, browser APIs)
Common Mistakes
- Saying gRPC uses JSON by default
- Claiming protobuf is human-readable on the wire
- Ignoring that a shared schema is required to decode
- Confusing field names with field numbers as the wire identifier
- Overstating protobuf speed without noting the schema/tooling cost
Best Answer (HR Friendly)
“gRPC packs data into a compact binary format called Protocol Buffers using a shared schema, which is smaller and faster than JSON's readable text. JSON is easier for humans to read, but protobuf is more efficient for machine-to-machine communication.”
Code Example
message User {
int32 id = 1;
string name = 2;
bool active = 3;
}// JSON (~40 bytes, field names repeated as text)
{"id":42,"name":"Ada","active":true}
// Protobuf (~9 bytes, field NUMBERS + binary values)
// 08 2A field 1 (id) = 42
// 12 03 41 64 61 field 2 (name) = "Ada"
// 18 01 field 3 (active) = trueFollow-up Questions
- Why are protobuf field numbers critical for compatibility?
- When would you still choose JSON over protobuf?
- What is gRPC-Web and how does it affect serialization?
- How does protobuf handle adding or removing fields safely?
- What are the downsides of a binary, schema-driven format?
MCQ Practice
1. What serialization format does gRPC use by default?
gRPC uses Protocol Buffers, a compact binary schema-based format, by default.
2. Why is protobuf typically smaller than JSON on the wire?
Protobuf sends field numbers plus binary values instead of repeating textual field names, shrinking payloads.
3. What is required to decode a protobuf message?
Protobuf is not self-describing; the receiver needs the shared schema to map field numbers back to fields.
Flash Cards
gRPC's default serialization? — Protocol Buffers — a compact, schema-defined binary format.
Why is protobuf smaller than JSON? — It sends numeric field tags and binary values instead of repeating textual field names.
What enables protobuf compatibility? — Stable field numbers — you can add fields without breaking old readers.
A downside of protobuf vs JSON? — Not human-readable and requires the shared schema to decode.