How gRPC handles backward compatibility with proto files
The most important point about gRPC.
Both Server and Client MUST have the proto file — or at least the generated classes from the proto file.
Why is this necessary?
Because gRPC doesn’t send raw JSON or XML over the wire.
Instead, it sends binary data based on the structure defined in the .proto
file.
How gRPC Works (Step-by-Step)
- You create the
.proto
file describing:- Services
- RPC methods
- Request & Response message types
- Data types
- Both client and server must generate code from this
.proto
using:protoc
(Protocol Buffer Compiler)- Language-specific plugins (Java, Python, Go, etc.)
Example .proto
file:
protoCopyEditsyntax = "proto3";
message User {
string name = 1;
int32 age = 2;
}
service UserService {
rpc GetUser(User) returns (User);
}
What Happens Under the Hood 🔍
Step | Server | Client |
---|---|---|
Define .proto | ✅ Yes | ✅ Yes |
Generate Code | protoc --java_out=... | protoc --java_out=... |
Implement Logic | Writes UserServiceImpl.java | Calls userService.getUser() |
Communication | Encodes User into binary | Decodes binary into User |
The BIG Difference 🔥
In REST APIs:
- Client only needs the API URL
- No code sharing is required
- Only JSON schema is needed (optional)
In gRPC:
- Client must know the data structure before calling the API
- Server cannot send unknown fields to the client
- The proto file is the contract between client and server
How Client Gets the Proto File? 🤔
✅ Usually, server provides:
- SDK (pre-generated client classes)
.proto
file + instructions- Git repo with client library
Example Folder Structure
pgsqlCopyEditservice-repo/
├─ user.proto # Proto file
├─ server/
│ ├─ UserService.java
│ └─ pom.xml
└─ client/
├─ UserClient.java
└─ pom.xml
What If Client Doesn’t Have Proto File?
❌ It won’t work
Because without the .proto
file, the client can’t:
- Serialize requests
- Deserialize responses
- Understand what fields exist
Why Did Google Design It This Way?
gRPC is built for microservices + high-performance systems — where contract enforcement is critical.
✅ With gRPC, client and server ALWAYS agree on:
- Data types
- Field names
- Field order
- Optional vs Required fields
Can There Be a Generic gRPC Client?
No 🚫.
You always need generated code or the proto file to interact with a specific gRPC service.
This is What Makes gRPC:
Feature | REST | gRPC |
---|---|---|
Loose Coupling | ✅ Yes | ❌ No |
Performance | 🔴 Slow | 🔥 Fast |
Type Safety | 🔴 No | ✅ Yes |
Automatic Code Generation | ❌ No | ✅ Yes |
Contract-Based | ❌ Optional | 🔥 Mandatory |
Unknown Fields | ✅ Ignored | ❌ Error |
Summary 💪
Tech | When to Use? |
---|---|
REST | Public APIs, Simple Services |
gRPC | Microservices, High Performance, Internal APIs |
Final Answer 🔥🔥
If anyone wants to call a gRPC service — they MUST either:
- Use the generated client SDK (from the server)
- Have the
.proto
file and generate client code themselves
Why People Get Confused?
Because HTTP APIs like REST are open — anyone can call them without needing extra files.
But gRPC is closed by default — only those who have the proto file (or generated SDK) can talk to the server.