GOAT EngineerPreparing your learning space…

System Design · Concept Library · Free article

Read Replicas — copies of the database near your readers

How to make database reads fast and plentiful when one primary can’t keep up — or is an ocean away: keep read-only copies that serve reads while the primary owns the writes. From what a replica is up through read-scaling, replication lag, and the read-your-own-writes trap it creates (and how to fix it) — for interns through principal engineers.

A read replica is a read-only copy of your database that serves reads, so the primary can focus on writes. Most apps read far more than they write (~90/10), so you point all writes at one primary and spread reads across N replicas — multiplying read capacity, and letting you put a copy near far-away users so their reads resolve locally instead of crossing an ocean. The catch is that replicas are kept in sync asynchronously — the primary commits, then ships the change in the background — so a replica runs a beat behind (replication lag, milliseconds to seconds). That creates the read-your-own-writes trap: you write, immediately read a lagging replica, and don’t see your own change (a "ghost"). The fix is surgical — pin the recent writer to the primary for a few seconds — not the sledgehammer of making every write wait. And remember what replicas don’t do: they scale reads, never writes — the single primary is still the write ceiling, which is where sharding comes in later.

The edge can’t cache Liam’s dashboard

The CDN fixed the shared reads — Zoe’s redirect is now answered a few kilometres away. But Liam, a creator in Sydney, opens his dashboard: his links, his click counts, his settings. That’s per-user data — the edge can’t cache it (it’d serve Liam’s numbers to the next visitor), so every one of those reads still travels across the Pacific to the single primary database near Seattle. He’s back to ocean latency, and worse, every creator’s dashboard read piles onto that one database.

The horizontal-scaling page warned this was coming: the stateless app tier clones freely, but the stateful database is the hard part. Here’s step one of taming it. You can’t cache a per-user read — so instead you keep a read-only copy of the database itself near Liam, and let it answer his reads locally. That copy is a read replica.

One writer, many readers

The pattern is a single primary (the one database that accepts writes — the source of truth) plus one or more replicas (read-only copies that accept reads). Your application does a read/write split: send every write and every read-that-must-be-current to the primary, and send the rest of the reads to a replica — often via a connection router that knows which is which. Add a replica in Sydney and Liam’s dashboard reads never leave the region.

Watch writes flow to the one primary while reads fan out across the replicas — and add a regional replica so a local user reads locally:

The interactive version of this example is available when JavaScript loads.

The primary streams its changes to the replicas

A replica stays up to date by replaying the primary’s change log — every insert, update, and delete, in order. The primary writes each change to a log (Postgres calls it the write-ahead log / WAL; MySQL, the binlog) and streams it to each replica, which applies the same changes to its own copy. Crucially, this is asynchronous by default: the primary commits your write locally and returns immediately, then ships the change to replicas in the background. That’s what keeps writes fast — they never wait for a replica — but it’s also the source of every subtlety on this page.

Scaling reads — and the ceiling replicas don’t move

Because most workloads are read-heavy — a ~90/10 read/write ratio is typical — replicas are a huge win: each one you add serves more reads, so read capacity grows roughly with the number of replicas. But hold the whole picture: every write still goes to the one primary. Replicas copy from it; they don’t share the write load. So adding replicas scales reads without limit but does nothing for writes — the single primary remains the write ceiling. Drag the replica count and watch reads climb while writes stay flat:

The interactive version of this example is available when JavaScript loads.

Replication lag: the copy runs a beat behind

Because replication is asynchronous, a replica is always slightly behind the primary — the gap between "the primary committed this" and "the replica has applied it" is replication lag. Usually it’s milliseconds; under heavy write load, a long transaction, or a slow network it can stretch to seconds. Engineers measure it two ways: in time (how many seconds behind the primary a replica is) and in log position (how many WAL/binlog bytes it still has to apply). Drag the lag and see the window of recent writes a replica read might miss:

The interactive version of this example is available when JavaScript loads.

