Denormalization

Difficulty: Intermediate

Question

When is denormalization a good idea, and what do you give up when you denormalize a schema?

Answer

Denormalization means deliberately storing redundant or pre-computed data so that common reads avoid expensive joins or aggregations. It is a good idea when a workload is read-heavy, a specific query is measurably slow because of joins or counts, and you have a reliable way to keep the duplicated data in sync. You trade faster reads for more complex writes and a risk of inconsistent data.

What denormalization looks like in practice

Common patterns, from mildest to most aggressive:

  • Copying a column: storing restaurant_name on the orders table so the order history page does not join restaurants.
  • Counter or summary columns: posts.comment_count or users.total_spent_paise instead of running COUNT(*) or SUM() on every page load.
  • Snapshot data: storing price_at_purchase on order_items. This is often required for correctness, because the product price will change later.
  • Pre-joined read tables: a product_listing table or materialized view that flattens product, seller, rating and stock into one row for search pages.
  • Embedded documents: in MongoDB or DynamoDB, putting the shipping address inside the order document because they are always read together.

When it is the right call

  • The read to write ratio is high, such as product catalogues, feeds, or dashboards.
  • Profiling (EXPLAIN ANALYZE, slow query logs) shows a join or aggregation is the bottleneck, and indexes alone did not fix it.
  • The duplicated data changes rarely. A restaurant name changes far less often than orders are read.
  • You need historical snapshots. An invoice must show the GST rate and price at the time of sale, not today's values.
  • In analytics, star schemas intentionally denormalize dimension tables because queries scan huge fact tables and joins are costly.

What you give up

  • Write amplification. Renaming one restaurant may mean updating millions of order rows, or accepting that old rows show the old name.
  • Update anomalies. If one code path forgets to update the copy, the two values disagree, and nothing in the database will tell you.
  • Storage. Usually minor, but wide repeated text columns add up.
  • More complex code. Every write must now update multiple places, ideally in the same transaction.

Ways to keep copies in sync

TechniqueConsistencyNotes
Same transaction in app codeImmediateSimple but easy to miss in one code path
Database triggerImmediateCannot be bypassed, but hidden logic
Materialized viewStale until refreshGood for dashboards
Change data capture (Debezium, Kafka)EventualCommon for search indexes and read models
Scheduled batch jobEventualFine for nightly reports

Common mistakes

  • Denormalizing before measuring. Normalize first (to roughly 3NF), add proper indexes, then denormalize the specific hot path.
  • Treating the copy as a source of truth. Always know which table is authoritative so you can rebuild the copy.
  • Forgetting concurrency on counters. UPDATE posts SET comment_count = comment_count + 1 is safe; reading the count in the app, adding one and writing it back is not.
Normalized vs denormalized order history. The same order history page served from two schema designs.
NormalizedDenormalized
Order page queryJoin orders, restaurants, usersSingle table read
Restaurant renameUpdate one rowUpdate many rows or accept stale names
Risk of inconsistencyLowHigher, needs sync logic
Best forWrite-heavy, OLTP core dataRead-heavy pages and analytics

Code examples

Keeping a counter column in sync inside one transaction

BEGIN;

INSERT INTO comments (post_id, user_id, body)
VALUES (77, 12, 'Great article');

-- Atomic increment: no read-modify-write race
UPDATE posts
   SET comment_count = comment_count + 1
 WHERE id = 77;

COMMIT;

-- The feed now reads one column instead of COUNT(*)
SELECT id, title, comment_count FROM posts ORDER BY created_at DESC LIMIT 20;

Both writes commit together, so the counter cannot drift from the comments table unless some other code path skips the update.

Key points

  • Denormalization trades write complexity and consistency risk for faster reads
  • Normalize first, measure, then denormalize only proven hot paths
  • Snapshot columns like price_at_purchase are often needed for correctness, not just speed
  • Always define which table is the source of truth so the copy can be rebuilt
  • Sync options: same transaction, triggers, materialized views, CDC, batch jobs

Common follow-up questions

  • How would you keep a denormalized search index in sync with the primary database?
  • Is storing price_at_purchase on order items denormalization? Why or why not?
  • How does denormalization differ in a document database like MongoDB?
  • What is a star schema and why is it denormalized?

Interview tips

  • Anchor your answer in a concrete example, such as a food delivery order history page
  • Say you would measure with EXPLAIN ANALYZE before denormalizing; it shows engineering judgement
  • Always mention how you would keep the copy consistent, since that is the real cost

Concepts covered

Denormalization, Read-heavy Workloads, Redundancy, Write Amplification, Materialized Views

More Database Design questions