Part 3 of 10
Normalization, denormalization, and joins
Each recipe has a cuisine, like "Thai". Store the text, or an ID pointing to one row in a cuisines table, picked from a dropdown? A fixed list gives consistent spelling, no mix-ups between same-named things, one place to rename, easy translation, and better search (the list can record that Thai is a Southeast Asian cuisine).
Normalized: each human-readable value is stored once, and everything else points to it by an ID that only has meaning inside the database. Denormalized: the text is copied into every record that uses it. An ID means nothing to people, so it never needs to change. Copied text can change, and then every copy needs updating: more code, more writes, more disk, and a risk of copies disagreeing.
The cost of normalizing: showing "Thai" on screen needs an extra lookup to turn the ID into text. In SQL that lookup is a join, like recipes joined to cuisines on cuisine_id. Document stores can do either, but they lean toward copying: JSON makes extra fields easy, and weak joins make normalizing a chore. Some have no joins, so the app fetches a document and then queries again by ID. MongoDB can join with $lookup in its aggregation pipeline.
Which to pick
Guess first
A publisher renames itself, and its name is copied into 10,000 recipes. How many records must change?
If each recipe stores its publisher's name as plain text (denormalized), nothing links recipes from one publisher, and adding a logo gets hard.
- Denormalized: put the logo link in every recipe, so each record stands alone but a new logo means touching thousands of records.
- Normalized: a publisher record holds name, logo, description, and news feed, and recipes point to its ID. Update once.
| Style | Writes | Reads |
|---|---|---|
| Normalized | Fast, one copy | Slower, needs joins |
| Denormalized | Costly, many copies, more disk | Fast, fewer joins |
Copied, or stored once
Step 1 of 4
Two ways to store each recipe's publisher. On top, the name is copied into every recipe. Below, recipes hold the ID 7, and the name lives in one publisher record.
A denormalized copy is a kind of derived data, and some process must keep copies in step. If it crashes halfway, copies can disagree. Atomic transactions help, but not every database can update several documents atomically; stream processing is another option (chapter 12).
Normalized data suits OLTP, where reads and updates must both be quick. Analytics often prefers denormalized data, since updates come in bulk and most queries only read. At small and medium scale, normalized is often best: nothing to sync, and joins are cheap enough. At very large scale, joins can start to cost too much.
Hydrating IDs in a social feed
Chapter 2's social network had a normalized schema plus cached timelines. A timeline is a stored copy of the posts-and-follows join, and fan-out keeps it current.
Real timelines, like X's, store only IDs: the post ID, the poster's ID, and a little extra for reposts and replies, not the post text. So reading a timeline still needs two lookups:
- Post ID to the text and stats, like likes and replies.
- Poster ID to the profile, like name and photo.
Looking up readable data by ID is hydrating the IDs: a join done in app code.
Why only IDs?
- The data changes fast (like counts change every second, and people change names and photos), and timelines should show the latest.
- Copying everything would raise storage cost a lot.
The lesson: read-time joins do not stop a system from being fast and scaling. Hydration runs in parallel, and its cost does not grow with follow or follower counts. Deciding what to copy is not obvious. The best design often normalizes some things and copies others, based on how often each changes and what reads and writes cost, sometimes driven by a few extreme users. Neither choice is good or bad; each is a trade-off.