We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Api Design Protocols
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Api Design Protocols
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
JSON is everywhere. Human-readable, universally supported, trivially easy to parse. But it has costs that are invisible at small scale and crushing at large scale.
A simple user object in JSON: {"id":123,"name":"Roni","email":"roni@example.com","active":true}. 62 bytes. Field names repeated in every single message. Quotes around strings. No type information. Every consumer must parse the text and infer types.
The same object in Protocol Buffers: ~30 bytes. No field names in the payload, just field numbers and values. Binary encoding, no text parsing. Types defined in the schema and enforced at compile time.
Google created Protocol Buffers in 2001 for internal use. By 2008, they open-sourced it. Today, Protobuf is the serialization format behind , Google's entire internal infrastructure, and thousands of high-performance systems. LinkedIn's data infrastructure team measured their Protobuf encoding at 20x faster than JSON for their messaging pipelines, processing 7 trillion messages per day.
1. Define the schema (.proto file):
syntax = "proto3";
message User {
int32 id = 1; // field number, not default value
string name = 2;
string email = 3;
bool active = 4;
repeated string tags = 5; // list
}
2. Compile to target language:
protoc --python_out=. --java_out=. --go_out=. user.proto
This generates type-safe classes in Python, Java, Go (and 10+ other languages). Each class has getter/setter methods for every field, serialization methods, and builder patterns.
3. Serialize and deserialize:
user = User(id=123, name="Roni", email="roni@example.com", active=True)
binary = user.SerializeToString() # ~30 bytes of binary
user2 = User.FromString(binary) # type-safe deserialization
Why field numbers matter. In JSON, the key "name" is sent with every message. In Protobuf, the field number 2 (1 byte) replaces the key. This is why Protobuf is smaller: field names exist only in the schema, never in the serialized data. Changing a field name in the .proto file does not break wire compatibility because the number stays the same.
Schema evolution rules:
The golden rule: never reuse a field number, even after deleting a field. Use the reserved keyword to prevent accidental reuse.