TweakCode
Building Blocks lessons

Databases · Stage 2 of 3

Read Replicas & Read-Your-Writes

Scale reads with lagging copies, and route a writer to the primary briefly

Foundations

In the first lesson, one primary served every read and write. Now, what if reads outgrow it, or it fails, and copies of the data must serve reads too?

Replication streams the primary's log of committed writes to read replicas. With asynchronous replication, the primary acknowledges a write before the replicas apply it, so a replica can lag behind by a moment: fast writes, slightly stale reads. Read-your-writes fixes the worst case: after a user writes, route that user's reads to the primary for a few seconds, so they always see their own change.

Take a primary at log position 41 and a replica at 39. A record committed at 40 is missing from the replica for a moment, so a stranger reading there sees the older state, which is acceptable. The user who wrote it is routed to the primary for a few seconds and sees it immediately.

writes + readsasync logreadsWriterPrimarylog at 41Replicalog at 39Others
The replica is two entries behind, so others may briefly read an older state. The writer's own reads go to the primary for a few seconds, so they see their change.
  • Databases

    Queues and partitions sit around a primary database. That lesson covers the key and the index.

    Open lesson

Case Scenario

The design brief

Place the replicas

The primary takes creates. Redirects should usually be served from a cache, and on a miss you may read a replica.

To see how replicas trade freshness for capacity, consider a URL shortener like bit.ly: the primary takes every create, redirects are served from a cache, and a cache miss may read a replica. The creator of a new short link must be able to open it immediately, while a stranger may see it up to a second later.

A create can succeed moments before another machine serves the first redirect, and a machine can fail while that create is still moving through the system. Treating every reader alike either slows all reads or surprises the person who just created the link. The creator of x7Kp must open it immediately; a stranger may observe it up to about one second later.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.