Architecture Intermediate
Read Replicas — Scaling Reads¶
Read ReplicasDatabaseScaling 3 min read
Scaling databases with read replicas. Master-slave replication and routing.
Principle¶
80-90% of operations are reads. Read replicas distribute read load across copies of the master DB.
Application Routing¶
const master = new Pool({ host: 'master-db' });
const replica = new Pool({ host: 'replica-db' });
class Database {
async query(sql, params, opts = {}) {
const pool = opts.readOnly ? replica : master;
return pool.query(sql, params);
}
}
await db.query('INSERT INTO orders ...', [data]); // → master
await db.query('SELECT * FROM orders', [], { readOnly: true }); // → replica
Summary¶
The simplest way to scale reads. Watch out for replication lag — critical reads after writes should go to the master.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.