Menu

Earn Premium with Referrals

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

See how it works and start inviting friends.

CQRS Overview
HLD

CQRS Overview

Splitting writes from reads into separate models — why the asymmetry of workloads demands different shapes.

The Workload Asymmetry

 most systems: reads DWARF writes (100:1 to 1000:1)
 and their OPTIMAL SHAPES differ:

 WRITES want:
   normalized data, constraints, small rows, correctness first

 READS want:
   denormalized views, pre-joined, shaped EXACTLY per query

 one table shape serving both = compromise everywhere.

The Split

        COMMANDS                    QUERIES
   (change state)              (answer questions)
        │                           │
   ┌────▼─────┐    events      ┌───▼────────┐
   │ write    │ ─────────────► │ read models │
   │ model    │  projection/   │ - search idx│
   │ (normal- │  CDC/sync      │ - dashboards│
   │ ized DB) │                │ - api views │
   └──────────┘                └─────────────┘

 commands NEVER query read models for validation decisions;
 queries NEVER touch the write store.
 each side scales, schemas, optimizes independently.

What Each Side Looks Like

AspectCommand sideQuery side
Data shapenormalized, constraineddenormalized per view
Consistencystrong, transactionaleventually synced
Scalingvertical/sharded carefullyreplicas, caches, CDNs
Schema changescareful migrationsrebuild projections freely
Tech examplespostgres, mysqlelasticsearch, redis, read replicas
 the sync between them:
 same-database replicas (simplest CQRS!) →
 CDC pipelines → event-driven projection updates.
 a spectrum of lag vs complexity — pick per view.

The Full Spectrum (don’t max out by default)

 LEVEL 0: single database, both roles         ← most apps!
 LEVEL 1: primary + read replicas             ← very common ✓
 LEVEL 2: specialized read stores per need
          (search → elastic; counters → redis)
 LEVEL 3: full CQRS+ES with event-sourced write side

 jump levels ONLY when measurements demand it:
 level 2+ costs ops surface, eventual-consistency UX,
 double schema maintenance. the ladder applies here too.

When CQRS Earns Its Complexity

 □ genuinely divergent query shapes (search + transactions +
   analytics on same domain)
 □ independent scaling curves (reads exploding past writes)
 □ event sourcing present anyway (projections are natural)
 □ team boundaries align with the split

 skip when:
 ✗ standard CRUD with moderate traffic
 ✗ team too small to operate sync pipelines
 ✗ "we might scale someday" (speculative — YAGNI's home turf)

Interview Framing

“Product catalog: heavy search traffic, strict inventory writes” scored shape: recognize workload split, propose write-model (postgres, transactional inventory) + read-models (elastic for search; cache for detail pages), sync via CDC, staleness handling for UI (“added-to-cart revalidates against write store”). Naming Level-1-as-starting-point shows you climb ladders only as needed — the calibrated answer interviewers wait for.

My Private Notes

Notes are auto-saved locally to this device.