The ghost link: reading your own write

Here’s the failure that bites everyone. Liam creates a new short link — the write goes to the primary and succeeds. A millisecond later he clicks it — the read goes to his local replica, which hasn’t received the new row yet because of lag. Result: a 404 on a link he just made. Nothing is broken; the row is safe on the primary; the replica is simply a beat behind. But to Liam it looks like his work vanished. The guarantee he needs is read-your-own-writes (a.k.a. read-after-write): the moment you write something, your next read reflects it — even if everyone else sees it a moment later. Reproduce the ghost, then turn the fix on:

The interactive version of this example is available when JavaScript loads.

Fixing read-your-own-writes (and its cousins)

The toolkit, cheapest first

  • Pin the recent writer to the primary. After someone writes, route their reads to the primary for a short window (a TTL a bit longer than your worst expected lag — a few seconds), via a session marker/cookie. The writer always sees their change; everyone else keeps reading the fast local replicas. This is the surgical, standard fix.
  • Read your own data from the primary. Simpler rule: always read the user’s own profile/objects from the primary, and everyone else’s from a replica. Coarser but easy to reason about.
  • Version / wait-for-position. The client remembers the log position (or timestamp) of its last write; a read is only served by a replica that has caught up to at least that position. Precise, but more machinery.
  • Monotonic reads. A related guarantee: don’t let a user’s reads go backwards in time (see a value, then see an older one from a more-lagged replica). Fix by pinning a session to one replica so it never jumps to a staler one.

The consistency spectrum

Not every read needs the same freshness, and matching the guarantee to the read is a senior instinct. At the loose end, eventual consistency: the replica will catch up eventually, and that’s fine for a like-count or a follower list. In the middle, session guarantees — read-your-own-writes and monotonic reads — give one user a coherent view without global coordination (the sweet spot for most user-facing reads). At the strict end, strong consistency: always read the latest, which means reading from the primary (or paying for synchronous coordination) — reserve it for the few reads where stale is genuinely wrong (a balance before a payment, say). Decide which bucket each read falls in:

The interactive version of this example is available when JavaScript loads.

Replicas are also a safety net

A read replica isn’t only for reads — it’s a warm, up-to-date copy of your data, so if the primary dies, a replica can be promoted to become the new primary and the system keeps running. That’s the foundation of database high availability. But promotion, choosing which replica, avoiding two primaries at once, and the synchronous-vs-asynchronous trade-off for durability are a meaty topic in their own right — the next time the saga faces a dead database node, that’s the chapter that goes deep. Here, just hold the idea: the copies you built for read scaling double as your failover insurance.

Measuring & operating replicas

  • Replication lag is the #1 metric. Track it per replica in both seconds-behind and log-bytes-behind. A climbing lag is an early warning that a replica (or the network, or a long transaction) is struggling.
  • Fence off too-lagged replicas. If a replica falls more than your tolerance behind, pull it out of the read pool until it catches up — better to shed read capacity than to serve badly stale data.
  • Watch the read/write ratio. It tells you whether replicas are even the right tool (read-bound) or whether you’re actually write-bound (→ sharding).
  • Route critical reads to the primary explicitly. Make "this read must be current" a deliberate, visible choice in code, not an accident of which connection you grabbed.

Cost, and when replicas aren’t the answer

Every replica is a full copy of your database — real money in storage and compute — plus the ongoing complexity of routing reads and reasoning about staleness. Usually worth it for a read-heavy or globally-distributed app. But they’re the wrong tool when:

Signs replicas won’t help

  • You’re write-bound, not read-bound. Replicas don’t share write load. Too many writes for one primary means sharding, not more replicas.
  • The dataset is too big for one machine. A replica is a full copy, so it doesn’t help if the data itself no longer fits — again, sharding.
  • Every read must be strictly current. If you can’t tolerate any staleness, you’re reading from the primary anyway, so replicas add cost without offloading it.
  • Read volume is modest. One well-sized primary (plus caching) may handle it. Add replicas on a measured read bottleneck, not by reflex.

