Definition:
Protocol Buffers (Protobuf) is a language-neutral, platform-neutral, efficient binary serialization format developed by Google.
It’s used to encode structured data — mainly in gRPC but also in other systems where compact, fast data transmission is critical.
🧠 Core Idea:
Define your data structure in a
.protofile → compile it → get efficient code to serialize/deserialize data.
🔧 Key Features:
| Feature | Description |
|---|---|
| Compact | Much smaller than JSON or XML (binary, not text) |
| Fast | Extremely fast to parse and serialize |
| Cross-language | Works in many languages (Java, Go, Python, JS, etc.) |
| Strongly Typed | Fields are explicitly typed |
| Version Safe | Supports backward & forward compatibility |
| Code Generation | Generates client/server/data classes from .proto files |
📁 Example:
// user.proto
syntax = "proto3";
message User {
int32 id = 1;
string name = 2;
string email = 3;
}
This creates classes (like User) in your language of choice. You can now serialize/deserialize them with blazing speed.
🧪 Protobuf vs JSON:
| Feature | Protobuf | JSON |
|---|---|---|
| Format | Binary | Text |
| Size | Smaller | Larger |
| Speed | Faster | Slower |
| Human-readable | ❌ No | ✅ Yes |
| Schema Required | ✅ Yes (via .proto) | ❌ No |
| Versioning | Built-in field numbering | Manual |
🔄 Workflow:
- Write a
.protofile describing your data. - Run the protoc compiler to generate code.
- Use generated code to serialize and deserialize data.
🚀 Where It’s Used:
- gRPC (as default serialization format)
- Google’s internal services
- Mobile apps (because it’s compact)
- IoT and bandwidth-sensitive apps