Duplicate requests
A timeout is not a failure
Section titled “A timeout is not a failure”A client sends a request and waits. After 500 ms it has heard nothing. It knows one thing: no answer arrived in time. It does not know whether the request never reached the server, whether the server did the work and the answer was lost, or whether the answer is still on its way. From the client’s side, all three look the same.
The usual reply is to send the request again. If the request only reads something, that costs nothing. If it takes a payment or creates an order, and the first attempt had in fact been done, the retry does it a second time. The course’s reliability module treats retries in depth. This module starts from the problem they cause: the same operation arriving twice.
Mental model. You post a cheque and hear nothing for a week. You cannot tell whether it was lost in the post or cashed and the receipt lost. Posting a second cheque is safe only if the bank can recognise that both pay the same bill.
HTTP has a word for operations that are safe to repeat. RFC 9110 defines an idempotent method as one where several identical requests have the same effect as a single one, and lists GET, HEAD, PUT, DELETE, OPTIONS and TRACE as idempotent. POST is not on that list, and POST is the method that creates orders and takes payments. Stripe’s documentation says the same from the other side: its API accepts idempotency keys on POST requests, and sending one on a GET or DELETE has no effect, because those are idempotent by definition.
Run the failure
Section titled “Run the failure”The shop sends its writes to one primary through an API. The API waits at most 500 ms for the primary’s answer and, if none comes, sends the write once more. Now the link between them becomes slow, and a round trip takes 600 ms. The primary is healthy and does every write it receives, but every answer arrives after the API has stopped waiting.
Duplicate request
The shop sends its writes to one primary. The API waits at most 500 ms for the primary’s answer, and if none comes it sends the write once more. Now the network between the API and the primary becomes slow, and a round trip takes 600 ms. The primary is well and does every write it receives, but every answer comes after the API stopped waiting. So the API sends each write again, and the primary does it again. Watch how many times the database does what the client asked once, and what the client is told. Then decide what you would have built differently and run the same failure again.
The failure lab needs JavaScript. The healthy system is shown below.
1. Break it
The healthy system is shown first. When you press the button the failure strikes, and the run follows the 60 seconds after it.
What the failure does: The network between the API and the primary becomes slow: a round trip takes 600 ms. The primary is well and does every write it receives, but the answers come back later than the API waits.
Each dot stands for many requests. The numbers show the real rate.
A dashed arrow is a call the sender does not wait for: the primary sends each write on and carries on.
No bottleneck: every component has headroom.
What happens to the data
Figures from the model at the moment shown. They change as the run plays.
- Writes done again, so far
- —
- Times the database did a write it had already done, because the same call arrived again after the caller stopped waiting. A database that recognises the call by its key does not.
- Writes reported as failed, so far
- —
- Writes the caller gave up on after every attempt, so the client was told each one failed. The database may have done some of them: the caller cannot tell.
Cause and effect
Each step leads to the next. A step that is changing right now is marked.
- Traffic900 req/sNormal steady
- API utilisation75%Normal steady
- API queuenone waitingNormal steady
- Latency (p99)101 msNormal steady
- Timeouts and rejections0%Normal steady
- Errors0%Normal steady
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Clients | Healthy | <1% | 900 | 900 | 0 | 0 ms | 0 ms | 0 ms |
| API | Busy | 75% | 900 | 900 | 0 | 14 ms | 60 ms | 93 ms |
| Primary | Healthy | 9% | 180 | 180 | 0 | 2.8 ms | 12 ms | 18 ms |
| Replicas | Healthy | 36% | 720 | 720 | 0 | 5.5 ms | 24 ms | 37 ms |
| Replication | Healthy | 45% | 180 | 180 | 0 | 3.2 ms | 14 ms | 21 ms |
- Traffic
- 900 req/s
- Successful
- 900 req/s
- Error rate
- 0%
- Mean latency
- 28 ms
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random, as a Poisson process, at a constant average rate.
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Service times are random with the stated mean; by default they are exponentially distributed.
- Each node is analysed separately, with the traffic it receives treated as Poisson.
- The system starts healthy at 900 requests a second. One request in five writes, 180 writes a second, and four in five read, 720 reads a second. Three API instances can serve 1,200 requests a second, the primary 2,000 writes a second, and the two replicas together 2,000 reads a second.
- Replication is asynchronous: the primary acknowledges a write as soon as it has it, and sends it to the replicas without waiting for them. Each replica applies the stream at up to 400 writes a second. The diagram shows one replica's apply stage, since the replicas are alike and fall behind together.
- The link to the replicas takes 250 ms there and back, so a write reaches a replica 125 ms after the primary acknowledged it, and is applied a few milliseconds after that, longer if the replica has a backlog.
- A failure strikes at one instant. Nothing repairs it except the steps the lab lists, which happen at fixed times: no restart, no operator, no autoscaling.
- The API retries a failed write once, which is the lab’s set-up. A retry follows at once, with no delay.
Simplifications
- A repeat is counted when the primary accepts an attempt for a write that it already received, whether or not the caller was still waiting. Every accepted attempt does its work.
- The primary with idempotency keys still receives every attempt and still passes every attempt on to the replicas, which would not happen with a real key store. The replicas apply 360 writes a second instead of 180, 90% of what they can.
- Nothing is lost on the network and nothing is refused: the only reason an attempt fails is that its answer is late.
- Sending a write to a replica and applying it are one stage with one rate here. Real databases report them separately (PostgreSQL has write, flush and replay lag): what a primary loses when it dies is what never reached a replica, while what a read returns depends on what has been applied.
- A replica applies the stream and answers reads in two separate components, so reads stay fast while it lags. A real replica does both with the same machine.
- Nothing is retried: a write that fails is refused to the client, and the client does not send it again.
- Every replica is the same and falls behind by the same amount. A real replica can be slower than its siblings.
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
- A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
- A request calls its dependencies one after another. Parallel fan-out is not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
- Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
- A caller’s timeout counts the mean time its dependencies take, not their full distribution.
- Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
Parameter values are illustrative. They are not benchmarks of any real product.
The shop asks for 180 writes per second. Every attempt is too late, so each write is sent twice and the primary does both: 360 writes per second are done, 180 more than were asked for. Over the 60 seconds of the run the lab counts 10,711 repeats, a little under 180 times 60 because the failure takes a few seconds to reach every write. If a write is a payment, that is a customer charged twice, thousands of times.
The client fares no better. The retry is also late, so the API gives up on every write, and the lab shows 10,665 writes reported as failed. The client was told that they failed, and the database had done them, twice each.
Three fixes, and their prices
Section titled “Three fixes, and their prices”Pick one in the lab and run the same failure again.
Send an idempotency key with every write. The client makes up a key for each write and sends the same key on every retry. The primary keeps the result of the first attempt by key and answers a repeat from it. The repeats fall from 10,711 to 0. But the 10,665 writes reported as failed do not change: the writes are still too slow, and the client is still told that they failed when they were done. The primary must also store every key and its result, look them up before every write, and keep them long enough for a retry to find them. The lab counts no cost for the lookup.
Never retry a write. Nothing is sent twice, so the repeats are 0. The cost is that the client is now told that 10,711 writes failed, and cannot know which of them were done. A retry that would have worked is given up too.
Wait longer for the answer. With a timeout of 1,500 ms the answers, a little over 600 ms, arrive in time: 0 repeats and 0 writes given up. It is the only fix that removes both numbers, and it fixes this slowness and nothing else. If the link gets slower than 1,500 ms, the same duplicates come back, and every caller waits longer for a primary that is really down.
| Fix | Writes done again | Writes reported as failed |
|---|---|---|
| None | 10,711 | 10,665 |
| Idempotency key | 0 | 10,665 |
| Never retry | 0 | 10,711 |
| Longer timeout | 0 | 0 |
No row is the answer to everything. A longer timeout shrinks the window in which duplicates happen, and a key makes the duplicates harmless whatever the timeout. The next lesson builds the key.
What happens if the retry is done by someone else?
Section titled “What happens if the retry is done by someone else?”You can choose not to retry a write in your own code. You cannot always stop others: a load balancer may retry a request, a mobile client may resend when the user taps again, and a message queue may deliver a message a second time. Duplicates come from every layer that wants to be reliable. A write that is only safe if nobody retries it is not safe, so the lasting fix belongs in the operation itself.
- A timeout says that no answer arrived, not that nothing was done. Retrying an operation that was done does it twice.
- RFC 9110 lists GET, HEAD, PUT, DELETE, OPTIONS and TRACE as idempotent. POST is not, and it is the method that creates and charges.
- In the lab, 180 writes per second are asked for and 360 are done, which makes 10,711 repeats in 60 seconds, and 10,665 writes are reported as failed.
- A key removes the repeats but not the false failures, never retrying removes the repeats and reports more failures, and a longer timeout removes both for one slowness only.
Next: Idempotency keys.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.