Scaling reads with replicas

Performance and scaling Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Pick the fix

After saving a profile, users sometimes see their old name. Reads go to replicas. Fix?

Options
Hint 1 of 1

Why is the value old?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Read from the primary for that user for a short window after their write

Correct. Read-your-writes without moving all reads.

Replicas apply changes a little after the primary, so a read right after a write can miss it. Routing that user's reads to the primary for a few seconds gives read-your-writes cheaply.

Why not the others?
  • B Add more replicas. More replicas, same lag.
  • C Cache the old value longer. Makes it worse.
  • D Disable replication. Loses the read scaling.

30-second answer

Replicas stream the primary's changes and serve read-only queries, moving read load off the primary and giving failover candidates. The catch is replication lag: a user who just wrote may read an old value from a replica. Handle it by sending a user's reads to the primary for a short time after they write (read-your-writes), routing reads that need fresh data to the primary, or waiting until the replica has replayed the write's log position.

Practise saying it · 30 s. No recording, just a timer

Read replicas.

Say your answer out loud, then check which of these points you covered:

  • Offload reads
  • Replication lag
  • Read-your-writes routing
  • Monitor lag
  • No write scaling

Deep dive

PostgreSQL streaming replication is asynchronous by default; synchronous mode trades write latency for zero lag on commit.

Monitor lag with pg_stat_replication and drop lagging replicas from rotation.

Long queries on replicas can conflict with replay; hot_standby_feedback trades bloat on the primary for fewer cancellations.

Replicas don't scale writes.

Common mistakes

  • Reading from a replica right after a write in the same flow.
  • No lag monitoring.
  • Expecting replicas to fix write load.

Senior take & follow-ups

Benefits, lag problems, then routing strategies and monitoring.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you give read-your-writes?

    Show hint

    Route that user to the primary for a few seconds after a write, or wait for the replica's LSN.

  2. Do replicas help writes?

    Show hint

    No.

Suggest a fix if something here is wrong or out of date.