Data Modeling & Schema Design

Denormalization Trade-offs

Denormalization deliberately stores derivable or repeated data to improve a measured read path. It trades read simplicity or latency for write amplification, consistency machinery, storage, and more complicated repair.
  • Denormalization begins with a correct normalized source of truth.
  • A performance hypothesis must name the query, target, and evidence.
  • Every duplicate needs a freshness and failure contract.
  • Snapshots can be semantically correct rather than denormalized.
  • Materialized views and summary tables centralize derivation but still consume storage and refresh capacity.
  • The operational design includes backfill, reconciliation, idempotency, observability, and rollback—not only an extra column.

Choosing how to serve order totals

Trade-offs for `order_total = Σ(quantity × unit_price)`
DesignRead pathWrite and consistency costBest fit
Compute from order_itemsAggregate at read timeSingle source; no duplicate maintenanceModerate line counts or infrequent reads
Stored orders.total_amountDirect header readEvery line mutation must update total atomicallyHot read path with tightly controlled writes
Materialized aggregateRead precomputed resultRefresh work and possible stalenessReporting or bounded-lag feeds
Cache outside schemaFast keyed lookupInvalidation, eviction, and cold missesDisposable acceleration where database remains authoritative
Reconciliation query for a stored total
SELECT o.order_id,
       o.total_amount AS stored_total,
       COALESCE(SUM(i.quantity * i.unit_price), 0) AS derived_total
FROM orders AS o
LEFT JOIN order_items AS i ON i.order_id = o.order_id
GROUP BY o.order_id, o.total_amount
HAVING o.total_amount <> COALESCE(SUM(i.quantity * i.unit_price), 0);
A denormalized value needs a routine way to detect and repair drift.