Normalization vs denormalization
Overview
There are two opposite instincts in data design. One says: store every fact exactly once, so it can never be inconsistent. The other says: store data in the shape it’s read, so reads are fast. Both are right in different places. This lesson explains normalization, denormalization and how to choose.
The two approaches
Normalization
Normalization splits data into tables so that each fact lives in one place, linked by keys. The customer’s name lives in customers; orders refer to the customer by customer_id.
Benefits:
- No update anomalies: change the name once and every order shows the new name.
- Less storage and smaller rows.
- Integrity: constraints and foreign keys keep data valid.
Cost: reads that need data from several tables require joins, which get expensive on large tables, across shards or at very high read rates.
Denormalization
Denormalization deliberately duplicates or pre-computes data to make reads cheap:
- Copied fields: store
customer_nameon the order orauthor_nameon the post. - Counters:
like_count,comment_count,follower_count. - Pre-joined or pre-computed views: a user’s feed stored ready to read; daily sales totals.
- One table per query: common in wide-column stores (next lesson).
Cost: writes do more work, data takes more space, and copies can become inconsistent if an update is missed.
Keeping copies correct
Every denormalized field needs an answer to “what updates it, and when?”:
| Strategy | How | Consistency |
|---|---|---|
| Same transaction | Insert the comment and increment comment_count together |
Always consistent (single database) |
| Database feature | Triggers, materialized views, generated columns | Consistent or refreshed on a schedule |
| Events + background job | Publish “author renamed”; a worker updates copies | Eventually consistent |
| Periodic rebuild | Recompute nightly from the source of truth | Stale between runs |
A good habit: keep the normalized data as the source of truth and treat every denormalized structure as derived data that can be rebuilt from it. If a counter drifts, a repair job can recount.
Some copies are features, not shortcuts
Sometimes copying is correct rather than an optimisation. An order should store the price and address at the time of purchase. If the product price changes next week, past invoices must not change. That’s a snapshot, not a stale copy.
How to choose
Lean towards normalization when:
- data changes often and must be correct everywhere (balances, inventory),
- write volume is high and reads are moderate,
- you’re early and access patterns are still changing.
Lean towards denormalization when:
- reads vastly outnumber writes (feeds, product pages, profiles),
- the copied data changes rarely (names, avatars),
- joins would cross shards or services,
- a slightly stale value is acceptable.
Most production systems combine them: a normalized relational core plus caches, counters, search indexes and read-optimized tables built from it.
In an interview
When you add a counter or a copied field, say how it stays correct: “I’ll keep like_count on the photo, incremented atomically when a like is inserted; if it drifts, a nightly job recounts.” That one sentence shows you understand the trade-off.
Recap
Normalization stores each fact once and joins at read time; denormalization copies and pre-computes data for fast reads at the cost of more complex writes. Keep a normalized source of truth, denormalize where reads dominate and staleness is acceptable, and give every copy a clear update strategy.
Copy or join?
A blog platform shows a post page with the post, the author's name and avatar, the number of comments, and the latest 3 comments. The post page gets 50,000 views per second; authors rarely change their name; comments are added about 200 times per second across the site. For each piece of data, decide whether to read it via a join/query at request time or to store a denormalized copy, and explain how the copy stays correct.
Author name and avatar:
Comment count:
Latest 3 comments:Author name and avatar: denormalize (copy onto the post, or more simply cache the
author record). Names change rarely, so update copies asynchronously when an author
edits their profile (an event triggers a background job). Brief staleness is fine.
Comment count: store a pre-computed comment_count on the post. Increment it in the
same transaction as inserting the comment (or with an atomic increment). Counting
rows on every one of 50,000 views/second would be very expensive.
Latest 3 comments: either query comments by (post_id, created_at DESC) LIMIT 3 with
an index and cache the result for a few seconds, or keep a small "latest comments"
list on the post/in Redis that is updated on each new comment. With 200 comments/s
sitewide and 50k views/s, caching or a stored list both work.
The source of truth stays normalized (authors, posts, comments tables); the copies are
optimisations that can be rebuilt.Summary
- Normalization stores each fact once and links data with keys, avoiding update anomalies.
- Denormalization copies or pre-computes data where it is read, trading write effort and storage for fast reads.
- Common forms: copied fields, counters, materialized views, read-optimized tables and caches.
- Every copy needs an update strategy: same transaction, events and background jobs, or periodic rebuilds.
- Keep a normalized source of truth and treat denormalized data as derived and rebuildable.