Skip to the content.

1. What is CQRS?

CQRS is an architectural pattern that separates read operations (queries) from write operations (commands) into different models or services.

The key idea:

Don’t use the same data model to update and read data.


2. Why Separate Reads and Writes?

In a typical CRUD system:

With CQRS:


3. How It Works

            ┌─────────────┐
            │   Client     │
            └─────┬───────┘
         Command   │   Query
                   │
     ┌─────────────┴───────────────┐
     │          Application         │
     │    (Separate Paths for R/W)  │
     └───────────┬───────┬─────────┘
                 │       │
           Write Model   Read Model
           (Commands)    (Queries)
                 │       │
                 ▼       ▼
               DB-W     DB-R
           (Normalized) (Optimized / Cached)

4. Benefits


5. Drawbacks


6. Example Scenario

E-commerce Orders


7. When to Use CQRS

✅ Useful when:

❌ Overkill when:


In short: CQRS says — keep your write models clean for updates, and your read models optimized for queries. They don’t have to be the same structure, the same DB, or even the same service.


Now let’s connect CQRS with Event Sourcing, because they often complement each other in real-world systems.


1. Event Sourcing — Quick Recap

Example: Instead of saving:

Balance: $120

We store:

Deposited $100
Withdrew $30
Deposited $50

If we replay these events → balance = $120.


2. How Event Sourcing Fits with CQRS

With CQRS + Event Sourcing:

  1. Write Side (Commands)

    • User sends a command → system validates it → generates one or more events.
    • These events are stored in the Event Store (append-only log).
  2. Read Side (Queries)

    • A projection service listens to events and updates read-optimized views (could be SQL, NoSQL, Elasticsearch, etc.).
    • Queries hit these precomputed read models.

3. Architecture Flow

        ┌─────────────┐
        │   Client     │
        └─────┬───────┘
         Command │ Query
                 │
        ┌────────▼──────────┐
        │   Write Model     │  (Validates commands)
        └────────┬──────────┘
                 │
          Store Event(s)
                 │
         ┌───────▼─────────┐
         │   Event Store   │  (Append-only log)
         └───────┬─────────┘
                 │
   ┌─────────────▼─────────────┐
   │       Projections         │ (Builds read models)
   └─────────────┬─────────────┘
                 │
          ┌──────▼───────┐
          │ Read Model   │ (Optimized for queries)
          └──────────────┘

4. Benefits of CQRS + Event Sourcing


5. Drawbacks


6. Real-World Example — Banking


💡 Key takeaway: