Roadmap › Microservices & System Design with .NET › Module: Data and Consistency
Lessons in this course (9)

6. CQRS and event sourcing

AdvancedMediumLesson 6 of 9

CQRS (Command Query Responsibility Segregation): separate the write model (commands, validation, domain rules) from the read model (queries, denormalised views shaped for screens).

text
Command → Handler → Domain model → Write DB ──(events)──> Projector → Read DB/view → Query handler → UI
  • Can be one database with separate code paths (most common) or separate stores.
  • Reads scale independently; read models can be rebuilt.

Event sourcing: store the sequence of events (OrderCreated, LineAdded, OrderShipped) instead of only current state; current state = replay of events. Gives a full audit trail and time travel, but adds complexity (versioning events, snapshots, projections). Tools: Marten (PostgreSQL), EventStoreDB, Cosmos DB change feed.

Interview tip: CQRS does not require event sourcing or MediatR. Say "we used CQRS-lite: commands through EF Core, queries through Dapper on views".