Skip to main content
PreparationZone Logo
PrepZone

CQRS and Event Sourcing

Separate read and write models with an append-only event log as the source of truth.

Pass checkpoint below

Why this matters

  • StreamHub's creator analytics dashboard needs denormalised read models (views per hour, revenue by region) while the write path handles subscriptions and payments with strong consistency.
  • CQRS lets you scale reads and writes independently — a read-heavy dashboard does not slow down payment processing.
  • Event sourcing provides a complete audit trail: replay events to rebuild state, debug production issues, or migrate to a new schema.

CQRS + event sourcing components

  • Command handler — validates and executes business logic; emits domain events.
  • Event store — append-only log (Kafka, EventStoreDB) holding every state change.
  • Projections — consumers that build read models from the event stream.
  • Read store — denormalised views optimised for queries (Elasticsearch, Redis, materialised views).
  • Snapshots — periodic state checkpoints to avoid replaying millions of events on startup.

Architecture

CQRS on AWS

writeCDCqueryCOMPUTE
Command APIEKS
DATABASE
RDS write mod…normalised
INTEGRATION
Amazon MSKdomain events
ANALYTICS
OpenSearchread projection
CLIENT
Dashboardquery API
Commands write RDS; MSK events update OpenSearch read projections.

Command and event flow

Java
// Command: user subscribes to a creator
{
  "command": "SubscribeToCreator",
  "user_id": "usr_42",
  "creator_id": "crt_99",
  "tier": "premium",
  "idempotency_key": "sub_8f2a_20260401"
}
Java
// Event emitted to event store
{
  "event_type": "SubscriptionCreated",
  "aggregate_id": "sub_8f2a",
  "version": 1,
  "timestamp": "2026-04-01T12:00:00Z",
  "payload": {
    "user_id": "usr_42",
    "creator_id": "crt_99",
    "tier": "premium",
    "amount_cents": 999
  }
}
Java
// Projection updates read model
{
  "projection": "creator_subscriber_count",
  "creator_id": "crt_99",
  "action": "increment",
  "new_count": 14823
}

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • When to use CQRS
  • Event store with Kafka
  • Snapshots for performance

Test yourself

Answer these before moving on — recall is what makes it stick.