Services

Resources

Company

gRPC

gRPC lets one program call a method on another program as if it were a local function. You describe the service in a .proto file, generate client and server code for your languages, and the generated stub handles serialization, the network call and errors. Messages are Protocol Buffers (protobuf), a compact binary format, and every call travels over HTTP/2.

Terraform uses it at its core: when you run terraform plan, Terraform starts each provider as a separate binary and talks to it over gRPC. That is why a provider can be written in any language that speaks the protocol.

How it works

A service definition looks like rpc Reserve(ReserveRequest) returns (ReserveReply);. Each message field has a number, such as string sku = 1;, and the wire format carries that number instead of the field name. Old clients skip numbers they do not know, so you can add fields without breaking them. That is the basis of backward compatibility in protobuf, and also why you must never reuse a field number after deleting a field.

A method can be unary (one request, one reply), server streaming, client streaming or bidirectional streaming, where both sides send messages on one open stream. Every call can carry a deadline. If the client sets 300 ms and the server has not answered by then, the client gets status DEADLINE_EXCEEDED (code 4). The server sees the call cancelled, so it can stop working on it. Most gRPC libraries set no deadline by default, and a call to a hung server waits forever.

Browsers cannot speak plain gRPC, because page JavaScript has no control over HTTP/2 frames and cannot read HTTP trailers, where gRPC puts the status code. gRPC-Web changes the encoding slightly, and a proxy such as Envoy translates between the browser and the gRPC backend. Public APIs for browsers and third parties are usually a REST API described with an OpenAPI specification; gRPC is more common between internal services.

Why it matters in production

HTTP/2 multiplexes many calls over one long-lived TCP connection, and that changes load balancing. A Kubernetes ClusterIP Service balances connections, so it picks a backend pod once per connection.

Say an orders service runs 2 replicas, each sending 1,000 requests a second to an inventory service with 4 pods. Each replica opens one connection at startup, and the two connections land on pods 1 and 2. Those pods take 1,000 requests a second each, and pods 3 and 4 get none. Scaling inventory to 8 pods adds 6 idle pods.

The fix is to balance per request: use a headless Service with the client's round_robin policy, or a service mesh such as Istio. With 4 pods, each then gets 2,000 / 4 = 500 requests a second.

See also

On the One2N blog

References

  1. gRPC: Core concepts, architecture and lifecycle

  2. Protocol Buffers: Language guide (proto 3)

Network diagram of gRPC load balancing. Two orders replicas each send 1,000 requests per second over a single long-lived HTTP/2 connection. Those two connections land on inventory pods 1 and 2, which take 1,000 per second each, while pods 3 and 4, outlined in red, get 0. Below, the fix: balance per request, so 2,000 ÷ 4 = 500 per second per pod.