REST and HTTP APIs: resources, methods and retries
One question, one answer
Section titled “One question, one answer”Most APIs that you will build or call work the same way. The client sends a request and waits. The server does the work and sends one response. Microsoft’s guidance on microservices calls this a synchronous protocol: the client sends a request and waits for a response from the service, and it can only go on when the response arrives. This module starts here, and the next lessons are about what to do when a request and a response are not enough.
The word REST is attached to many such APIs. This lesson uses it in Roy Fielding’s sense, as a short list of constraints, and says “HTTP API” for an API that is built from HTTP requests whether or not it follows all of them.
What REST asks for
Section titled “What REST asks for”Fielding’s dissertation defines an architectural style by the constraints it adds to a plain client-server design. Four of them matter for the rest of this module:
- Client-server. The client and the server are separate programs that can change independently.
- Stateless requests. Each request from the client to the server must contain all of the information necessary to understand the request. The server keeps no conversation between two requests.
- Cacheable responses. The data in a response is labelled as cacheable or not, so that someone along the way can reuse it.
- Uniform interface. Fielding calls it the central feature that sets REST apart. It has four parts: identification of resources, manipulation of resources through representations, self-descriptive messages, and hypermedia as the engine of application state.
A resource is anything that can be named: Fielding’s examples are a document, an image, a service such as today’s weather, or a collection of other resources. A representation is a sequence of bytes plus metadata that describes them. The client never holds the resource; it holds a representation of it, such as a JSON document, and sends representations back to change it.
Mental model. A resource is a noun with an address,
/orders/42. A request is a verb applied to it: read it, replace it, delete it. Everything the server needs to know is in the request, so any server that can reach the order can answer.
Why stateless matters for scale
Section titled “Why stateless matters for scale”Because each request carries what the server needs, no request has to reach the server that handled the one before. A load balancer can send each request anywhere, a server can be added or removed between two requests, and a crashed server loses no conversation. The scalability module makes the same point about stateless services. The price is paid by the client, which has to send the same information, such as a token or the current page, with every request.
A server that keeps a checkout step in its own memory breaks the constraint, and ties each client to one server. The check below asks you to recognise it.
Check your understanding: REST and HTTP methods
This quiz needs JavaScript to check your answers.
What a method promises
Section titled “What a method promises”HTTP gives each method a meaning, and RFC 9110 defines two properties of a method that decide what a client may do when no response comes back.
- A method is safe when its defined semantics are essentially read-only. The safe methods are GET, HEAD, OPTIONS and TRACE.
- A method is idempotent when the intended effect of several identical requests is the same as the effect of one. Every safe method is idempotent, and so are PUT and DELETE. POST is not on the list.
RFC 9110 gives the reason for the distinction: a request with an idempotent method can be repeated automatically if a communication failure happens before the client reads the response. That is the situation of the duplicate requests lesson: a timeout, and no way to know whether the request was done. For GET, PUT and DELETE the client can simply ask again. For POST it cannot, unless the operation is made idempotent with an idempotency key.
| Method | Safe | Idempotent | A typical use |
|---|---|---|---|
| GET | yes | yes | Read an order |
| PUT | no | yes | Replace an order with this representation |
| DELETE | no | yes | Remove an order |
| POST | no | no | Create an order, take a payment |
Idempotent means the same intended effect on the server, not the same response. A second DELETE may answer 404 because the order is already gone, and the order is still gone. A server can also fail to keep the promise: RFC 9110 says what a method is meant to do, and an implementation that charges a card on every GET is wrong but possible.
What the status code says
Section titled “What the status code says”The first digit of a status code says what the client should do about it.
| Class | Meaning | What the client does |
|---|---|---|
| 2xx | Success | Use the result |
| 3xx | Redirection | Follow it to another address |
| 4xx | Client error | Change the request; asking again as it is will fail again |
| 5xx | Server error | The request may be fine; it can be retried later, within the limits of the method |
Choose the code by who must act. A 400 or a 404 tells the client to change something. A 503 tells it that the server could not do the work now. Retrying a 4xx unchanged only repeats the mistake, and retrying a 5xx on a POST brings back the duplicate problem.
Request and response, with a cost
Section titled “Request and response, with a cost”A response is one round trip at least, plus the time the server needs. Where the round-trip time comes from is in the networking module. A client that needs ten answers and sends ten requests one after the other pays ten round trips. That is why a design that turns every function call into a request becomes chatty: Microsoft’s guidance warns that a direct conversion of in-process method calls into remote calls gives communication that is chatty and does not perform well, and recommends coarser calls that group the work.
What happens if the client repeats a POST?
Section titled “What happens if the client repeats a POST?”The customer taps the pay button, the request times out, and the app sends it again. If the first request had been done, the order is placed twice, because POST promises nothing about repeats. Nothing in HTTP prevents it: it is the operation that has to be made safe, by a key that the client chooses and sends on every attempt, or by turning it into a PUT to an address the client chose, such as /orders/{id}, where a second PUT replaces the first with the same data. Which of the two fits depends on whether the client or the server picks the identity of the order.
- A REST API is built from resources that are named by address and changed through representations, and each request is self-contained: the server keeps no conversation.
- A stateless request lets any server answer it, which is what lets a load balancer spread the load.
- Safe methods are read-only; idempotent methods have the same intended effect when repeated. GET, PUT and DELETE are idempotent and POST is not.
- After a timeout a client may repeat an idempotent request. A POST needs an idempotency key, or a design that makes it a PUT.
- The status class tells the client who must act: change the request for a 4xx, wait and retry for a 5xx.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.