الانتقال إلى المحتوى
.NETCQRS· 7 دقائق للقراءة

CQRS Is Not Event Sourcing (and Doesn't Need Two Databases)

CQRS splits one model into a command side and a query side. Separate stores, messaging and event sourcing are optional steps with their own costs.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

The ticket said the order history page was slow. The design that came back was labeled "CQRS": an event store for orders, a message broker, a separate read database, and projectors to keep it in sync. It took a quarter to build. The page is fast now, and support has a new kind of ticket: "I cancelled my order and it still says Pending." The read database catches up moments later, but the customer has already taken a screenshot.

The common misconception

The belief: doing CQRS means event sourcing, a write database plus a read database, a message bus between them, and eventual consistency. Without those, it isn't really CQRS.

That picture didn't come from nowhere. The people who popularized CQRS also popularized event sourcing, and conference talks and sample apps usually show the two together. Most diagrams have two databases with arrows between them. Microsoft's Azure guide has sections on separate stores and on combining CQRS with event sourcing, and Greg Young himself notes that most query sides can be eventually consistent. Put together, the full stack looks like the definition.

The half-truth: those are things CQRS makes possible, not things it requires.

What actually happens

CQRS is one split: two models where there was one

Greg Young, who introduced the pattern, traces it to Bertrand Meyer's Command Query Separation: a method either changes state or returns data, never both. CQRS applies that idea at a larger scale: where one object handled both, you get two, one with the commands and one with the queries. In his CQRS Documents, a CustomerService becomes a CustomerWriteService and a CustomerReadService. Martin Fowler's description matches: separate models for update and display, which may share a database or use separate ones.

The read side can query the same database

Young calls it a thin read layer: it reads straight from the database and projects DTOs, bypassing the domain model. Microsoft's Azure guide calls separate models over a single data store the foundational level of CQRS, and the ordering service in Microsoft's eShopOnContainers reference app uses exactly that: one database, two logical models. In EF Core terms, commands load an entity, call its methods and save; queries project straight into DTOs with Select, which tracks nothing because the result contains no entities.

Separate stores are an optional, expensive step

A second store lets you shape and scale reads independently, with a denormalized view or a document database. It also has to be kept in sync, usually by publishing events from the write side. Azure's guide lists what comes with that: the database update and the event can't share a transaction, so it recommends a transactional outbox and idempotent consumers, and reads become subject to , so users may not see their own change right away. Young warns that synchronizing models can become very expensive.

Event sourcing is a different pattern

changes how state is stored: an append-only list of events per entity, with the current state rebuilt by replaying them. An event store can only be read stream by stream, so event-sourced systems typically build separate read models, which is why the two patterns travel together. The dependency runs one way: event sourcing leans on CQRS, while CQRS works fine over ordinary tables. Microsoft is blunt about the cost: event sourcing is complex, costly to migrate to or from, and for most systems traditional data management is enough.

The fix: split the code, not the database

C#
using Microsoft.EntityFrameworkCore;public sealed class CancelOrderHandler(ShopDb db, IEventBus bus){    public async Task Handle(int orderId, CancellationToken ct)    {        var order = await db.Orders.SingleAsync(o => o.Id == orderId, ct);        order.Cancel();        await db.SaveChangesAsync(ct);        // A second write, outside the transaction: if it fails, reads never learn        await bus.PublishAsync(new OrderCancelled(orderId), ct);    }}public sealed class OrderListQuery(IReadStore reads){    // A copy that a projector updates later, so it can miss the user's change    public Task<List<OrderRow>> Run(int customerId, CancellationToken ct) =>        reads.OrdersFor(customerId, ct);}

Both versions separate commands from queries, so both are CQRS. The first is wrong as a starting point: it pays for a broker, a projector and a second store before anything showed they were needed. The second does the split with the database you already have. The command side still enforces that shipped orders can't be cancelled, and the query side reads three columns without touching the domain model. In a test with EF Core 10 and SQLite, the list query selected only Id, PlacedAt and Status, the context tracked no entities afterwards, and a cancelled order showed as cancelled on the very next query.

MythReality
CQRS means event sourcingThey're separate patterns; event sourcing leans on CQRS, not the other way round
CQRS needs a read database and a write databaseThe foundational form is two models over one database
CQRS means eventual consistencyOnly when reads come from a separate store that's updated later
CQRS needs a message busAzure's guide: messaging isn't a requirement for CQRS
CQRS belongs across the whole systemFowler: use it on specific portions of a system, and cautiously

How to apply it in your project

  1. Separate the code pathsPut commands, which change state and return little, and queries, which return DTOs and change nothing, in separate classes. Sharing one DbContext is fine.
  2. Keep the domain model on the command sideEntities and business rules live where state changes. Queries project with Select instead of loading aggregates with Include and mapping them.
  3. Add a read store only for a measured needReach for a separate store when a query can't be made fast or scaled on the main database, and plan for the outbox, idempotent consumers and stale reads from day one.
  4. Decide on event sourcing separatelyUse it where history, audit or replay is a business requirement, and only for that part of the system, such as a payment ledger.

Key takeaways

  • CQRS is one split: a model for commands and a model for queries, where you'd otherwise have one.
  • A thin read side over the same database is the foundational form of CQRS, not a shortcut.
  • Separate read stores bring synchronization, an outbox and eventual consistency; add them for a measured need.
  • Event sourcing is a different pattern for storing state as events; it leans on CQRS, and CQRS doesn't need it.
  • Fowler's advice holds: apply CQRS to the parts of a system where reads and writes really differ, not everywhere.

📚 المصادر

  1. CQRS — Martin Fowler, bliki (2011)
  2. CQRS Documents — Greg Young (2010)
  3. CQRS pattern — Azure Architecture Center, Microsoft Learn
  4. Event Sourcing pattern — Azure Architecture Center, Microsoft Learn
  5. Applying simplified CQRS and DDD patterns in a microservice — .NET Architecture Guides, Microsoft Learn

التعليقات (0)