Understanding gRPC
A Guide to High-Performance Microservices
gRPC = Google Remote Procedure Call
gRPC is very important if you want to work in microservices, backend, or high-performance systems.
It is a framework that allows one application to call a method in another application as if it were a local function call, even if it’s running on another server.
It is built by:
Google
Uses Protocol Buffers (Protobuf)
Uses HTTP/2
🧠 Simple Definition
gRPC allows one service to directly call methods on another service over the network.
Instead of sending JSON like REST, it sends binary data (faster + smaller).
🌍 Real-World Example: Food Delivery App
Imagine you are building Swiggy/Zomato type system.
You have microservices:
Order Service
Payment Service
Restaurant Service
Delivery Service
🔹 Scenario: User Places Order
Order Service receives order
It must:
Check restaurant availability
Process payment
Assign delivery agent
Instead of calling REST APIs like:
POST /payment/process
POST /restaurant/check
With gRPC, Order Service directly calls methods like:
paymentService.processPayment(orderDetails);
restaurantService.checkAvailability(itemId);
Even though those services are on different servers.
It feels like a normal method call.
That’s gRPC magic.
🧠 1️⃣ What is Protocol Buffers (Protobuf)?
Protocol Buffers is a binary serialization format created by:
It is used to:
Define message structure
Serialize data efficiently
Generate code automatically
Think of Protobuf as:
A strongly typed contract between client and server.
🧾 2️⃣ What is a .proto File?
A .proto file defines:
Messages (data structure)
Services (methods)
RPC functions
Example:
syntax = "proto3";
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
}
message UserRequest {
int32 userId = 1;
}
message UserResponse {
string name = 1;
string email = 2;
}
This is like a contract.
🏗 3️⃣ What Happens After Writing .proto?
You run Protobuf compiler:
protoc
It generates:
Java classes
Client stub
Server stub
Automatically.
🧩 4️⃣ What is a Stub?
Stub = Auto-generated helper class.
There are two types:
🔹 Client Stub
Used by client to call remote service.
Example:
UserResponse response = stub.getUser(request);
Looks like normal method call.
But internally:
Converts request to binary
Sends via HTTP/2
Waits for response
Converts binary back to object
🔹 Server Stub (Base Class)
Server extends generated base class:
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
You override:
@Override
public void getUser(UserRequest request,
StreamObserver<UserResponse> responseObserver)
This is server-side logic.
⚙️ How gRPC Internally Works
Step-by-step:
1️⃣ Client calls stub method
2️⃣ Stub serializes using Protobuf
3️⃣ Sent over HTTP/2
4️⃣ Server receives
5️⃣ Server stub deserializes
6️⃣ Server executes logic
7️⃣ Response serialized
8️⃣ Sent back
9️⃣ Client stub deserializes
⚡ Why gRPC is Faster Than REST?
| REST | gRPC |
| Uses HTTP/1.1 | Uses HTTP/2 |
| Uses JSON | Uses Binary (Protobuf) |
| Text-based | Compact binary |
| Slower | Faster |
