Kill the primary (failover)
Kill the primary (failover)
The shop sends its writes to one primary database and its reads to two replicas. Replication is asynchronous: the primary tells the client a write succeeded as soon as it has it, and sends it on to the replica, which is far away, 250 ms there and back. You kill the primary. Writes fail until a replica is promoted, which takes 30 seconds from the failure here. Watch the writes that fail, then count the writes the primary had acknowledged that the replica never received. 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 primary database stops answering. Writes have nowhere to go until a replica is promoted. Reads carry on.
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.
Then, with no one pressing a button: A replica is promoted to primary after 30 s.
- Writes have been failing for
- —
- The seconds in which more than half of the writes were refused.
- Writes refused so far
- —
- Writes the primary could not take. The client was told that each one failed.
- Acknowledged writes the replica never received
- —
- Writes the primary had told clients succeeded that had not reached the replica when it died: the writes still on their way. A primary that waits for the replica has not acknowledged them yet.
- Time a write takes at the database
- —
- From the API to the primary, and to the replica too when the primary waits for it.
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.
- A replica is promoted 30 seconds after the failure. The figure is illustrative: it stands for noticing that the primary is gone, then promoting a replica. A fix can shorten it.
Simplifications
- Nothing detects the failure and no one decides: the promotion comes at a fixed time. How long a real failover takes depends on how the failure is detected and who promotes.
- The promoted replica takes over at once with everything it received, and the old primary never returns. A real failover must also stop the old primary from accepting writes if it comes back, which the PostgreSQL documentation calls STONITH. This lab does not simulate that, or the two-primaries situation it prevents.
- Every acknowledged write that the promoted replica did not have counts as lost. Some may still be on the failed primary’s disk, if it can be read.
- Promoting a replica takes it out of the replicas, so the reads that remain run on one. Nothing replaces it during the run.
- 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.