Precompute joins
A hot read path needs a multi-table join every time.
Say it like this
The home timeline is read a thousand times more than it is written, so I denormalise: each post is copied with its author name and avatar into the per-user feed row at write time, and the read is a single range scan with no joins.
Basic solution
Store a copy of the joined result (denormalise) or maintain a materialised view updated on write.
Write once normalised, copy into the read model
The post is written once to the source table, then copied with author fields into each follower feed. The feed read is a single range scan; a profile change triggers a fix-up job on the copies.
How it works
- Identify the hot read and the join it needs. A feed row needs the post text, the author's handle and avatar, and the like count, which live in three different tables.
- Decide what to copy into the read model: the fields the page shows and that change rarely (author handle, avatar URL, post text). Leave out fields that change constantly (like count), because every copied field has to be fixed up when it changes.
- On the write path, write both: insert the post into the posts table, then append a denormalised copy (post plus the copied author fields) into each consumer's read table. Do both inside one transaction if they share a database, or publish an event that a consumer turns into the copies if they do not.
- Serve the hot read from the read model alone: one table, one key, one range scan that returns rows already carrying everything the page needs.
- When a copied field changes (a user renames their handle), a background job finds every copy and rewrites it. For fields that change rarely, an alternative is to store a reference and resolve it from a cache at read time so there are no copies to fix.
- Keep the normalised tables as the source of truth. Because every copy was derived from them, the read model can be deleted and rebuilt from them at any time.
The rule
- Normalise for writes and correctness; denormalise the specific read paths that dominate traffic, and make every copy rebuildable.
Worked example
Home timeline for the news feed
- 300 million daily active users, each loading the feed 10 times, so 3 billion feed reads per day, about 35,000 per second average and 150,000 at peak. Each feed page shows 20 posts.
- Normalised read: 1 query for 20 post ids, 1 query for 20 posts, 1 query for up to 20 authors, 1 for like counts. Four round trips of 2 to 5 ms each, 10 to 20 ms per page, and 600,000 database queries per second at peak.
- Denormalised read: a feed_item table keyed by (user_id, created_at desc) holding post_id, text, author_handle, author_avatar_url. One range scan returns 20 rows in about 2 ms, 150,000 queries per second at peak, a 4x cut in query count and a 5x cut in latency.
- Write cost: a post from a user with 500 followers becomes 500 feed_item rows of about 400 bytes, 200 KB per post. At 6,000 posts per second that is 3 million row writes per second, which is why the copies go to a wide-column store built for appends.
- Storage: 300M users times 800 cached items times 400 bytes is about 96 TB, trimmed by capping the feed length and expiring items older than 30 days.
- Handle changes: about 50,000 per day. A background job rewrites that user's copies in followers' feeds, about 500 rows each, 25 million row updates per day, negligible against 3 million writes per second.
Variants and when each wins
Copy fields into the read table
- The read table carries copies of the fields from the other tables. It is the simplest form and wins for immutable content; write amplification equals the number of copies, and any change to a copied field needs a fix-up job.
Materialised view
- The database maintains a precomputed join, refreshed on a schedule or incrementally. It wins for reporting and dashboards where moderate freshness is fine; most databases cannot refresh it per row at feed scale.
Read model in a different store
- Events from the write store build a document in Elasticsearch or a row in a wide-column store shaped exactly for the page. It wins when the read pattern needs a different engine from the write pattern, and it is eventually consistent.
Hybrid: ids in the read model, bodies from a cache
- The read model holds only post ids; bodies and author profiles come from a cache keyed by id in one batched fetch (MGET). It wins when authors edit profiles, because an update changes one cache entry rather than millions of copies, at the cost of one extra batched round trip.
Recommendation: the hybrid for feeds where authors edit profiles, full copies for immutable content such as chat messages, materialised views for dashboards.
Failure modes
Copies drift from the source
- You notice an old avatar still showing in some feeds days after the user changed it. It happens because the fix-up job that rewrites copies failed, or was never written. Fix it with an idempotent, retried job that rewrites copies on every change, plus a periodic reconciliation that samples copies against the source and repairs mismatches.
Write amplification outgrows the store
Transaction spans two stores
- You notice a post exists but is missing from some feeds after a crash. It happens because the post insert committed in one store and the feed copies in another store did not. Fix it by writing the post, then publishing an event through an outbox table in the same transaction; a consumer builds the feed copies and retries until every one is done.
Schema change touches every copy
- You notice adding one field to the feed item means backfilling 96 TB. It happens because the read model stores the full shape of every post. Fix it by keeping rarely used fields out of the copy and resolving them at read time, and by versioning the row shape so old and new rows coexist.
Reads of deleted content
- You notice a deleted post still appears in feeds. It happens because the copies are never removed. Fix it by checking the source for a tombstone at read time (hydration) and dropping the row, and by cleaning copies lazily in the background.
Interviewer follow-ups
Answer out loud before you open each one.
How do you decide which fields to copy?
Copy what the page always shows and what changes rarely: post text, author handle, avatar URL. Leave out what changes constantly or is cheap to fetch in a batch: like counts, follow state. Every copied field is a future fix-up job, so copy the minimum that removes the join.
What is the difference between denormalisation and caching?
A cache holds a temporary copy with a TTL and can be dropped; a denormalised read model is a durable table that the application maintains on every write and treats as a first-class store. Caches tolerate misses; read models must be complete, which is why they need rebuild and reconciliation jobs.
How do you rebuild a read model after a bug corrupts it?
From the normalised source of truth: replay the posts and follow graph through the same fan-out code into a fresh table, then swap it in. This is why the source tables stay normalised and why the fan-out must be deterministic and idempotent.
Where does denormalisation stop being worth it?
When the write amplification costs more than the reads it saves, which happens for high fan-out writers, or when copied fields change so often that fix-up traffic rivals the original writes. At that point keep ids in the read model and hydrate bodies from a cache.
Connections
- In study paths
- Feeds and fan-out
- Pairs with
- Precompute on write, read cheaply · Append-heavy, time-ordered rows · Publish events reliably with the write · React to database changes · Know your write amplification
- Pulls against
- Default to a relational database
- Used in
- News Feed (Twitter / X timeline) · Chat App (WhatsApp / Messenger)
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Post service, Feed service, Fix-up job | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon Aurora | posts (source), profiles (source) | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| Amazon DynamoDB | feed_item (read model) | Firestore, Bigtable | Cosmos DB | ScyllaDB, Apache Cassandra |