• Uncategorised
  • 0

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)

  1. You create the .proto file describing:
    • Services
    • RPC methods
    • Request & Response message types
    • Data types
  2. 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 🔍

StepServerClient
Define .proto✅ Yes✅ Yes
Generate Codeprotoc --java_out=...protoc --java_out=...
Implement LogicWrites UserServiceImpl.javaCalls userService.getUser()
CommunicationEncodes User into binaryDecodes 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:

FeatureRESTgRPC
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 💪

TechWhen to Use?
RESTPublic APIs, Simple Services
gRPCMicroservices, High Performance, Internal APIs

Final Answer 🔥🔥

If anyone wants to call a gRPC service — they MUST either:

  1. Use the generated client SDK (from the server)
  2. 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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *