7.1 · The protocol family at a glance
| Protocol | Transport | Schema | Best for |
|---|---|---|---|
| REST | HTTP/JSON | OpenAPI | Public APIs, broad client compatibility. |
| GraphQL | HTTP / WS | SDL | Many heterogeneous clients; flexible field selection. |
| gRPC | HTTP/2 + protobuf | .proto | Internal service-to-service, polyglot. |
| Connect / ConnectRPC | HTTP/JSON or gRPC | .proto | gRPC's ergonomics with browser-friendly transport. |
| tRPC | HTTP/JSON | TS types | Full-stack TS monorepo; instant typed RPC. |
| JSON-RPC | HTTP / WS | JSON Schema | Crypto, MCP, lightweight RPC. |
| WebSocket | WS | your choice | Bidirectional real-time. |
| SSE | HTTP | — | Server-to-client streaming (LLM tokens, notifications). |
| Webhooks | HTTP | JSON Schema | Server-to-server async events. |
7.2 · REST — done well
- Resources as nouns:
/orders/{id}/refunds— not RPC-ish verbs. - Verbs: GET (safe), POST (create), PUT (replace), PATCH (partial), DELETE.
- Status codes: 2xx success, 3xx redirect, 4xx client error, 5xx server. Use 409 / 422 thoughtfully.
- Pagination: cursor (
?after=…) beats offset at scale. - Filtering:
?status=paid&created_at[gte]=…— document and validate. - 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 — fields on demand
- 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) for multi-team graphs; persisted queries for security + caching.
7.4 · gRPC & Connect
- Binary protobuf over HTTP/2; bi-directional streaming; auto-generated clients in 11 languages.
- Best for internal service mesh.
- Connect / ConnectRPC (Buf) — same .proto, but speaks HTTP/JSON too so browsers and curl work.
- Pair with Buf for schema management, breaking-change detection, lint.
7.5 · tRPC
Inside a TypeScript monorepo, define a router on the server; the client gets typed function calls — no schema duplication, no client gen. Powerful for full-stack TS; useless outside it.
7.6 · WebSockets & SSE
| WebSocket | SSE (EventSource) | |
|---|---|---|
| Direction | Full-duplex | Server → client only |
| Transport | WS upgrade | Plain HTTP |
| Reconnect | You implement | Built-in |
| Browser support | Universal | Universal (incl. mobile) |
| Sweet spot | Chat, collaboration, multiplayer | LLM streaming, notifications, progress |
Rule of thumb: if the client doesn't need to send much, SSE is simpler and survives proxies better.
7.7 · Webhooks — done right
- Sign payloads (HMAC) and document the verification.
- Retry with exponential backoff; expose a delivery log + replay tool.
- Idempotent handlers — receivers will get duplicates.
- Provide event types, versioning, and a sandbox.
- WebSub / WebTransport are real but niche — webhooks remain the default.
7.8 · 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 end-to-end.
- Auth — OAuth 2.1 / OIDC + scopes; never hand-rolled tokens.
- Errors with stable codes; never raw stack traces.
- Backwards compatibility by default; deprecation policy in writing.
7.9 · Versioning & deprecation
- URI versioning (
/v1/...) — explicit; easy to route. - Header versioning (
Accept: application/vnd.acme.v2+json) — cleaner URLs, harder for clients. - Date versioning (Stripe-style) — clients pin a date, you ship breaking changes monthly.
- Deprecate via
Sunset+Deprecationheaders; communicate; provide a migration window.
7.10 · Self-check
Q1 · You stream LLM tokens from server → browser. Best protocol?
WebSocket
SSE
gRPC
REST long-poll