Communication in system design, part 1: a message's journey, Protobuf, REST or gRPC
Five things to weigh when components talk, a message's path through serialization, protocol, handling pattern and network I/O, the Reactor pattern (not the Actor model), HTTP/2, JSON vs Protobuf and its compatibility rules, REST vs gRPC between backends, and the gRPC load-balancing trap on Kubernetes.
On this page
1. The problem
A pricing service moves to gRPC to handle 30,000 RPS. Scaling from 3 to 10 pods doesn't lower p99: the 7 new pods get almost no requests. The same month, a "rename fields for clarity" PR makes old apps read phone numbers into the email field.
2. Key idea
How components talk is the hardest architecture decision to change after the database. Ask five questions before picking a technology: what if a message is lost or duplicated, can old clients and new servers coexist, how fast must it be, how are errors reported, how is it encrypted and authenticated.
| Choice | Use when | Don't use when |
|---|---|---|
| REST + JSON | public APIs, partners, mobile, most internal services | internal paths at tens of thousands of RPS with small messages |
| REST over HTTP/2 | you want multiplexing and header compression without a new programming model | old load balancers don't speak HTTP/2 |
| gRPC + Protobuf | high-load internal calls, streaming, polyglot teams needing strict contracts | browsers calling directly (needs gRPC-Web and a proxy) |
2.1 How does Protobuf stay compatible?
On the wire, Protobuf fields are identified by number, not by name.
- Adding a field with a new number: safe, old readers skip unknown fields.
- Changing the number or the type: breaks, often silently.
- Removing a field: mark both number and name
reservedso nobody reuses them. - Renaming while keeping the number: binary still works, Protobuf's JSON mapping breaks.
2.2 Why does gRPC pile onto one pod?
A ClusterIP Service balances at layer 4, per connection. gRPC multiplexes every request over one long-lived HTTP/2 connection, so each client sticks to its pod. Fixes: client-side balancing (headless Service + round_robin), an L7 proxy or service mesh (Envoy, Linkerd), or capping connection age (MAX_CONNECTION_AGE).
3. In Java
gRPC propagates a deadline (an absolute cut-off) across the whole call chain, so downstream services give up on their own when time runs out.
try {
OrderReply reply = orderStub
.withDeadlineAfter(300, TimeUnit.MILLISECONDS) // the whole chain shares this deadline
.getOrder(OrderRequest.newBuilder().setId(id).build());
return reply;
} catch (StatusRuntimeException e) {
switch (e.getStatus().getCode()) {
case DEADLINE_EXCEEDED, UNAVAILABLE -> throw new PricingUnavailableException(id, e); // transient
case NOT_FOUND -> throw new OrderNotFoundException(id); // bad input
default -> throw e;
}
}
// server hangs 1 s -> DEADLINE_EXCEEDED after ~300 msREST over HTTP/2 in Spring Boot: server.http2.enabled=true. Other languages: Go passes context.WithTimeout into the stub; Node.js @grpc/grpc-js has a deadline option.
A message's journey: what each layer handles
| Layer | Concern | Example choices |
|---|---|---|
| Logic | error handling | timeouts, retries, fallbacks |
| Serialization | compatibility, performance | JSON, Protobuf, Avro |
| Protocol | delivery guarantees, security | HTTP/1.1, HTTP/2, AMQP, Kafka, MQTT |
| Processing model | performance | thread-per-request, Reactor, virtual threads |
| Network I/O | efficiency | blocking, non-blocking, epoll |
- Reactor (Netty, Nginx, Redis): an event loop watches I/O on many connections while a worker pool computes. Actor (Akka, Erlang) is a programming model: each actor has a mailbox and handles one message at a time. They are different things.
- No retries is at-most-once (may lose); retries give at-least-once (may duplicate); to behave like exactly-once the receiver must be idempotent.
- HTTP/2 server push is essentially dead (Chrome removed it in 2022); for server-initiated data use SSE, WebSocket or gRPC streaming.
- The cost of gRPC: no curl (you need
grpcurl), only about 16 status codes, and old LBs or WAFs may not speak HTTP/2 to the backend.
4. Working with AI
Hand to AI: generating .proto files and stubs, clients with deadlines, compatibility tests between the old and new versions.
Check in AI-written code:
- No changed numbers or types on existing fields; removed fields are
reservedby number and name. - CI runs
buf breakingagainst the main branch. - Every gRPC call has a deadline; every REST call has connect and read timeouts.
- gRPC on Kubernetes balances per request, not just via
ClusterIP. - gRPC status codes are mapped: only
UNAVAILABLEandDEADLINE_EXCEEDEDare retried.
5. Exercises
Exercise 1: Review a .proto change
The AI describes the PR as "clearer names, adds phone, no client impact". Which changes break old clients? How do you fix them?
// before
message User { string id = 1; string name = 2; int32 age = 3; string email = 4; }
// after
message User { string id = 1; string full_name = 2; string age = 3; string phone = 4; }Solution to Exercise 1
name→full_namewith the same number 2: binary doesn't break, but anyone using the JSON mapping breaks, because the JSON field name changed.agefromint32tostring: breaks, the wire types differ.phonereusing number 4 fromemail: the worst break; old clients read phone numbers into email with no error at all.
Fix: keep age as int32 (add field 5 if you need a string); reserved 4; reserved "email"; then add phone = 6. Add buf breaking to CI so PRs like this are blocked automatically.
Exercise 2: Read an incident
The pricing service runs gRPC behind a ClusterIP Service. Scaling from 3 to 10 pods leaves p99 at 450 ms; the dashboard shows the 3 old pods at 85% CPU and the 7 new ones at 3%. Diagnose it, say what you do in the first 15 minutes, and fix the root cause.
Solution to Exercise 2
- Diagnosis: layer 4 balancing per connection; clients hold HTTP/2 connections to the 3 old pods and the new pods receive nothing.
- 15 minutes: rolling-restart the clients so they open new connections and spread the load.
- Root fix: a headless Service with client
round_robin, or a service mesh that balances per request; setMAX_CONNECTION_AGEon the server so clients reconnect periodically; alert when CPU differs widely between pods.
Summary
- Five considerations; performance isn't first.
- Protobuf: never change field numbers or types; reserve removed fields.
- REST for the outside and most services; gRPC for hot paths or streaming.
- Every gRPC call has a deadline.
- gRPC on Kubernetes needs per-request balancing.
Read more: Part 2: Async, real time and a fraud detection case study.
Practice interview questions on this topic
- What does HTTP/2 improve over HTTP/1.1, and what more does HTTP/3 solve?
- How does gRPC work, which call types does it have, and when should you use it?
- When would you choose gRPC or GraphQL over REST?
- A request crosses a chain of 4 synchronous services — how do you set timeouts so one slow dependency can't take everything down?
- How do L4 and L7 load balancers differ, and why do new pods get no traffic when you scale a gRPC service?
Comments
Community rules