Mental model
gRPC is a high-performance Remote Procedure Call (RPC) framework that uses Protocol Buffers (compact binary serialization) over HTTP/2 (multiplexed streams over a single TCP connection).
Theory
- Protocol Buffers: Strongly typed binary IDL (Interface Definition Language). Serializes data up to 10x faster and 5x smaller than JSON.
- HTTP/2 Transport: Supports binary framing, header compression (HPACK), and concurrent stream multiplexing over a single TCP socket.
Alternatives and trade-offs
- REST / JSON over HTTP/1.1: Human-readable, universal browser support; text parsing overhead, HTTP/1.1 head-of-line blocking.
- gRPC / Protobuf over HTTP/2: Ultra-fast binary serialization, bi-directional streaming, strict schema contracts; requires gRPC-Web proxies for direct browser clients.
Failure modes and misconceptions
- Changing Field Tag Numbers: In Protobuf, changing field tag numbers (
string email = 2tostring email = 3) breaks backward and forward compatibility for deployed clients. Field names can change, but tag numbers MUST remain immutable. - L4 Load Balancer Connection Sticking: Classic Layer 4 TCP load balancers route an entire HTTP/2 TCP connection to a single backend pod, causing unbalanced server load. Use Layer 7 gRPC load balancing (Envoy / Traefik).
Decision scenario
Use gRPC with Protocol Buffers for internal microservice-to-microservice communication requiring low latency, strict schema validation, and high throughput.
Learning outcomes
- Compile Protocol Buffer (
.proto) schemas into strongly typed client and server stubs. - Leverage HTTP/2 stream multiplexing for concurrent RPC invocations.
- Maintain backward compatibility when updating Protobuf message fields.
Trade-offs
gRPC delivers massive throughput and serialization efficiency for internal microservices, but requires gRPC-Web translation layers for browser clients.