Skip to content

← All failure labs

Replication lag

Replication lag

The shop sends its writes to one primary and its reads to two replicas. The primary sends every write on to the replicas, and each replica applies them at up to 400 writes a second. The shop normally writes 180 a second, well within that. A bulk import then quadruples the writes to 720 a second, for 20 seconds. The primary can take them. Can the replicas keep up? Watch how far behind they fall, and what the reads they answer return meanwhile. Then decide what you would change and run it 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: Four times as many writes arrive as before. The primary can take them. Every replica has to apply every one.

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.

Then, with no one pressing a button: The burst of writes ends after 20 s.

Replica lag: how old the data a replica returns is
—
How long a write takes to reach a replica and be applied. A read answered by the replica returns the database as it was that long ago.
Writes waiting to be applied
—
Writes the replica has received and not yet applied.
Reads that could be stale, so far
—
Reads answered by a replica that was more than 1 s behind, or that was cut off from a primary that went on acknowledging writes. Each could return data that has changed since.
Writes refused so far
—
Writes the primary could not take. The client was told that each one failed.

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 burst lasts 20 seconds, and then the writes return to what they were.

Simplifications

  • The burst adds writes and nothing else: reads stay at 720 a second, where a real spike would often add reads too.
  • A replica may hold up to 10,000 writes it has not applied. The run never gets there, so nothing is cut off.
  • A read counts as one that could be stale when the replica that answers it is more than a second behind. Whether a given read is wrong depends on whether it asks for something that changed in that time, which the model does not know.
  • The replicas fall behind together, by the same amount. Real replicas can differ, so two reads a moment apart can reach replicas that are not equally behind, and the second can return older data than the first.
  • 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.