Scaling Reads
When: read-heavy systems (feeds, product pages, profiles, URL redirects) — typically 10:1 to 1000:1 reads to writes.
Work down this ladder, cheapest first:
1. optimize the query indexes (composite, covering), avoid N+1, denormalize hot joins
2. cache CDN for static/public, Redis/Memcached for app data, in-process for tiny hot sets
3. read replicas scale reads horizontally; accept replication lag
4. precompute materialized views, fan-out-on-write timelines, search indexes
5. shard when the working set no longer fits one primary + replicas
- Cache invalidation: cache-aside with delete-on-write + TTL; versioned keys (
user:42:v7) avoid races. - Replica lag: route a user's reads to the primary for a few seconds after they write (read-your-writes), or carry a version/LSN token.
- Hot keys (a viral post): replicate the key across shards (
key#1..#N, read a random copy), in-process caches with short TTL, request coalescing so one miss fetches for many waiters. - Thundering herd on expiry: jittered TTLs, probabilistic early refresh, a lock so only one request recomputes.