The BackendDeeper

Kayavak / The Backend / BE·45 HTTP API Design / Deeper

Deeper · The Backend

HTTP API Design

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

In the late 1990s programs talked over the network through remote procedure calls dressed up as objects (CORBA, Java RMI) and then SOAP (1998–2000), XML messages sent to one endpoint with heavy tooling. In 2000 Roy Fielding, one of HTTP's authors, described in his doctoral dissertation the architectural style the web itself followed: REST. Resources with addresses, a small uniform set of methods, stateless requests and cacheable responses. The web had scaled to the whole world on those constraints, so APIs built the same way inherited caching, proxies and simplicity.

APIs labelled "REST" rarely follow all of Fielding's constraints, but the core ideas stuck. Other styles answered other needs: GraphQL (Facebook, released 2015) lets clients choose exactly the fields they need; gRPC (Google, 2015) offers typed, efficient calls between services. OpenAPI (from Swagger, 2010–11, renamed in 2016) made HTTP APIs describable by machines.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

An API is a promise to clients you don't control and can't update. Design it around resources and the meanings HTTP already gives methods and status codes, and change it only in ways old clients survive.

Question Answer
What is it? The contract between a service and its clients: resources, methods, representations, errors, versions
Problem it solves Letting independently developed clients use a service correctly, now and after it changes
Parts Resources and URLs, method semantics (safe, idempotent), status codes, errors (Problem Details), pagination, caching, versioning
How it works Clients send standard requests; HTTP's semantics tell clients, proxies and caches what's safe to retry or reuse
Assumptions Methods mean what HTTP says (GET changes nothing; PUT is idempotent)
When they change A GET that changes data gets repeated by prefetchers and caches; a non-idempotent POST retried by a client charges twice
Where it fails Breaking changes to fields clients depend on; offset pagination over changing data
Related ideas HTTP (The Web Frontend, ch19); idempotency in messaging (ch51); resilience and retries (ch52)

What if?

What if a client times out on "create payment" and retries?

Reason it through, then open

The first request may have succeeded; the client just didn't hear back. Without protection, the retry creates a second payment. An idempotency key (a client-generated id sent with the request, stored with the result) lets the server recognise the retry and return the original response. It turns an unsafe POST into a safely retryable one.

What if you paginate with ?page=2 over data that's changing?

Reason it through, then open

When rows are inserted or deleted between requests, items shift between pages: clients see duplicates or skip items, and deep pages get slower because the database still counts past every earlier row. Keyset (cursor) pagination asks for "items after this id", which is stable under inserts and uses an index.

Where the boundary is

Aspect Safe Idempotent
Means Doesn't change server state Doing it twice has the same effect as once
GET, HEAD Yes Yes
PUT, DELETE No Yes
POST No No (unless you make it so, e.g. with an idempotency key)

How sure are we?

Claim How it's known Where from
REST was defined in Fielding's 2000 dissertation Historical Architectural Styles and the Design of Network-based Software Architectures
Method safety and idempotency Convention HTTP Semantics, RFC 9110
Most "REST" APIs don't meet all of REST's constraints Model Especially hypermedia; widely observed, Fielding has said so himself

Still open

Whether GraphQL, REST or RPC suits a given API is mostly a matter of clients and organisation, and arguments continue. Versioning strategies (URL, header, never break) also remain a matter of taste and context.

What would change your mind? If your API had one client that you deploy together with the server, most of the compatibility discipline here would be optional. Public APIs never have that luxury.

Recall

What's the difference between safe and idempotent?

Safe changes nothing; idempotent means repeating has the same effect as doing it once.

How does an idempotency key make retries safe?

The server stores the result under the key and returns it for repeats instead of acting again.

Why prefer keyset pagination for changing data?

It's stable under inserts and deletes and uses an index instead of counting past earlier rows.

Where was REST defined?

In Roy Fielding's 2000 doctoral dissertation.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.