Why gRPC for internal services
- HTTP/2 multiplexing (see Networking) — many RPCs on one connection
- Protobuf — compact, fast encode/decode, schema in
.proto - Codegen — stubs in 10+ languages from one IDL
- First-class streaming — not bolted on like chunked HTTP
- Deadlines/cancellation propagated in metadata
Default at Google, widely adopted in K8s ecosystems (etcd, Kubernetes API).
Four RPC types
| Type | Pattern | Example |
|---|---|---|
| Unary | 1 req → 1 resp | GetUser |
| Server streaming | 1 req → N resp | ListLogs, stock ticks |
| Client streaming | N req → 1 resp | upload chunks, aggregate |
| Bidirectional | N ↔ N | chat, collaborative edit |
Streaming uses HTTP/2 DATA frames; flow control per stream.
Protobuf essentials
message Stock {
string sku = 1;
int32 quantity = 2;
google.protobuf.Timestamp updated_at = 3;
}
service Inventory {
rpc GetStock(GetStockRequest) returns (Stock);
rpc WatchStock(WatchRequest) returns (stream Stock);
}- Field numbers never reuse
optional/repeated/oneof/map<>- Well-known types — Timestamp, Struct, Any
- Package versioning:
package inventory.v2;
gRPC status codes
Maps to google.rpc.Status — not HTTP status (though HTTP/2 carries them):
OK,INVALID_ARGUMENT,NOT_FOUND,ALREADY_EXISTSDEADLINE_EXCEEDED,UNAVAILABLE,RESOURCE_EXHAUSTED- Rich errors: grpc-status-details-bin with
ErrorInfo,RetryInfo
Metadata
Headers for auth (authorization), tracing (traceparent), routing, custom baggage. Binary metadata for encoded tokens.
Deadlines
Client sets deadline — server should abort work when exceeded. Propagate to downstream calls. Critical for preventing thread pile-up.
Load balancing
L7 gRPC-aware LB required — connection reuse means naive round-robin on TCP connections skews load. xDS, lookaside, or proxyless gRPC LB. Underlying HTTP/2 multiplexing is Networking; pool sizing on the server is Concurrency → Thread Pools.
vs REST
gRPC: performance, typing, streaming, internal mesh. REST: browsers, CDNs, public APIs, curl debugging.
Bridge: gRPC-Gateway generates REST from proto; Envoy translates gRPC-Web.
Further Reading
- gRPC Documentation — Core concepts and RPC typesReference30m
- Google Protocol Buffers — Style GuideReference20m
- gRPC — Deadlines and cancellationReference15m
Hands-On Tasks (Optional)
API design drills and whiteboard exercises — protocol selection, contract design, and bulk-transfer architecture. Assumes Networking and sibling tracks on the hub page (Distributed Systems, Databases, Concurrency, LLD).
- Design a protobuf service20m
Define `InventoryService` with unary `GetStock`, server-streaming `WatchStock`, and client-streaming `BulkUpdate`. Justify streaming mode per method.