AlgoViz

REST

easy 26 min

REST is a style for network APIs: everything worth naming gets an address, and a small shared set of HTTP methods acts on it.

See it run

What each method promises, how a read is answered, and how writes stay safe when people and networks interfere.

Operations

Read the promises

Three questions about each method. The answers decide what caches and retry code may do.

Step 1 of 5

GET

saferepeatablecache keeps it
GETyesyesyes
POSTnonorarely
PUTnoyesno
PATCHnono promiseno
DELETEnoyesno

GET reads and nothing else

Safe, so a browser may prefetch it. Idempotent, so a client may repeat it. A cache may keep it.

Step 1: GET

SAFE = {"GET", "HEAD", "OPTIONS"}IDEMPOTENT = SAFE | {"PUT", "DELETE"}def may_repeat(method, has_key=False):    return method in IDEMPOTENT or has_key
Extra trip
None
Guards against
Surprises
Where it is written
RFC 9110 and RFC 5789

Idempotent speaks about the state on the server. The answers of two calls may differ.

In the real world

The same rest doing real jobs. Pick one and step through it.

A profile screen needs a user, their orders, reviews and points. Count the trips.

Step 1 of 4

Four endpoints

Our systemClientCDNpublic GETsAPI gatewaytoken, limitAPI server 1no sessionAPI server 2no sessionRedishot answersPostgreSQLthe truth

requests

user · orders · reviews · points

4 requests

The server decides each shape, so the screen asks four times.

Step 1: Four endpoints

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Writing on a GET

    critical

    Prefetching, crawlers and retries may call a GET at any time.

    Wrong
    GET /orders/9/cancel
    Right
    POST /orders/9/cancel
  • 200 for a failure

    major

    Caches, monitors and clients see a success. The error rate on the dashboard stays at zero.

    Wrong
    200 { "ok": false, "error": "not found" }
    Right
    404 { "title": "Not found", "status": 404 }
  • Checking the version in a separate read

    critical

    Another writer can slip in between the read and the write.

    Wrong
    if load(id).version == v:    save(id, body)
    Right
    UPDATE ... WHERE id = :id AND version = :v-- 0 rows changed means 412
  • Lists without a maximum

    major

    One client asking for everything can fill memory and hold a database connection for minutes.

    Wrong
    limit = int(request["limit"])
    Right
    limit = min(max(int(request["limit"]), 1), 100)

Interview tips

  • How to explain it

    • Resources have addresses.
    • A few methods mean the same everywhere.
    • So caches, proxies and retry code can help.
  • Before you ship an endpoint

    • Right method and right status code?
    • What happens if it is called twice?
    • Who may cache the answer, and for how long?
  • Reach for it when

    • The API is public.
    • The work is mostly create, read, update and delete.
    • Reads repeat.
  • Look elsewhere when

    • The server must push.
    • Services exchange many small calls.
    • Each screen wants another shape.