Skip to content
System Design Lab
Design to Scale

Read Replicas

Copy the database to replicas that serve reads, so the primary only handles writes — and learn to deal with replication lag, where users do not see what they just saved.

Intermediate 4 min read· Updated 2026-11-18

What you will learn

  • Explain how asynchronous replication lets replicas absorb read traffic.
  • Recognise read-your-writes violations caused by replication lag.
  • Route reads so users always see their own changes.
  • Know what replicas do not solve: write scaling and failover by themselves.

Before you start

Overview

A popular teacher cannot answer every student's question alone, so they hand photocopies of the lesson notes to assistants. Students ask the assistants; only corrections go to the teacher. The catch: right after a correction, the assistants still hold the old photocopies for a while.

Most applications read far more than they write. A read replica receives a continuous stream of changes from the primary and serves read queries. Writes still go only to the primary. Because replication is usually asynchronous, each replica is a little behind — the replication lag — typically milliseconds, sometimes seconds or minutes.

Run it

A sends everything to one primary that is nearly saturated. B splits reads across two replicas: latency halves, but count the Reads that missed a recent write. C routes a user's reads to the primary for one second after they write. Try the lag slider and the lag-spike fault.

Loading simulation…

Consistency guarantees you may need

GuaranteeProblem it preventsHow
Read your own writes"I saved my profile and it reverted"Read from the primary for a while after the user writes, or until the replica has reached the write's position (LSN/GTID).
Monotonic readsData appears, then disappears on refreshPin a user's session to one replica.
Consistent prefixAn answer is visible before its questionKeep causally related data in one partition, or read from the primary.

Routing reads in Spring

Read-only transactions go to replicas; recent writers stick to the primary
public class ReplicaRoutingDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
        boolean wroteRecently = RecentWrites.forCurrentUser().within(Duration.ofSeconds(2));
        return readOnly && !wroteRecently ? "replica" : "primary";
    }
}

@Transactional(readOnly = true)          // → replica (unless this user just wrote)
public Profile getProfile(long userId) { return profiles.findById(userId).orElseThrow(); }

@Transactional                           // → primary; records the write for this user
public void updateProfile(long userId, ProfileUpdate update) {
    profiles.save(profiles.findById(userId).orElseThrow().apply(update));
    RecentWrites.forCurrentUser().mark();
}

Trade-offs

BenefitsCosts
Read capacityAdd replicas to scale reads almost linearly.Writes still all hit one primary; replicas also replay every write.
ConsistencyThe primary stays strongly consistent.Replica reads are eventually consistent; routing logic must handle lag.
OperationsManaged databases make replicas a click away.More nodes to monitor; lag spikes during heavy writes or long queries.

Production considerations

  • Alert on replication lag in seconds, and take lagging replicas out of the read pool automatically.
  • Send heavy reports and analytics to a dedicated replica so they cannot slow user-facing reads.
  • Make the read-your-writes window longer than your normal lag, and measure stale reads if you can.
  • When writes are the bottleneck, replicas will not help — look at sharding.

Further reading

Related topics