Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Denormalization at Scale
HLD

Denormalization at Scale

When distributed databases force redundancy — copies as the price of single-shard reads.

Scale Changes the Math

 on ONE node: denormalization is optional (joins are cheap-ish).
 across SHARDS: joins become scatter-gather disasters.

 "order with customer name":
   normalized:  orders(shard by order) JOIN users(shard by user)
   → cross-shard join → app merges two queries → latency + complexity

   denormalized: orders row CARRIES customer_name snapshot
   → single-shard read. done.

 AT SCALE, DENORMALIZATION STOPS BEING AN OPTIMIZATION
 AND BECOMES THE ARCHITECTURE.

The Distributed Denormalization Catalog

 1. READ-TIME SNAPSHOTS (write-time copies)
    orders.customer_name ← copied at purchase
    immutable-ish facts: perfect candidates

 2. WRITE FAN-OUT (Cassandra's native mode)
    same trip written to trips_by_driver AND trips_by_rider
    tables — each query pattern gets its own co-located copy

 3. AGGREGATE COLUMNS maintained incrementally
    users.trip_count ← INCR on trip completion
    (atomic counters per shard make this cheap)

 4. DERIVED DOCUMENTS via CDC
    postgres truth → stream → build fat read-models in
    elasticsearch/dynamo — full pages as single documents

The Consistency Ledger

 every copy needs its update story AND staleness budget:

 copy                    mechanism         staleness
 ────────────────────────────────────────────────────
 customer_name display   CDC stream        seconds
 trips_by_rider          write fan-out     ZERO (same txn)
 trip_count badge        atomic INCR       zero, but lossy-crash edge
 search index            CDC pipeline      minutes

 fan-out writes are the STRONGEST option: both copies updated
 in one local transaction when they share a shard design.
 CDC copies are looser but decouple schemas.

Fan-Out Write Economics

 one logical trip → N physical writes:

 trip → trips_by_rider + trips_by_driver + trips_by_city_day
      = 3× write amplification

 budget honestly:
 - storage ×N
 - write IOPS ×N  
 - consistency surface ×N
 
 worth it? each copy serves a hot query at single-shard speed.
 NOT worth it for cold patterns — those go to the warehouse.
 measure query frequency before multiplying writes.

When NOT to Denormalize Even at Scale

 ✗ rapidly-changing copied fields (status churn = sync storm)
 ✗ fields needing TRANSACTIONAL truth (balances!)
 ✗ speculative copies ("might need it someday")
 
 keep a NORMALIZED core of record; denormalize outward
 into read-optimized structures. truth stays small and clean;
 speed lives in derived copies that can be rebuilt.

Interview Framing

Sharded designs get probed here: “show user profile with last 10 trips” scored answer: recognize cross-shard shape, choose write-time snapshot or fan-out table with staleness statement, mention write-amplification cost consciously. The phrase “normalized core, denormalized edges” captures the mature position in four words.

My Private Notes

Notes are auto-saved locally to this device.