Highlights

Contents

Part II
02

HTTP methods and idempotency

We learn what each HTTP method promises and which promises let a client retry safely. Networks drop responses, and a client that cannot tell a lost request from a lost reply will either give up too early or do the work twice. We will cover the five everyday methods, the three special ones, the safe and idempotent properties, and how an idempotency key makes a POST retryable.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1HTTP messages2HTTP methods andidempotency3HTTP status codes4HTTP caching headers5HTTP/1.1, HTTP/2 andHTTP/36TLS and the handshake7Certificates and trust8DNS for backend engineers9The HTTPS request end toend

#Section 5GET, POST, PUT, PATCH and DELETE

Wren’s order API needs five operations: read an order, create one, replace one, change one field, and cancel one. HTTP has a verb for each. GET reads a representation of a resource and must not change it. POST asks the server to process the enclosed data. Most often that means creating a new resource inside a collection, as in POST /orders, where the server picks the new id and returns it. PUT replaces the resource at the given URL with the enclosed representation, creating it if the client chose the URL. PATCH applies a partial change. DELETE removes the resource.

FIVE VERBS AGAINST ONE RESOURCE, /orders/123FIVE VERBS AGAINST ONE RESOURCE, /orders/123/orders/123GETread itPUTreplace itPATCHchange fieldsDELETEremove itPOST /orderscreate a new one
Figure 1.7One resource, five verbs. POST targets the collection because the server chooses the new id.

The difference between PUT and PATCH shows up in real bugs. PUT /orders/123 with the body {"note":"no onions"} means “order 123 is now exactly this”. Every field you left out, the items, the address, the total, is gone or reset to a default. PATCH /orders/123 with the same body means “change only the note”. Mobile clients that send PUT with partial objects wipe data this way, often because an older app version does not know about a field that a newer server added.

PATCH bodies come in two common formats. JSON Merge Patch (RFC 7396) sends the fields to overwrite, and null means delete. JSON Patch (RFC 6902) sends a list of operations, for example [{"op":"add","path":"/items/-","value":{"sku":"pie"}}], which can also append to arrays and test a value before changing it. The server should say which format it expects through the request Content-Type, application/merge-patch+json or application/json-patch+json.

POST is the catch-all. Actions that do not fit a resource model, such as POST /orders/123/refund or POST /search with a large query body, use it. That flexibility is also why POST promises nothing about repeats, as Section 7 shows.

#Section 6HEAD, OPTIONS and CONNECT

Three methods exist for the machinery around requests rather than for application data. HEAD is GET without the body. The server returns the same status and headers, including Content-Length and ETag, and stops there. A client that wants to know whether a 10,000,000-byte menu export changed, or how large it is before downloading, sends HEAD and spends a few hundred bytes instead of ten million. Frameworks usually implement HEAD automatically by running the GET handler and discarding the body, which is correct but wastes the work of building the body.

OPTIONS asks what a resource supports. The server answers with an Allow header listing methods, such as Allow: GET, HEAD, PUT, DELETE. Browsers also send OPTIONS on their own as the CORS preflight described in Unit II, so OPTIONS requests must reach the CORS handler without needing a login.

CONNECT asks a proxy to open a raw TCP tunnel to a host and port, as in CONNECT api.wren.example:443 HTTP/1.1. When the proxy replies 200, it stops interpreting HTTP and copies bytes in both directions. A browser behind a corporate proxy reaches Wren this way. The TLS session runs end to end through the tunnel, so the proxy learns the destination host and port and nothing else.

CONNECT TURNS A PROXY INTO A BLIND TUNNEL; OPTIONS ASKS WHAT IS ALLOWEDCONNECT TURNS A PROXY INTO A BLIND TUNNEL; OPTIONS ASKS WHAT IS ALLOWEDclientforward proxyapi.wren.exampleCONNECT :443200 tunnelencrypted TLS bytes pass through; the proxy cannot read them
Figure 1.8CONNECT turns a forward proxy into a tunnel. Only the destination host and port are visible to it.

