High-Performance RPC: Microservices with gRPC & Protocol Buffers
이 글의 핵심
Schema-first RPC over HTTP/2: protobuf codegen, streaming, deadlines, and ops-ready patterns.
Introduction: service-to-service bottlenecks
gRPC runs over HTTP/2 with compact Protocol Buffers payloads—strong schemas, efficient binary encoding, and first-class streaming.
Covers: .proto basics, protoc codegen, synchronous server/client sketches, streaming modes, errors/metadata, performance tips, production checklist.
Table of contents
- Protocol Buffers basics
- gRPC C++ server & client
- Streaming
- Complete examples
- Common errors
- Performance
- Production patterns
- Checklist
- Summary
1. Protocol Buffers
Define messages/services in .proto, run protoc with grpc_cpp_plugin to generate *.pb.{h,cc} and *.grpc.pb.{h,cc}. Never reuse field numbers for different meanings—compatibility hinges on field IDs.
syntax = "proto3";
package myapp;
message UserRequest { int32 user_id = 1; string name = 2; }
message UserResponse { int32 user_id = 1; string name = 2; int64 created_at = 3; }
service UserService {
rpc GetUser(UserRequest) returns (UserResponse);
}
2. gRPC C++
ServerBuilder + service implementation; client uses CreateChannel + stub. Always check status.ok() and set deadlines on ClientContext.
3. Streaming
Server streaming, client streaming, bidi—choose based on data direction; handle Write failures and IsCancelled() on long RPCs.
4. Complete examples
Add metadata/auth snippets, retry with backoff, and TLS credential setup in production services—the patterns are standard gRPC client configuration.
5. Common errors
UNAVAILABLE/Connection refused → server down or wrong address; DEADLINE_EXCEEDED → increase deadline or optimize server; RESOURCE_EXHAUSTED → raise message size limits via ChannelArguments.
6. Performance
Reuse channels/stubs; prefer bytes over giant JSON strings inside protobuf; reserve repeated fields; HTTP/2 multiplexes many RPCs per connection.
7. Production patterns
Health checks, metadata propagation (x-request-id), graceful Shutdown(), TLS for untrusted networks, schema evolution policy.
8. Checklist
- Deadlines on clients
- Status codes meaningful
- TLS where required
- Stream cancellation handling
9. Summary
Protobuf + gRPC gives typed, fast service RPC with streaming; operate it with observability and clear compatibility rules.
Next: Secure coding & OpenSSL (#43-2)
Previous: Linux syscalls (#42-3)
Keywords
gRPC, Protocol Buffers, C++ microservices, HTTP/2 RPC, protobuf codegen