StoicBuilder

HandbookXIII the AqueductRead replicas

XIIIthe Aqueduct, Scale it

Read replicas

1 min readUpdated 28 November

In plain words

A read replica is a copy of your database that follows the primary's changes and answers read-only queries, taking load off the primary at the cost of being slightly behind it.

Where you see it

Harbor's usage reports and admin screens run heavy queries all day, so the team points them at a replica and frees the primary for checkouts and deploys. Then a customer renames a project, the page reloads from the replica, and the old name is still there for a moment.

lib/db.tsserver
const primary = new Pool({ connectionString: process.env.DATABASE_URL });const replica = new Pool({ connectionString: process.env.DATABASE_REPLICA_URL }); // reports can be a few seconds oldconst usage = await replica.query("select day, builds from usage_daily where account_id = $1", [accountId]); // a user reading their own change goes to the primaryconst project = await primary.query("select * from projects where id = $1", [projectId]);
Send reads that can tolerate a short delay to the replica. Reads that must see a change the user has made go to the primary.

What goes wrong

  1. Reading your own write from a replica

    The user saves, the next page reads from a replica that hasn't caught up, and their change seems lost. Send a user's reads to the primary for a few seconds after they write, or for that whole flow.

  2. Assuming lag is always small

    Replication lag is usually under a second, but a large migration or a long-running query on the replica can push it to minutes. Monitor lag, and stop sending traffic to a replica that falls too far behind.

  3. Using replicas to fix slow writes

    Replicas only take reads; every write still lands on the primary, and is replayed on every replica too. If writes are the bottleneck, look at indexes, batching, partitioning or sharding.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Route read-only queries that can tolerate a short delay, such as reports, search and admin lists, to the read replica, and keep everything inside a transaction or right after a user's own write on the primary. Add a metric for replication lag, and fall back to the primary when lag passes 5 seconds.

Quick check

Sign in to earn +10 XP for it

After Harbor moved project pages to a read replica, some users rename a project and see the old name until they refresh. What's the fix?

Got a prompt that touches read replicas?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.