There is one more, TRACE, which echoes the request back for debugging. Servers disable it, because echoing headers once let scripts read cookies they were not supposed to see.

#Section 7Safe and idempotent methods

Two properties decide what a client, proxy or library may do with a request without asking you first. A method is safe if the client does not request any change to server state. GET, HEAD and OPTIONS are safe. The server can still write an access log or bump a view counter. The client did not ask for those effects and is not responsible for them. Safety is a promise about intent, and crawlers, prefetchers and caches rely on it.

A method is idempotent if sending the same request once or ten times leaves the server in the same final state. Every safe method is idempotent, since none of them change anything. PUT and DELETE are idempotent too. POST and PATCH are not, by default.

SAFE MEANS NO INTENDED CHANGE; IDEMPOTENT MEANS REPEATS LAND IN THE SAME STATESAFE MEANS NO INTENDED CHANGE; IDEMPOTENT MEANS REPEATS LAND IN THE SAME STATEidempotentsafeGETHEADOPTIONSPUTDELETEPATCHPOSToutside bothPATCH can be idempotent if it sets values;POST needs a key
Figure 1.9Safe implies idempotent, but not the reverse. POST and PATCH carry neither promise by default.

Work through Wren’s examples. PUT /orders/123 with a full order sets the state to that order, and a second copy sets it to the same thing. DELETE /orders/123 removes the order. A repeat finds nothing to remove, and the state is the same, even though the second call returns 404 instead of 204. Idempotency is about the final state, not about getting identical responses. POST /orders is different. Two copies create two orders, 124 and 125, and the customer is charged for two meals.

PATCH depends on its content. “Set the note to X” gives the same result however often it runs, so it is idempotent in practice. “Append item Y to the basket” adds one more item each time, so it is not.

These properties are what let HTTP client libraries retry automatically. Many retry idempotent requests after a connection reset and refuse to retry POST, because they cannot know whether the first attempt took effect.

#Section 8Idempotency and retries

Here is the case that costs real money. The Wren app sends POST /payments for 450 rupees. The server charges the card and returns 201 Created. The phone goes into a lift and loses signal before the response arrives, so the app times out. From the app’s side there are two possibilities. Either the request never arrived, or it arrived, succeeded and only the reply was lost. The app cannot tell them apart. If it retries blindly, the customer may pay 900.

THE RESPONSE WAS LOST, NOT THE REQUEST: A BLIND RETRY CHARGES TWICETHE RESPONSE WAS LOST, NOT THE REQUEST: A BLIND RETRY CHARGES TWICEmobile appWren APIpaymentsPOST /payments 450charge 450201 CreatedtimeoutPOST /payments 450 (retry)charge 450 againcustomer pays 900
Figure 1.10The request succeeded and only the response was lost. Without an identity for the operation, the server cannot tell a retry from a new payment.

The fix gives the operation an identity the client chooses. When the user taps “Pay”, the app generates a random idempotency key, usually a UUID, and sends it as Idempotency-Key: 7f3a... on the first attempt and on every retry of that same tap. The server stores the key together with the outcome, in the same database transaction as the payment record. When a request arrives with a key it has seen, it returns the stored response instead of charging again. If the first attempt is still running, the second gets 409 Conflict and retries a little later. Stripe made this design popular, and an IETF draft standardises the header name.

AN IDEMPOTENCY KEY TURNS THE RETRY INTO A LOOKUPAN IDEMPOTENCY KEY TURNS THE RETRY INTO A LOOKUPPOST /paymentsIdempotency-Key: 7f3a{"amount":450}keys7f3a -> 201, pay_88second arrivalkey existsreturn stored201, pay_88no new chargefirst arrival: run it, store the outcome under the key
Figure 1.11The first arrival runs and records its outcome under the key. Every later arrival is a lookup.

Three details make it work. The key must live longer than any client’s retry window, and 24 hours is common. The server should also store a fingerprint of the request body, so the same key arriving with a different amount is rejected with 422 instead of silently returning the old result. And the key belongs to one user action, not one HTTP attempt, so the app must generate it before the first try and reuse it. Unit V designs the full storage.