Try
↑ ↓ move↵ openesc close

Interview prep / Replication and lag

Mid · Replication and lag · System design interview question

Every night a batch job updates a few million rows on the leader. During it, replica lag climbs from near zero to twenty minutes, and the morning dashboards read stale data until it clears. Why, and what are your options?

Sketch your own answer first, then compare it with the one below.

A strong answer

The leader runs the batch as a handful of large transactions, quickly. Each replica must apply the same changes in the leader's order, often with far less parallelism than the leader had. If the batch produces row changes faster than the replica can replay them, the backlog grows. When the batch ends the replica still has to work through it, and it only gains on the backlog at the difference between its apply speed and the normal write rate. That is why lag outlasts the job.

A row-based log makes it worse, since one statement touching five million rows becomes five million events to ship and apply.

Options, roughly in order of effort. Run the batch in small chunks, committing every few thousand rows, so replicas apply a steady stream and in-order replay is never stuck behind one giant transaction. Throttle the batch while lag is above a threshold. Schedule it away from the dashboard hours. Enable parallel apply on the replica if the database supports it, which helps when the changes touch independent rows. And take the dashboards off the lagging replica, or make them tolerate a stated staleness.

Whichever you choose, add an alert on lag in seconds, so you learn about the next one before the users do.

Sources

Learn it by running it

The replication and lag lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms