gRPC — Explained with Examples
gRPC is a high-performance Remote Procedure Call framework by Google that uses Protocol Buffers and HTTP/2 for efficient service-to-service communication.
gRPC stands for gRPC Remote Procedure Calls (the ‘g’ stands for different things in different versions). It was open-sourced by Google in 2015 and has become the backbone of many microservices architectures.
How gRPC Works
Instead of sending JSON over HTTP/1.1 like REST, gRPC uses Protocol Buffers (protobuf) — a binary serialization format — and runs over HTTP/2, which enables multiplexing, header compression, and bidirectional streaming.
// Define a service in a .proto file
syntax = "proto3";
service UserService {
rpc GetUser (GetUserRequest) returns (User);
rpc ListUsers (ListUsersRequest) returns (stream User);
rpc UpdateUser (stream UpdateUserRequest) returns (User);
rpc Chat (stream ChatMessage) returns (stream ChatMessage);
}
message GetUserRequest {
int32 id = 1;
}
message User {
int32 id = 1;
string name = 2;
string email = 3;
}Real-World Analogy
REST is like sending letters in envelopes (HTTP/1.1 with JSON) — each letter is self-contained but bulky. gRPC is like a phone call over a fast line (HTTP/2 with protobuf) — you have a persistent connection, data flows in both directions, and the conversation is compressed and efficient.
Streaming Types
gRPC supports four communication patterns:
- Unary — single request, single response (like REST)
- Server streaming — one request, stream of responses
- Client streaming — stream of requests, one response
- Bidirectional streaming — both sides send streams simultaneously
Example: gRPC Client in Node.js
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDef = protoLoader.loadSync('user.proto');
const proto = grpc.loadPackageDefinition(packageDef);
const client = new proto.UserService(
'localhost:50051',
grpc.credentials.createInsecure()
);
client.getUser({ id: 1 }, (err, response) => {
console.log(response); // { id: 1, name: 'Alice', email: 'alice@example.com' }
});Related Terms
REST, GraphQL, WebSocket, API Gateway, HTTP
Related Tutorial
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro