Topic 033 / Core
gRPC
Chapter 3.4 · API Styles and Serialization
What and why
REST (→ 31) and GraphQL (→ 32) are built for client-facing APIs over plain HTTP and JSON; internal service-to-service calls instead want low latency, a strict typed contract, and streaming support, without JSON's parsing and verbosity cost. gRPC is an RPC framework running over HTTP/2 (→ 28) that uses Protocol Buffers for a compact binary wire format and a strongly typed service contract shared by client and server. Builds on: → 28.
Diagram
| REST / JSON | gRPC / Protobuf | |
|---|---|---|
| Wire format | text, self-describing | compact binary, schema-driven |
| Typing | documented, not enforced | enforced by generated code |
| Streaming | request/response only | request, response, or bidirectional streams |
| Ad hoc inspection | curl and read |
needs the schema to decode |
How it works
- The service and its message types are defined once in a contract file.
- Client and server code are generated from that contract in each language used.
- The client calls a generated method as if it were local; it's serialized to Protobuf and sent as an HTTP/2 stream.
- The server deserializes the call, runs the handler, and serializes the typed response back the same way.
- HTTP/2 streams let a call be request-streaming, response-streaming, or bidirectional, beyond a single request/response pair.
Practical example
(illustrative). An internal "get user" call where the typed contract makes a renamed or removed field a build-time error in both services, rather than something discovered only when a caller's JSON parsing breaks at runtime.
When to use it
- Use when: calls are internal, service-to-service, and need low latency, a strict contract, or streaming.
- Avoid when: the API faces browsers or third-party integrators directly — browser support for gRPC is limited, and REST/JSON needs no generated client to be usable with a plain HTTP tool.
- Failure scenarios: the generated-code toolchain adds a build step; a service that changes its contract without regenerating the other side's stubs drifts silently until a call breaks; binary payloads aren't inspectable with a plain HTTP client the way JSON is.
- Simpler alternative: REST/JSON internally too, for a small team where the generated- code tooling isn't yet paying for itself.
Practice
(pattern kind — implement minimal; 20–30 min, no lab services). Define a tiny service contract with one RPC method, generate client and server stubs, and call it locally. Then rename a field on only one side and observe exactly where the contract break surfaces — at code generation, at compile time, or at decode time. Done when you can state exactly where the break surfaced and why there.
Key takeaway
gRPC trades JSON's readability and browser-friendliness for a compact, strictly typed, streaming-capable contract — the right trade for internal calls, the wrong one for a public browser-facing API. Done when you can: say which of REST, GraphQL, or gRPC fits a given caller and why. Used next in → 34 (Protocol Buffers' wire format generalizes into serialization and schema evolution).
References
None — this topic makes no external claims.
Source directory