How it’s actually run

Every serious database ships replication. PostgreSQL streams its WAL to replicas (physical replication); MySQL/MariaDB ship the binlog (logical replication). Managed clouds make it a checkbox — RDS / Aurora / Cloud SQL read replicas, often placeable in other regions for locality — and handle the streaming and promotion for you. The names differ but the shape is identical everywhere: one primary owns writes, a change log streams to read-only copies, and you split your reads across them. Recognise the shape and any product is a variation on it.

The read/write split, and pinning the writer

The whole pattern is two small routing decisions: send writes (and must-be-current reads) to the primary; send the rest to a replica; and after a write, pin this user to the primary for a short window so they read their own change.

The interactive version of this example is available when JavaScript loads.

Pitfalls

  • Reading your own write from a replica. The ghost 404. Pin the recent writer to the primary for a window longer than your worst lag.
  • Ignoring replication lag. A silently climbing lag means users are reading increasingly stale data. Monitor it in time and log position, and fence off replicas that fall too far behind.
  • Expecting replicas to scale writes. They don’t — every write still hits the one primary. A write bottleneck needs sharding.
  • Sending every read to a replica. Balance checks, "did my payment go through?", and other must-be-current reads belong on the primary. Route by freshness need, not by habit.
  • Reaching for synchronous replication to fix RYOW. It makes every write pay a round trip and can block writes on a replica blip. Use session pinning for read consistency.
  • Reads that go backwards. Bouncing a session between replicas of different lag shows a user newer-then-older data. Pin a session to one replica (monotonic reads).

Best practices

  • Split reads from writes deliberately — writes + must-be-current reads to the primary; everything else to a replica, ideally a nearby one.
  • Pin the recent writer to the primary for a window longer than your worst expected lag — the cheap, standard read-your-own-writes fix.
  • Place replicas near your readers — regional replicas turn a cross-ocean read into a local one, the same locality win as the CDN.
  • Monitor replication lag (time + log position) and fence off replicas that fall too far behind.
  • Route by freshness need — classify each read as eventual / session / strong and send it accordingly.
  • Remember the ceiling — replicas scale reads; plan sharding separately for write scale and dataset size.

Rapid-fire cheat-sheet (intern → principal)

"What is a read replica and why use one?"
A read-only copy of the database that serves reads while the primary owns writes. Two wins: it scales reads (most apps are ~90/10 read-heavy) and, placed regionally, lets far-away users read locally instead of crossing to the primary.
"How does a replica stay in sync?"
The primary streams its change log (Postgres WAL / MySQL binlog) to each replica, which replays it. It’s asynchronous by default: the primary commits and returns immediately, then ships changes in the background — fast writes, but the replica runs a beat behind.
"What is replication lag and why does it matter?"
The gap between a write committing on the primary and a replica applying it — usually ms, sometimes seconds. It matters for reads where staleness is visible and wrong: reading your own recent write, or seeing data go backwards. Measure it in seconds-behind and log-bytes-behind.
"A user created something and got a 404 reading it back. Why, and how do you fix it?"
Read-your-own-writes violation: the write hit the primary, the immediate read hit a lagging replica that didn’t have it yet. Fix by pinning the recent writer’s reads to the primary for a window longer than your worst lag — not by making all writes synchronous.
"Do read replicas help with write scaling?"
No — every write still goes to the single primary; replicas only copy from it. Replicas scale reads. Scaling writes (or a dataset too big for one machine) needs sharding: splitting data across multiple primaries.
"How would you choose the consistency for a read?" (the deeper one)
Match it to the read: eventual for a like-count (a replica is fine), session guarantees (read-your-own-writes / monotonic) for a user’s own view, strong (read the primary) only for reads where stale is genuinely wrong, like a balance before a payment. Don’t force global strong consistency by default — it’s the expensive option.

The takeaway

By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.