Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Foundations of Scale
Lesson 14 of 16

Scaling reads with replicas

Use replicas to multiply read capacity, and handle the replication lag that comes with them.

14 min read

Replicas as read capacity

The replicas you add for durability can also serve reads. With a 20:1 read-to-write ratio, a primary handling 1 000 writes and 20 000 reads per second is overloaded, but one primary taking writes plus four replicas each taking 5 000 reads is comfortable. Read replicas scale reads almost linearly and need no change to the data model. They do nothing for writes, though: every replica must apply every write, so if writes are the bottleneck, replicas will not help and you need batching, a different store or sharding.

Replication lag

Most read replicas replicate asynchronously, so they trail the primary by milliseconds normally and by seconds or more under heavy load. A user who saves a profile and immediately reloads the page may read from a replica that has not yet applied the change, and see their edit vanish. The data is not lost, only late, but it looks like a bug to the user.

Routing reads safely

Decide per query whether a replica is good enough, and make the routing rule explicit in code rather than leaving it to chance. A common approach is a small data-access layer that exposes a primary and a replica connection and makes each call site choose. The rules below cover most cases.

  • Read-your-writes: after a user writes, send their reads to the primary for a short window, such as 10 seconds
  • Critical reads, such as checking a balance before a transfer, always go to the primary
  • Feeds, listings, search results and analytics can tolerate a few seconds of lag and go to replicas
  • Monitor lag and remove a replica from rotation when it falls too far behind

Replicas, caches or both

Replicas and caches both offload reads, but they behave differently. A cache is faster and cheaper per read and works best for a small hot set; a replica serves any query, including long-tail items and complex filters that would never be cached. Most read-heavy designs use both: a cache absorbs the hottest 80 to 90% of reads, and replicas handle the misses so the primary is left for writes. On the canvas, watch the database utilisation in the Feedback panel as you add replicas and change the cache hit rate.

Key takeaways

  • Read replicas multiply read capacity but add nothing for writes
  • Asynchronous replicas lag, so recent writes may be missing from replica reads
  • Route read-your-writes and critical reads to the primary
  • Combine a cache for hot keys with replicas for the long tail

Sign in to track lessons and earn badges.

Replication and durabilityIntroduction to sharding

Challenge checkpoint

Course: 0/16 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 8/10Design a News FeedCapstone: serve a very read-heavy feed with precomputed caches and read replicas while tolerating replication lag.MAJORMedium
All 10 challenges →