Architecture
gRPC and Protobuf
gRPC is Google's high-performance RPC framework. Master Protocol Buffers, streaming patterns, interceptor chains, and building efficient microservice communication at scale.
- Performance β Binary protocol, 10x faster than JSON
- Streaming β Client, server, and bidirectional streaming
- Type Safety β Strongly typed contracts with code generation
gRPC is what REST would be if it were designed for microservices.
Protocol Buffers
DfProtocol Buffers
Protocol Buffers (protobuf) is a language-neutral, platform-neutral mechanism for serializing structured data. It uses a .proto file to define the data structure and service interface, then generates code in multiple languages. Protobuf is 10x smaller and 10x faster than JSON for serialization.
Proto File Definition
Proto File for a User Service
syntax = "proto3";
package userservice;
option java_package = "com.example.userservice";
// Message definition
message User {
int64 id = 1;
string name = 2;
string email = 3;
UserRole role = 4;
google.protobuf.Timestamp created_at = 5;
}
enum UserRole {
USER_ROLE_UNSPECIFIED = 0;
USER_ROLE_ADMIN = 1;
USER_ROLE_MEMBER = 2;
USER_ROLE_GUEST = 3;
}
// Service definition
service UserService {
rpc GetUser(GetUserRequest) returns (User);
rpc ListUsers(ListUsersRequest) returns (stream User);
rpc CreateUser(CreateUserRequest) returns (User);
rpc UpdateUser(stream UpdateUserRequest) returns (UpdateUserResponse);
rpc Chat(stream ChatMessage) returns (stream ChatMessage);
}
message GetUserRequest {
int64 id = 1;
}
Serialization Comparison
| Metric | JSON | Protobuf |
|---|---|---|
| Size | Large (text) | Small (binary) |
| Speed | Slow (parse) | Fast (decode) |
| Schema | Optional | Required |
| Human Readable | Yes | No |
| Cross Language | Yes | Yes |
Protobuf Size Advantage
Here,
- =Protobuf serialized size
- =JSON serialized size
Size Comparison
For a User object with 10 fields:
JSON: {"id":123,"name":"Alice","email":"alice@example.com","role":"ADMIN",...} β 200 bytes Protobuf: Binary encoding with field tags β 80 bytes
Over millions of messages, this 60% reduction in size translates to significant bandwidth and storage savings.
gRPC Communication Patterns
HTTP/2 Features
| Feature | Description | Benefit |
|---|---|---|
| Multiplexing | Multiple streams over single connection | No head-of-line blocking |
| Header Compression | HPACK compression | Smaller headers |
| Server Push | Proactive responses | Reduced latency |
| Binary Protocol | Binary framing | Faster parsing |
gRPC Interceptors
DfgRPC Interceptor
gRPC interceptors are middleware that can modify requests and responses. They are used for authentication, logging, monitoring, rate limiting, and error handling. Interceptors can be applied on both client and server sides.
| Interceptor Type | Use Case |
|---|---|
| Unary Interceptor | Modify unary calls |
| Stream Interceptor | Modify streaming calls |
| Client Interceptor | Client-side middleware |
| Server Interceptor | Server-side middleware |
gRPC Interceptor Chain
Request β Auth Interceptor β Rate Limit Interceptor β Logging Interceptor β Handler
Response β Auth Interceptor β Rate Limit Interceptor β Logging Interceptor β Handler
Practice Exercises
-
Proto Design: Design the proto files for an e-commerce system with Product, Order, and Payment services. Define the messages and service RPCs.
-
Streaming Design: Design a real-time notification system using gRPC bidirectional streaming. How do you handle connection failures and message ordering?
-
Performance Comparison: Benchmark gRPC vs REST for a microservice that returns 1KB of data. What are the expected latency and throughput differences?
-
Migration Plan: Your team is migrating from REST to gRPC. Design the migration strategy, including compatibility layers and gradual rollout.
Key Takeaways:
- Protocol Buffers provide 10x smaller and faster serialization than JSON
- gRPC supports four patterns: unary, server stream, client stream, bidirectional
- HTTP/2 enables multiplexing, header compression, and binary framing
- Interceptors provide middleware for auth, logging, and rate limiting
- gRPC is ideal for internal microservice communication
- Use gRPC when performance matters; REST for public APIs
What to Learn Next
-> WebSockets and Real-Time WebSocket protocols, connection management, and scaling patterns.
-> Elixir and Phoenix for Real-Time Building real-time systems with the BEAM VM and Phoenix Channels.
-> API Design REST, GraphQL, gRPC, versioning, and rate limiting.
-> Microservices Service decomposition, communication patterns, and deployment.
-> Message Queues Async processing, event-driven architecture, and pub/sub patterns.
-> Service Mesh Istio, Linkerd, and service-to-service communication.