gRPC
Protocol buffers, streaming types, interceptors, service reflection
gRPC (Google Remote Procedure Call) is a high-performance RPC framework using Protocol Buffers (protobuf) for binary serialization and HTTP/2 for transport, achieving 5–10x better throughput and 3–5x lower latency than JSON/REST for equivalent payloads. Protobuf encoding is 3–10x more compact than JSON, and HTTP/2 multiplexing allows multiple RPC calls to share a single TCP connection. gRPC is the dominant choice for inter-service communication in polyglot microservices environments.
Key Points
- Protobuf schema (.proto file): field numbers are the wire format identity (changing a field number is a breaking change); adding new fields with new numbers is backward-compatible (old clients ignore unknown fields).
- Four streaming types: Unary (one request, one response), Server Streaming (one request, stream of responses), Client Streaming (stream of requests, one response), Bidirectional Streaming (full-duplex streams).
- Interceptors (equivalent to HTTP middleware): add authentication (JWT validation), logging, tracing (OpenTelemetry), retry logic, and rate limiting at the gRPC layer — chain multiple interceptors per channel.
- gRPC-Web: gRPC cannot be called directly from browsers (lacks HTTP/2 trailer support); gRPC-Web uses HTTP/1.1 with a proxy (Envoy) that translates between browser-compatible and gRPC protocols.
- gRPC status codes: OK (0), CANCELLED (1), UNKNOWN (2), INVALID_ARGUMENT (3), NOT_FOUND (5), ALREADY_EXISTS (6), PERMISSION_DENIED (7), RESOURCE_EXHAUSTED (8 = rate-limited), UNAVAILABLE (14 = retry-safe).
- Deadlines: gRPC supports per-call deadlines (Context with timeout); if the deadline expires, all in-progress work is cancelled — prevents cascading timeouts by propagating deadline through call chain.
- Reflection: grpc_reflection package exposes all service descriptors at runtime — enables gRPC UI tools (gRPCurl, Postman, BloomRPC) to discover and invoke services without the .proto file.
- Protobuf vs alternatives: Protobuf is binary and type-safe; Avro (schema in payload or registry); Thrift (Facebook); FlatBuffers (zero-copy, no parsing for read-heavy use cases like game engines).
Real-World Example
Google uses Protocol Buffers and gRPC for virtually all internal service communication — the internal Stubby RPC system was the inspiration for gRPC. Netflix uses gRPC for all microservice-to-microservice communication within their streaming backend, citing predictable latency and strongly-typed contracts as key advantages over REST. Uber uses gRPC with TChannel (their own RPC), now migrating to gRPC + Envoy.