0%
API Design Interviews: REST, gRPC, GraphQL, Pagination, Idempotency, Versioning and Webhooks
Idempotency, Retries and Concurrency Control
Every API that crosses a network will see requests fail in ways the client cannot fully understand. A mobile app on a train loses signal halfway through a call. A load balancer drops a connection during a deploy. A server finishes the work but takes longer than the client timeout to answer. In each case the client is left holding a question it cannot answer on its own: did my request take effect or not? The rest of this lesson is about designing APIs so that the safe response to that question is always the same: send it again.
In an API design interview, this topic usually appears the moment you define a write endpoint. You propose POST /payments or POST /orders, and the interviewer asks what happens if the client times out and retries. A weak answer says the client should not retry. A strong answer explains why retries are unavoidable, which methods are already safe to retry, how idempotency keys make the rest safe, and how the server stores and expires those keys. This section covers the first two parts.
The three outcomes of a failed call
When a request fails, there are three possibilities, and the client usually cannot tell them apart. The request may never have reached the server, for example because DNS failed or the connection was refused. The request may have reached the server and failed there, before or during the work. Or the request may have succeeded on the server, and only the response was lost on the way back.
The third case is the dangerous one. From the client side, a lost response looks exactly like a request that never arrived: a timeout or a reset connection. If the client retries a create order call in that situation, and the server treats every call as new, the customer gets two orders. If it is a payment, they are charged twice. If the client does not retry, the customer may have no order at all. Neither choice is acceptable, which is why the fix has to come from the API design, not from the client being careful.
Some failures are unambiguous. A 400 Bad Request with a validation error means the server rejected the input and did nothing. A 409 Conflict means a rule stopped the change. These are deliberate answers from the server and the client should not blindly retry them. The ambiguous failures are timeouts, connection resets, 502 and 503 from a proxy, and 500 from a server that may have crashed partway through. Lesson six covers how to classify status codes as retryable or not. Here the focus is on making the retry itself harmless.
What idempotent means, precisely
An operation is idempotent if performing it once or many times leaves the server in the same state. Note that it is about the state on the server, not the response. Deleting a resource twice is idempotent even if the first call returns 204 and the second returns 404, because after both calls the resource is gone. Setting a user's display name to Priya is idempotent because the name is Priya whether you set it once or five times. Adding ten dollars to a balance is not idempotent, because each call moves the balance again.
HTTP defines which methods are expected to be idempotent. GET, HEAD, OPTIONS, PUT and DELETE are idempotent by definition. GET, HEAD and OPTIONS are also safe, meaning they should not change state at all. POST is neither safe nor idempotent. PATCH is not guaranteed to be idempotent, because a patch can say increment this counter or append this item as easily as it can say set this field. Lesson three covers the full method semantics; what matters here is what those definitions buy you. Infrastructure such as proxies, SDKs and service meshes may automatically retry idempotent methods on a connection failure, and they will not retry POST. If you design a PUT that is not actually idempotent, that automatic retry becomes a bug.
PUT with a client chosen identifier
One way to make creation idempotent without any extra machinery is to let the client choose the resource identifier and use PUT. Instead of POST /documents returning a new id, the client generates an id, often a UUID, and calls PUT /documents/3f9a. The first call creates the document. A retry with the same body finds the document already there with the same content and returns success. Because the id is the natural deduplication key, there is nothing extra to store.
This works well for resources the client naturally names, such as files in a sync client, settings, or records imported from another system. It works less well when the server must assign identifiers, for example sequential invoice numbers that must have no gaps, or when the request is an action rather than the creation of a thing, such as charge this card or send this email. For those you need idempotency keys, covered in the next section.
Make the natural operation idempotent where you can
Before reaching for keys, look at whether the operation can be redefined so that repeating it is harmless. Instead of an endpoint that adds an item to a cart, have one that sets the quantity of an item in the cart to three. Instead of an endpoint that increments a counter, record the event with a unique id and let the count be derived. Instead of mark as read toggling a flag, have it set the flag to true. These redesigns cost nothing at runtime and remove whole classes of retry bugs.
The interviewer is checking whether you notice that the shape of the request decides its retry safety. A set operation with absolute values is idempotent. A delta operation is not. When you write a PATCH body in an interview, it is worth saying aloud that the fields are absolute values, so a repeated patch converges to the same state.
Why not just tell clients not to retry
Some candidates propose that clients should simply not retry writes, or should first call a GET to check whether the write happened. Neither holds up. Not retrying turns every network blip into a lost order and a support ticket. Checking first only works if the client can identify the thing it created, which it usually cannot, because the server assigned the id in the response that was lost. And even with an id, a check then write sequence has a race if the original request is still being processed when the check runs.
Real clients also retry without being asked. Browsers re-send requests on some connection failures. Mobile networking libraries retry. Users tap the button again when the spinner does not stop. A queue consumer that crashes before acknowledging a message will see it again. You cannot prevent duplicates from arriving, so the API has to be designed to absorb them. That is the principle to state early in the interview: assume every write will be delivered at least once, and make the result the same as if it were delivered exactly once.
Where the duplicate can sneak in
It helps to list the layers that can create duplicates, because an interviewer may ask which ones your design covers. The end user can double click or double tap. The client code can retry on a timeout. An SDK can retry on a connection reset. A gateway or service mesh can retry on a 503. Inside your own system, a worker can process a queue message twice. Each layer has its own fix: a disabled button, an idempotency key generated once per user intent, an SDK that reuses that key on every attempt, a gateway that only retries idempotent methods, and a consumer that deduplicates by message id. A good design covers all of them, and the key point is that the idempotency key must be created at the top, at the moment of user intent, so that every lower layer reuses it rather than inventing a new one.
Idempotency and side effects outside your database
Even when your own database write is idempotent, a request may trigger side effects elsewhere: an email, a call to a card network, a message on a queue. Repeating the request must not repeat those effects either. That means each external call needs its own deduplication: pass your idempotency key or a derived one to the card network, record that the email for order 812 was sent before sending another, and publish events with stable ids. When you describe an idempotent endpoint in an interview, mention the downstream calls too. Interviewers often probe exactly there, because it is where real systems leak duplicate charges and duplicate notifications.
A quick test for any endpoint
For each write endpoint in your design, ask one question: if this exact request arrives twice, what changes? If the answer is nothing, the endpoint is naturally idempotent and you can say so. If the answer is a second order, a second charge or a second email, it needs a key or a redesign. Running this test aloud over your endpoint list takes under a minute and shows the interviewer a deliberate habit.
Early in the interview, say that every write may arrive more than once and the API will make duplicates harmless. That single sentence frames idempotency keys, retries and consumer deduplication as one coherent design instead of three unrelated features.