gRPC, Protobuf & Streaming RPC

IDL design, unary vs streaming RPC, HTTP/2 transport, deadlines/metadata, and status codes.

4/5Overview: 40m

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

TypePatternExample
Unary1 req → 1 respGetUser
Server streaming1 req → N respListLogs, stock ticks
Client streamingN req → 1 respupload chunks, aggregate
BidirectionalN ↔ Nchat, 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_EXISTS
  • DEADLINE_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

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 service

    Define `InventoryService` with unary `GetStock`, server-streaming `WatchStock`, and client-streaming `BulkUpdate`. Justify streaming mode per method.

    20m