Difficulty: Intermediate
Your database is overloaded with reads. When would you add a read replica, and when would you put a cache like Redis in front of it instead?
Use a read replica when you need to offload arbitrary SQL reads (filters, joins, reports, search pages) that are hard to cache and must stay close to the source of truth. Use a cache like Redis when a small set of hot, repeatedly requested results (a product page, a user session, a config blob) dominates traffic and can tolerate being served from memory with explicit invalidation. Many production systems use both: Redis for the hottest keys, replicas for everything else.
A read replica is a full copy of the database that applies the primary's change stream (PostgreSQL WAL, MySQL binlog). Your app sends read-only queries to it. It can answer any query the primary can, with indexes and joins, but it costs as much as a database server and is still bound by disk and query execution speed.
A cache stores the result of a computation keyed by something simple, such as product:123 or feed:user:42:page:1. A Redis GET is a sub-millisecond in-memory lookup, so one Redis node can serve far more simple reads than a database. But it can only serve what you explicitly put in it, under keys you can predict.
| Aspect | Read replica | Redis cache |
|---|---|---|
| What it can serve | Any SQL query | Only precomputed keys |
| Freshness | Replication lag, usually milliseconds to seconds | Until TTL or invalidation |
| Consistency work | Handle read-your-writes | Handle invalidation and stampedes |
| Best for | Varied queries, reports, search, listings with filters | Hot objects, sessions, counters, rate limits |
| Scaling cost | A full DB instance per replica | Cheap memory, very high QPS |
Replicas: replication lag breaks read-your-writes. A user updates their profile, the next request reads from a replica that has not caught up, and they see old data. Fixes include reading from the primary for a short window after a write, routing a user's reads to the primary when their session has a recent write, or waiting for the replica to reach a known LSN. Replicas also do not help write load at all.
Caches: invalidation is the hard part. Prefer cache-aside with delete-on-write rather than update-on-write to avoid races, set TTLs as a safety net, and protect against stampedes (many requests missing the same key at once) with request coalescing or a short lock. Never treat the cache as the source of truth unless you have designed for it.
import { Redis } from "ioredis";
import { Pool } from "pg";
const redis = new Redis(process.env.REDIS_URL!);
const primary = new Pool({ connectionString: process.env.PRIMARY_URL });
const replica = new Pool({ connectionString: process.env.REPLICA_URL });
export async function getProduct(id: number) {
const key = "product:" + id;
const cached = await redis.get(key);
if (cached) return JSON.parse(cached);
// miss: read from replica, not primary
const { rows } = await replica.query("SELECT * FROM products WHERE id = $1", [id]);
if (rows[0]) await redis.set(key, JSON.stringify(rows[0]), "EX", 300);
return rows[0] ?? null;
}
export async function updatePrice(id: number, pricePaise: number) {
await primary.query("UPDATE products SET price_paise = $1 WHERE id = $2", [pricePaise, id]);
await redis.del("product:" + id); // delete, do not overwrite
}
Reads try Redis first and fall back to a replica, so the primary only handles writes. On update the key is deleted rather than rewritten, which avoids races where an older value overwrites a newer one.
Read Replicas, Redis Cache, Replication Lag, Cache Invalidation, Read Scaling