7.1 · Protocol family at a glance
| Protocol | Transport | Schema | Best for |
|---|---|---|---|
| REST | HTTP / JSON | OpenAPI 3.1 | Public APIs; broad clients. |
| GraphQL | HTTP / WS | SDL | Many heterogeneous clients; flexible fields. |
| gRPC | HTTP/2 + protobuf | .proto | Internal service-to-service, polyglot. |
| Connect / ConnectRPC | HTTP/JSON or gRPC | .proto | gRPC ergonomics, browser-friendly. |
| tRPC | HTTP/JSON | TS types | Full-stack TS monorepo. |
| JSON-RPC 2.0 | HTTP / WS / stdio | JSON Schema | Crypto, MCP, lightweight RPC. |
| WebSocket | WS | your choice | Bi-directional real-time. |
| SSE | HTTP | — | Server-to-client streams (LLM tokens, notifications). |
| WebTransport | QUIC | your choice | Modern WS replacement; streams + datagrams. |
| Webhooks | HTTP | JSON Schema | Server-to-server async events. |
| MCP | JSON-RPC over stdio/SSE | JSON Schema | Model ↔ tool / resource integration (AI agents). |
7.2 · REST done well
- Resources as nouns:
/orders/{id}/refunds. - Verbs: GET (safe), POST (create), PUT (replace), PATCH (partial), DELETE.
- Status codes: 2xx / 3xx / 4xx / 5xx — use 409, 422 thoughtfully.
- Pagination: cursor (
?after=…) beats offset at scale. - Errors: stable shape (
{ error: { code, message, request_id } }). - OpenAPI 3.1 spec is the source of truth — generate clients + docs from it.
7.3 · GraphQL
- Schema-first; clients request only what they need.
- Solves "N round-trips per screen" for mobile.
- Watch out: query cost, N+1 (use DataLoader), caching is harder than REST.
- 2026 trends: federation (Apollo, Hive), persisted queries for security + caching.
7.4 · gRPC & Connect
- Binary protobuf over HTTP/2; bidirectional streaming; auto-generated clients in 11 languages.
- Best for internal service mesh.
- Connect / ConnectRPC (Buf): same .proto but also speaks HTTP/JSON — browsers, curl, gRPC all work.
- Buf for schema management, breaking-change detection, lint.
7.5 · tRPC
Inside a TypeScript monorepo, define a router on the server; client gets typed function calls — no schema duplication, no client generation. Powerful in TS; not portable.
7.6 · JSON-RPC & MCP
- JSON-RPC 2.0 — minimal RPC over any transport; used by Ethereum, MCP, Language Server Protocol.
- MCP (Model Context Protocol) — JSON-RPC framing for exposing tools, resources, prompts to LLM hosts. Universal connector for AI agents (see STACK Ch 19, AI Codex Ch 11).
7.7 · WebSocket vs SSE vs WebTransport
| WebSocket | SSE | WebTransport | |
|---|---|---|---|
| Direction | Full-duplex | Server → client | Full-duplex |
| Transport | TCP via WS upgrade | HTTP/1+ | QUIC |
| Reconnect | You implement | Built-in | You implement |
| Streams + datagrams | Streams only | One stream | Both |
| Sweet spot | Chat, collab, multiplayer | LLM streaming, notifications | Low-latency gaming, modern WS-replacement |
7.8 · Webhooks done right
- Sign payloads (HMAC) + document verification.
- Retry with exponential backoff; expose delivery log + replay tool.
- Idempotent handlers — receivers will get duplicates.
- Provide event types, versioning, sandbox.
7.9 · API design hygiene — the non-negotiables
- Idempotency on every write (Idempotency-Key header).
- Pagination — never return unbounded collections.
- Rate limits + clear 429 +
Retry-After. - Tracing headers (
traceparent) propagated. - Auth — OAuth 2.1 / OIDC + scopes (see Ch 08).
- Errors with stable codes; never raw stacks.
- Backwards compatibility by default; deprecation policy in writing.
- Bulk endpoints for high-volume clients.
- Field masks / sparse fieldsets.
- Long polls / async ops for slow work — return a job ID + status URL.
7.10 · Versioning & deprecation
- URI versioning (
/v1/...) — explicit; easy to route. - Header versioning (
Accept: application/vnd.acme.v2+json) — clean URLs. - Date versioning (Stripe-style) — clients pin a date; you ship breaking changes monthly.
- Deprecate via
Sunset+Deprecationheaders; provide a migration window.
7.11 · Schema-first toolchain
| Format | Generates |
|---|---|
| OpenAPI 3.1 | Client SDKs, docs, mock servers, contract tests. |
| AsyncAPI | Same idea for event-driven (Kafka, MQTT). |
| protobuf | gRPC / Connect clients in any language. |
| JSON Schema | Request/response validation, generation. |
| GraphQL SDL | Codegen via GraphQL Code Generator. |
7.12 · Self-check
Q1 · You stream LLM tokens from server to browser. Default protocol?
WebSocket
SSE
gRPC streaming
REST long-poll