Skip to content

← All failure labs

Duplicate request

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.

Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%
Replica servers0.3 ms0.5 ms0.5 ms250 msClientsHealthy<1%APIBusy75%PrimaryHealthy9%ReplicasHealthy36%ReplicationHealthy45%

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.

0 ms
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.

Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%90090000 ms0 ms0 ms
APIBusy75%900900014 ms60 ms93 ms
PrimaryHealthy9%18018002.8 ms12 ms18 ms
ReplicasHealthy36%72072005.5 ms24 ms37 ms
ReplicationHealthy45%18018003.2 ms14 ms21 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.

Built by GhaziAlibi.

© ShardCraft. All rights reserved.