Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 2 · Lesson 16 · 4 min read

gRPC and Protocol Buffers in System Design

On this page
  1. Thousands of internal calls per second
  2. Defining a gRPC service in a .proto file
  3. Why Protocol Buffers are smaller and faster than JSON
  4. The four gRPC call types, including streaming
  5. gRPC drawbacks
  6. gRPC load balancing on Kubernetes
  7. When to use gRPC vs REST
  8. Check yourself

Thousands of internal calls per second

An order service checks stock with an inventory service several thousand times a second, all inside one data center. Over REST with JSON, each call spends noticeable CPU turning objects into text and back, and the field names travel in every message. Nobody outside the company ever sees this API. It's the textbook case for gRPC.

Defining a gRPC service in a .proto file

With gRPC you define the service and its messages in a .proto file:

Protobuf
syntax = "proto3";
package inventory;

service Inventory {
  rpc CheckStock (StockRequest) returns (StockReply);
  rpc WatchStock (StockRequest) returns (stream StockReply);   // server streaming
}

message StockRequest { string sku = 1; int32 warehouse_id = 2; }

message StockReply {
  string sku = 1;
  int32 warehouse_id = 2;
  int32 available = 3;
  int32 reserved = 4;
  int64 updated_at_unix = 5;
}

A code generator turns this into typed client and server code for C#, Go, Java, Python, TypeScript and more. Calling the service looks like calling a local method, and a renamed field is a compile error rather than a production bug.

Why Protocol Buffers are smaller and faster than JSON

Messages are encoded as Protocol Buffers (protobuf): binary, with field numbers instead of field names, and small integers packed into fewer bytes. Encoding the StockReply above:

YAML
protobuf: 26 bytes
JSON:     98 bytes  {"sku":"HDPH-77-BLK","warehouse_id":12,"available":348,"reserved":17,"updated_at_unix":1791608337}

Parsing binary is also cheaper than parsing text. gRPC runs over HTTP/2 (Day 13), so many calls share one connection with multiplexing.

gRPC uses a typed .proto contract and binary Protocol Buffers over HTTP/2, giving smaller and faster messages than JSON over REST.
gRPC uses a typed .proto contract and binary Protocol Buffers over HTTP/2, giving smaller and faster messages than JSON over REST.

The four gRPC call types, including streaming

StyleShapeExample
UnaryOne request, one responseCheckStock
Server streamingOne request, a stream of responsesLive stock updates for a product page
Client streamingA stream of requests, one responseUploading a batch of sensor readings
BidirectionalBoth sides streamChat or collaborative editing between services

gRPC drawbacks

  • Browsers. Browsers can't speak native gRPC. You need gRPC-Web with a proxy, or a REST/JSON gateway in front.
  • Readability. You can't inspect a call with curl or read it in a log without tooling such as grpcurl.
  • Careless contract changes. Field numbers are the contract. Never reuse or renumber one; add new fields with new numbers so old clients keep working. When you remove a field, mark its number reserved 4; so nobody reuses it by accident. Also, a proto3 field set to 0 looks the same as a missing one unless you declare it optional.

gRPC load balancing on Kubernetes

gRPC keeps one long-lived HTTP/2 connection per client and sends every call over it. A layer 4 load balancer (Day 19) balances connections, not calls, so it picks a server once and every call from that client lands there forever. Add servers and they sit idle while the original ones stay hot. A Kubernetes ClusterIP service is layer 4, so this is the default behavior there.

Fix it with a layer 7 proxy that balances individual calls (Envoy, Linkerd, or a load balancer with gRPC support), or with client-side balancing, where the gRPC client resolves all server addresses and spreads calls itself.

gRPC also has deadlines built in: the caller sets how long it will wait, and the deadline travels with the call to every downstream service. Set one on every call (Day 75).

When to use gRPC vs REST

Most teams land on the same rule: gRPC for internal, high-volume, service-to-service calls; REST or GraphQL at the edge for browsers, mobile apps and partners. Google built gRPC from its internal RPC system, Stubby, and the pattern of a typed contract between internal services is the part worth copying. ASP.NET Core, Go and Java all have first-class gRPC support.

Check yourself

Each client holds one long-lived HTTP/2 connection, and the layer 4 service balanced connections once. Use a layer 7 proxy or client-side load balancing so individual calls are spread.

Unary (one request, one response), server streaming (one request, many responses), client streaming (many requests, one response) and bidirectional streaming (both sides send freely). Server streaming suits pushing live price updates to a caller; client streaming suits uploading many readings that the server summarizes.

Browsers can't call native gRPC, and partners expect readable, cacheable HTTP APIs. You'd need a gRPC-Web proxy or a translation layer anyway.

Fields are encoded as compact binary values identified by numbers instead of repeated text names, and there's no text parsing of numbers or strings. The schema is known in advance, so both sides can serialize and parse with generated code.

Add new fields with new numbers, never reuse or renumber existing fields, and mark removed fields as reserved. Old clients ignore unknown fields and new clients see defaults for missing ones, so both versions keep working.