Course contentsShow
← Software Engineering
Lesson 1352 of 1,93340. Event-Driven ArchitectureFree lesson

Query Side and Projections

Build read models by projecting events into denormalized views optimized for specific query patterns.

Query Side and Projections

You'll learn how to build efficient read models by projecting events into denormalized views tailored for specific query needs.

The Core Idea

In CQRS, the query side is entirely separate from the command side. While the command side writes events to an event store, the query side reads those events and builds projections—denormalized, read-optimized data structures designed for specific query patterns.

Think of projections like movie theaters showing the same film at different times for different audiences. The original film (your event stream) stays unchanged, but each showing (projection) serves a specific need—subtitles for one audience, 3D for another.

Why Projections Matter

Traditional databases force you to choose one structure that serves both reads and writes. With projections, you can:

  • Create multiple specialized views from the same event stream
  • Optimize each view for its specific query pattern (sorted lists, aggregates, searches)
  • Store the same data in different formats (relational, document, graph)
  • Rebuild projections anytime by replaying events

Building Projections

A projection handler listens to events and updates its read model accordingly:

When OrderPlaced event occurs:
  → Add order to "Orders by Customer" view
  → Update "Monthly Sales Total" view
  → Append to "Recent Orders" view

When OrderCancelled event occurs:
  → Remove from active orders
  → Update sales totals

Each projection maintains its own storage, completely independent of the event store and other projections.

The Trade-off

Projections introduce eventual consistency—your read models update shortly after events occur, not instantly. The command side writes immediately, but queries reflect reality "eventually" as projections catch up.

Key Takeaway: Query-side projections transform events into purpose-built read models, allowing you to optimize each view for its specific query pattern without compromising the authoritative event store.