Overview
Curated: · Written: · Reviewed:
API Design & REST Fundamentals
In one line
Model resources as nouns, let the HTTP method carry the verb as RFC 9110 defines it, keep client interaction state off the server, and make unsafe writes retry-safe with an idempotency key. Reach for it whenever an interviewer asks you to design an API.
Key definitions
- Safe: the client does not request a state change. GET and HEAD are safe, so caches and link prefetchers may call them freely.
- Idempotent: the end state is the same however many times the request is applied. The response may differ between calls.
- Idempotency key: a client-generated unique value per logical operation, stored with the request hash and response before side effects run.
- Problem details (RFC 9457): the standard error envelope.
typeis the stable machine-readable code clients branch on;detailis for humans. - Keyset (cursor) pagination: the next page is a WHERE on the last item's composite sort key, not an offset.
- Stateless: each request carries what the server needs. Any instance can serve any request.
How it works
- The method carries the verb. Caches, proxies and prefetchers rely on GET being safe; retry logic relies on idempotent methods.
- A GET that writes will eventually be triggered by a prefetcher, crawler or link scanner. That failure belongs to the API designer.
- POST is neither safe nor idempotent. A repeat with the same key returns the stored response with
Idempotent-Replay: true. The same key with a different body returns 422. - A timed-out POST is ambiguous: the client cannot tell whether the server acted. The key must be sent with the first attempt.
When to use and when not to
| Decision | Use when | Avoid when |
|---|---|---|
| Cursor pagination | Large or changing collections, deep scrolling | Users need random access such as jump to page 7 |
| Offset pagination | Small, static, admin-facing lists | Concurrent inserts (rows are skipped or repeated) |
| Path versioning (/v2/orders) | Breaking changes; caches and logs must separate versions | You want stable URLs for every client |
| Header versioning | Clean URLs | The CDN cache key and access logs ignore the header |
| Action endpoint (POST /orders/123/cancel) | The action has no natural noun | A noun resource reads naturally |
| Nested sub-collection (/orders/123/lines) | The child has no meaning outside the parent | Nesting goes beyond two levels |
Traps interviewers probe
- Returning 200 with an error body. Every intermediary, generic client and breaker counts this as success, so retries never fire and the breaker never opens. Fix: send a real 4xx or 5xx with a problem-details body.
- Retrying a POST without a key. A timeout after the server acted produces a second charge. Fix: send an Idempotency-Key and store the response before side effects.
- Assuming a GET is harmless. A link scanner fetches each delivered copy once and each open once. With 400 recipients who each open twice, a GET that appends a row writes 400 + 800 = 1,200 rows. Fix: make the write a POST, PUT or DELETE.
- Process-local state behind a round-robin balancer. Only the instance that took the write sees the data. Fix: persist the state in the shared database, where every instance can reach it.
- Offset pagination on a feed with inserts. Deep offsets scan and discard rows, and new inserts shift pages so rows repeat or vanish. Fix: keyset pagination on a stable composite key such as (created_at, id), with an index to match.
- Unbounded limit parameters. One caller can hold the whole connection pool. Fix: cap page size on the server.
Interview tips
Opening. "Resources are nouns and the method carries the verb. Safety and idempotency are contracts that caches, proxies and retry logic depend on, so I design to them explicitly."
Status codes to get right.
- 201 with
Locationfor a created resource. 200 for an update, even when the request was a PUT upsert on an existing row. - 202 for work that continues asynchronously: return
Locationfor polling andRetry-After. The client must poll; it does not get the result inline. - 401 means authenticate and try again. 403 means authenticating differently will not help.
- 400 is malformed. 422 is well formed but semantically invalid. 429 comes with
Retry-After.
Follow-ups to expect.
- Why not POST everything? Name who relies on safety and idempotency: caches, prefetchers, retry logic.
- The POST timed out, did the charge happen? You cannot know from the client. That is why the key goes out first.
- A crawler deleted data. Whose fault is it? The API's. Say so directly.
- How do you deprecate a field? Additive changes first, unknown-field tolerance stated as a requirement, usage measured per client, then a dated timeline.
- Your feed has heavy concurrent writes. Name offset duplicates and skips, then give keyset with a stable composite key.
30-second outline. State the constraint and the value it buys. Map methods to safety and idempotency. Add a key to every unsafe write that may be retried. Return real status codes with a problem-details body. Version additively, measure old-version usage, and only then remove.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.