Read replicas and replication lag

Vinnie on rung five of the scaling ladder: read replicas take the reads while the primary takes the writes. Why copies are usually asynchronous, what replication lag does to a user who just saved something, how to read your own writes, how shared-storage replicas differ from classic ones, why a read replica isn't a failover plan, and the managed replicas on AWS, Azure and Google Cloud.

GazeekDATABASE SCALING PATTERNS WITH VINNIE·EPISODE 6
EPISODE 6

Read replicas and replication lag

Vinnie on rung five of the scaling ladder: read replicas take the reads while the primary takes the writes. Why copies are usually asynchronous, what replication lag does to a user who just saved something, how to read your own writes, how shared-storage replicas differ from classic ones, why a read replica isn't a failover plan, and the managed replicas on AWS, Azure and Google Cloud.

0:00 / 6:23

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: How read replicas work (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain how read replicas take reads off the primary
  • Explain replication lag and the read-after-write problem
  • Read your own writes from the primary, and measure the lag
  • Tell read replicas apart from failover
Transcript

Read replicas and replication lag

Vinnie here. Today, rung five of the ladder. Read replicas. Reads are piling up on one database. So you make copies, and let them answer the reads. Simple. Except for one thing that bites. Replication lag. Let's sniff it out.

The problem

Lots of apps read far more than they write. Product pages, feeds, reports. A read replica is a read-only copy. Send reads there, and the primary has room for the writes.

How read replicas work

Here's how it works. One. Every write goes to one database, the primary. Two. The primary ships each change to the replicas. Usually asynchronously, so it doesn't wait for them before it says done. Three. Your app sends its reads to the replicas. More replicas, more room for reads. Four. Here's the catch. Because the copy is asynchronous, a replica can be a little behind. That's replication lag. Often well under a second, but nothing promises zero. Five. The classic bug. A user saves their profile, the page reloads from a replica, and their change is missing. The fix: read your own writes. For a short while after a user writes, send that user's reads to the primary. Or make the copy synchronous. In PostgreSQL, name a synchronous standby and set remote apply. Then a commit waits until that standby has applied it. Every write gets slower, and if that standby goes down, commits wait. Session stickiness is the same idea. After a write, the session sticks to the primary for a while.

Measure the lag: PostgreSQL

Measure the lag. On a PostgreSQL primary, pg stat replication has a row per streaming connection, usually one per standby. Replay lag says how far behind it is. On the replica itself, take the time now, minus the time of the last transaction it replayed. But on a quiet primary, that number keeps growing, even when the replica has nothing left to replay. Managed databases usually chart this for you, as a replica lag metric. Watch it, and set an alarm.

Classic vs shared-storage replicas

How do the big databases do it? PostgreSQL and MySQL replicate asynchronously by default. Both can also wait for a replica: PostgreSQL with synchronous standbys, MySQL with semisynchronous replication. Each replica keeps its own copy, and replays the changes. Some cloud databases split storage from compute. The writer and its readers share one storage volume, so there's little to copy, and the lag is much smaller. You'll see one in a moment. SQL Server has readable secondary replicas in Always On availability groups. MongoDB reads from the primary by default, because secondaries may return stale data.

Replicas are not failover

One thing people mix up. Read replicas scale reads. Failover keeps you running when the primary dies. Different jobs. Many managed databases keep a standby in a second zone, copied synchronously. Often it serves no reads at all. It's only there to take over. A read replica can be promoted, but that's a step you take, and it may be missing the last few changes. Some shared-storage databases blur the line. If the writer fails, they promote one of the readers, automatically.

In the cloud

Here's who does what, on the three big clouds. On AWS, Amazon RDS, the Relational Database Service, has read replicas, each with its own copy. Aurora shares one storage volume between the writer and up to fifteen replicas, so lag is usually well under a hundred milliseconds. And Aurora Global Database copies to other Regions. On Azure, the PostgreSQL and MySQL flexible servers have read replicas. Microsoft warns that PostgreSQL lag can stretch to minutes, even hours, under heavy writes. And Azure SQL Database can send read-only connections to a secondary, on its Premium, Business Critical and Hyperscale tiers. On Google Cloud, Cloud SQL has read replicas, and read pools: one address in front of up to twenty nodes. AlloyDB has read pools too. And Spanner adds read-only replicas in other regions.

When not to use replicas

When not to reach for replicas? When writes are your problem. Replicas don't take a single write off the primary. When every read must be the very latest. Then you're reading from the primary anyway, or paying for synchronous copies. And when a cache would do. Each replica is usually billed as a full database instance.

Remember

Let's recap. Writes go to the primary. Reads spread over the replicas. The copies are usually asynchronous, so measure the lag, and read your own writes from the primary. And a read replica isn't a failover plan, unless your database makes it one.

The full article, and what's next

Next time, rung six. Materialized views. Do the hard sum once, and keep the answer. See you there.

Read the article

Amazon RDS: Working with DB instance read replicas: the whole idea in writing, free.

Read it ↗