Accept a short delay for scale
Keeping every replica identical on every write is slow and fragile.
Say it like this
Redirects need availability more than freshness, so I replicate asynchronously and accept that a just-created link may take a few hundred milliseconds to work everywhere, and I say that trade-off out loud.
Basic solution
Let replicas converge asynchronously; design the UI so a few hundred milliseconds of staleness is fine.
The staleness window
The primary acknowledges the write immediately; replicas apply it a little later. Until then a reader on the replica sees the old value, which is the anomaly you must name and contain.
How it works
- A write is committed on the primary (or on W replicas) and acknowledged to the client. The client sees success at this point.
- The change is shipped to the other replicas afterwards, by a replication log, a change stream, or a fan-out job. Nothing waits for that shipping to finish.
- Reads can be served by any replica, so for a short window a reader on a replica that has not yet applied the change sees the old value while a reader on the primary sees the new one.
- The window closes when replication catches up: typically tens to hundreds of milliseconds inside a region, seconds across regions.
- The application is designed so that window is harmless: the UI shows the write immediately from local state (optimistic UI), the writer's own reads are routed to a copy that has the write (read-your-writes), and consumers of the change tolerate seeing it twice (idempotent).
What is actually promised
- Stop writing and every replica converges to the same value. Nothing is promised about when, so you measure replication lag and alert on it.
Worked example
URL shortener across two regions
- Creation writes go to the primary in us-east. The redirect fleet reads from local replicas in us-east and eu-west.
- Cross-region replication lag is ~150 ms typical, up to a few seconds under load. That lag is the staleness window.
- Creation rate 40/s; redirects 4,000/s. If every redirect read the primary cross-region, that is 4,000 × 80 ms round trip of added latency and one region doing all reads. With local replicas, redirect p99 stays under 20 ms.
- The anomaly: a user in Europe creates a link and clicks it within 150 ms, and the eu replica returns 404 because the row has not arrived yet. Fix for the creator only: the creation response includes the long URL, and the client caches it; or the creator's reads pin to the primary for 2 seconds.
- Everyone else, by the time they receive the link, is well past the window, so they never see the anomaly.
Variants and when each wins
Asynchronous replication (default eventual)
- The primary acknowledges the write before any replica has it, then ships the change. It is the fastest option and wins for read-heavy data; it loses the last writes if the primary dies before shipping them.
Semi-synchronous
- The primary waits for at least one replica to confirm before acknowledging. It wins when a committed write must never be lost, at the cost of one extra round trip per write.
Causal consistency
- Readers see writes in the order they depend on each other, so a reply never appears before its parent. It is achieved with version vectors or session tokens and wins for social products where ordering anomalies confuse users.
Strong consistency (synchronous)
- Every read sees the latest write, which needs a quorum or a single leader with linearizable reads. It wins for money, inventory and auth; it is slower and less available during partitions.
Choose eventual for read-heavy, user-generated data; add read-your-writes for the writer; pay for strong consistency only on money, inventory and auth.
Failure modes
Writer does not see own write
- "I posted and it is gone." It happens because the user's follow-up read hit a replica that had not applied the write yet. Fix it with read-your-writes routing: send that user's reads to the primary, or to a caught-up replica, for a short window after each write.
Replication lag grows unbounded
- Replicas serve data that is hours old. It happens because a replica cannot keep up: a slow disk, or a long-running query on the replica blocking it from applying changes. Fix it by alerting on lag, giving replicas faster IO, killing the blocking queries, and adding replicas to share the read load.
Out-of-order application
- A reply appears before its parent, or a delete is undone by an older update arriving later. It happens because events were applied in arrival order across partitions rather than in the order they were written. Fix it by partitioning by entity so each key's events stay in order, and by checking versions on apply so an older update cannot overwrite a newer one.
Lost writes on failover
- The last second of writes vanishes after the primary dies. It happens because replication is asynchronous and those writes had not been shipped. Fix it with semi-synchronous replication for data that cannot be lost, or accept the loss and document it for that data class.
Inconsistent caches on top of inconsistent replicas
- Stale data persists for minutes, long after replication caught up. It happens because a cache was populated from a lagging replica and now holds the old value for its full TTL. Fix it with short TTLs, or by populating caches from the primary on write.
Interviewer follow-ups
Answer out loud before you open each one.
Name the user-visible anomaly in your design and how you handle it.
For a feed: a post can take a moment to appear to followers, which is fine, but the author must see it immediately, so the author reads their own timeline from the primary or has the post injected client-side. Naming the specific anomaly shows you understand the trade, not just the term.
How do you measure eventual consistency?
Replication lag per replica, as both time and bytes behind, with alerts well below the point users notice. For cross-region, a canary that writes a timestamp and reads it from each region gives a direct end-to-end number.
Which parts of a system must not be eventually consistent?
Anything where two readers acting on different values causes real harm: account balances, inventory counts, permissions and auth tokens, rate-limit counters for security endpoints. Those go through a single leader or a quorum; everything else can be eventual.
What is the difference between eventual and causal consistency?
Eventual says replicas converge sometime, with no ordering guarantee in the meantime. Causal adds that if write B depended on seeing write A, no reader sees B without A. That removes the most confusing anomalies, such as a reply before its parent, at modest cost.
Connections
- Built on
- Merkle tree · Vector clock · CRDT
- Pairs with
- Let users see their own writes · Spread reads across replicas · Tune reads and writes with quorums · Pick consistency or availability under partition
- Pulls against
- Agree on one leader or one value · Commit across two stores
- Used in
- URL Shortener (TinyURL / bit.ly) · News Feed (Twitter / X timeline)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Aurora | Primary (us-east), Replica (eu-west) | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |