gRPC & Protocol Buffers
High-performance RPC framework using Protocol Buffers — unary and streaming RPCs, HTTP/2 multiplexing, schema evolution, service mesh integration, and REST transcoding.
Overview
RPC over HTTP/2 with a strict schema
gRPC is an open-source, high-performance Remote Procedure Call framework developed by Google. It uses HTTP/2 as the transport and Protocol Buffers (Protobuf) as the interface definition language and wire format.
Why gRPC over REST
- Transport — HTTP/1.1 — HTTP/2: multiplexing, header compression, binary framing
- Payload — Text-based JSON — Binary Protobuf
- Schema — Not enforced — Strict, forward and backward compatible
- Performance — Baseline — Typically 5-10x faster and more bandwidth-efficient for the same payload
Protocol Buffers
Service contracts live in .proto files. The protoc compiler generates strongly typed client and server code in any supported language (Go, Java, Python, Node.js, C++).
> [!TIP] Protobuf encoding is compact: a JSON object is roughly 3x larger than the equivalent Protobuf message.
The four call types
- Unary — Single request, single response; like a REST call — The most common pattern
- Server-side streaming — One request, a stream of responses — Real-time data feeds, large result sets
- Client-side streaming — A stream of requests, one response — File uploads, batch input
- Bidirectional streaming — Both sides stream at once over one HTTP/2 connection — Real-time collaboration, interactive protocols
Schema evolution
- Add a field with a new number — Yes: backward and forward compatible
- Change a field's number or type — Never
- Remove a field — Mark its number reserved and never reuse it
> [!WARNING] Field numbers are the wire contract: reusing one silently corrupts data for old clients.
gRPC in Kubernetes
A layer-4 load balancer does not understand HTTP/2 stream multiplexing. It balances connections, so every request on one long-lived connection lands on the same backend.
> [!IMPORTANT] Use a service mesh (Istio, Linkerd) or a gRPC-aware load balancer (NGINX, Envoy, AWS ALB) that terminates HTTP/2 and balances per request.
gRPC-Gateway
gRPC-Gateway generates a reverse proxy from Protobuf annotations that translates HTTP/JSON REST calls to gRPC. One backend serves both REST (for browsers) and gRPC (for internal services) from the same .proto definition.
Key concepts
- Protocol Buffers (Protobuf)
- Binary serialization format and IDL. Define message and service schemas in .proto files. protoc compiles them to language-specific stubs. Binary encoding is 3-10x smaller than equivalent JSON for typical API payloads.
- HTTP/2 Transport
- gRPC uses HTTP/2 which provides multiplexing (multiple requests over one TCP connection), header compression (HPACK), binary framing, and server push. This eliminates HTTP/1.1 head-of-line blocking and connection overhead.
- Bidirectional Streaming
- Both client and server send a stream of messages over a single long-lived HTTP/2 connection. Used for real-time applications, collaborative features, and interactive protocols where latency matters.
- Field Numbers
- Every Protobuf field has a unique integer tag encoded in the binary wire format (not the field name). Never change a field number once published — it breaks backward compatibility. Adding new fields with new numbers is safe.
- gRPC-Gateway
- Protobuf plugin that generates an HTTP/JSON reverse proxy from .proto annotations. Allows a single gRPC service to serve REST clients (browsers) and gRPC clients (microservices) from one codebase.
- gRPC Load Balancing
- HTTP/2 multiplexing means all streams go to one backend with L4 load balancing. Use L7 load balancers (Envoy, NGINX, AWS ALB with gRPC protocol) or client-side load balancing with a headless Kubernetes service for proper request distribution.
When to use it
- Designing high-throughput microservice-to-microservice communication
- Interview questions about inter-service communication protocols and trade-offs
- Replacing REST APIs for internal services where performance and schema safety matter
- Building streaming APIs for real-time data feeds or bidirectional communication
- Enforcing contract-first API development with generated client and server stubs
Sign up free to read the full topic
More DevOps topics
Start free on Cariara · Coding problems · System design studio