Tuesday, June 30, 2026

CQRS (Command Query Responsibility Segregation) in .NET: A Complete Guide with Real-World Banking Example

Introduction

As enterprise applications grow, handling millions of users while maintaining performance becomes increasingly challenging. A traditional CRUD architecture, where the same model is responsible for both reading and writing data, often becomes a bottleneck.

Imagine a banking application where thousands of customers are transferring money, depositing funds, and withdrawing cash, while millions of users are simultaneously checking their account balances and transaction history. Using the same model and database operations for both reads and writes can lead to scalability issues, complex business logic, and poor performance.

This is where CQRS (Command Query Responsibility Segregation) comes into play.

CQRS is an architectural pattern that separates read operations from write operations, allowing each side of the application to be designed, optimized, and scaled independently.

In this article, we'll explore CQRS from the ground up, understand its architecture, implement it using ASP.NET Core, and examine how it solves real-world enterprise challenges.


What is CQRS?

CQRS stands for Command Query Responsibility Segregation.

The idea behind CQRS is surprisingly simple:

  • Commands modify data.

  • Queries retrieve data.

Instead of using a single model for both operations, CQRS separates them into two independent models.

In a traditional CRUD application, the same entity is responsible for both reading and updating data.

Client
   |
Application
   |
Database

Everything goes through the same model.

With CQRS, the architecture becomes:

                Client

         /                 \

     Commands            Queries

         |                   |

    Write Model        Read Model

         |                   |

    Write Database     Read Database

The write side focuses on business rules and consistency, while the read side is optimized for speed and data retrieval.


Why Do We Need CQRS?

Consider a real-world banking system.

Every day, customers perform operations such as:

  • Depositing money

  • Withdrawing money

  • Transferring funds

  • Opening new accounts

These operations modify data.

At the same time, customers constantly check:

  • Account balance

  • Transaction history

  • Mini statements

  • Monthly statements

These operations only read data.

In most banking applications, read operations are significantly higher than write operations.

Example:

OperationDaily Requests
Check Balance1,000,000
View Transactions650,000
Transfer Money35,000
Deposit Money18,000
Withdraw Money20,000

Clearly, the application spends much more time serving read requests.

CQRS allows us to optimize both workloads independently.


Understanding Commands

A Command represents an intention to change the application's state.

Examples include:

  • Create Customer

  • Place Order

  • Deposit Money

  • Withdraw Money

  • Cancel Order

  • Update Address

A command should never return business data.

For example:

Deposit ₹5,000

Expected response:

Success

Not:

Current Balance = ₹25,000

If the client needs the updated balance, it should execute a separate query.

This separation keeps responsibilities clean and predictable.


Understanding Queries

Queries are responsible only for retrieving information.

Examples include:

  • Get Customer Details

  • Get Balance

  • Search Products

  • View Orders

  • Transaction History

Queries must never modify data.

Their sole responsibility is to return information in the most efficient way possible.


Real-World Banking Example

Let's build a simple banking scenario.

John currently has:

Balance = ₹10,000

He deposits ₹5,000.

The client sends:

POST /accounts/deposit

Request:

{
  "accountId": 101,
  "amount": 5000
}

The request follows this flow:

API

↓

Deposit Command

↓

Command Handler

↓

Domain Logic

↓

Repository

↓

SQL Server

After the command completes, the balance becomes:

₹15,000

Now the user checks the balance.

GET /accounts/101

This request follows a different path.

API

↓

GetBalance Query

↓

Query Handler

↓

Read Database

↓

DTO

↓

Response

The response is:

{
  "balance": 15000
}

Notice that the read operation never touches the write model.


Why Separate Read and Write Models?

The write side contains business rules.

For example:

Account

- Account Number
- Balance
- Domain Events
- Version
- Validation Rules

The read side only contains information required by the user interface.

AccountSummary

- Customer Name
- Current Balance
- Last Transaction
- Reward Points

There is no unnecessary logic.

This makes queries extremely fast.


CQRS in ASP.NET Core

A typical project structure looks like this:

Application

 ├── Commands
 │      ├── DepositMoney
 │      ├── WithdrawMoney
 │      └── TransferMoney
 │
 ├── Queries
 │      ├── GetBalance
 │      ├── GetStatement
 │      └── GetTransactions
 │
 ├── Validators
 │
 ├── Domain
 │
 ├── Infrastructure
 │
 └── API

This structure keeps responsibilities organized and easy to maintain.


Implementing a Command

A command represents a user's intention.

public class DepositMoneyCommand : IRequest
{
    public int AccountId { get; init; }

    public decimal Amount { get; init; }
}

The command handler performs the business logic.

public class DepositMoneyHandler : IRequestHandler<DepositMoneyCommand>
{
    private readonly IAccountRepository repository;

    public DepositMoneyHandler(IAccountRepository repository)
    {
        this.repository = repository;
    }

    public async Task Handle(
        DepositMoneyCommand request,
        CancellationToken cancellationToken)
    {
        var account = await repository.GetAsync(request.AccountId);

        account.Deposit(request.Amount);

        await repository.SaveAsync(account);
    }
}

Notice that the controller contains almost no business logic.


Implementing a Query

The query only retrieves information.

public class GetBalanceQuery : IRequest<AccountDto>
{
    public int AccountId { get; init; }
}

The handler is simple.

public class GetBalanceHandler
    : IRequestHandler<GetBalanceQuery, AccountDto>
{
    private readonly IReadRepository repository;

    public GetBalanceHandler(IReadRepository repository)
    {
        this.repository = repository;
    }

    public async Task<AccountDto> Handle(
        GetBalanceQuery request,
        CancellationToken cancellationToken)
    {
        return await repository.GetBalanceAsync(request.AccountId);
    }
}

There is no business logic because queries should only retrieve data.


CQRS with MediatR

MediatR is the most popular library used with CQRS in ASP.NET Core.

Instead of calling services directly, controllers send commands and queries through the mediator.

[HttpPost]
public async Task<IActionResult> Deposit(
    DepositMoneyCommand command)
{
    await _mediator.Send(command);

    return Ok();
}

Similarly,

[HttpGet("{id}")]
public async Task<AccountDto> Get(int id)
{
    return await _mediator.Send(
        new GetBalanceQuery
        {
            AccountId = id
        });
}

This keeps controllers thin and promotes loose coupling.


CQRS in an E-Commerce System

Consider an online shopping platform.

When a customer places an order, the workflow is complex.

Place Order

↓

Validate Inventory

↓

Reserve Stock

↓

Calculate Discounts

↓

Process Payment

↓

Create Order

↓

Publish Event

However, retrieving order details is much simpler.

Get Order

↓

Read Database

↓

Return DTO

Different responsibilities deserve different models.


CQRS with Event-Driven Architecture

Enterprise systems often combine CQRS with messaging.

Deposit Money

↓

MoneyDeposited Event

↓

Azure Service Bus

↓

Notification Service

↓

Reporting Service

↓

Analytics Service

Each service reacts independently without tightly coupling business logic.

This architecture improves scalability and resilience.


Eventual Consistency

One important concept in CQRS is eventual consistency.

Suppose the write database updates immediately.

Write Database

↓

Publish Event

↓

Read Database Updated

There may be a short delay before the read database reflects the latest changes.

This delay is acceptable for most enterprise systems because the read model eventually becomes consistent.


Advantages of CQRS

Improved Performance

Read operations are optimized independently from write operations.


Independent Scaling

If your application receives millions of read requests but only thousands of writes, you can scale only the read infrastructure.


Cleaner Business Logic

Business rules remain isolated within command handlers and domain models.


Better Security

Commands can require stricter authorization while queries may expose public information safely.


Easier Maintenance

Separating responsibilities makes large applications easier to understand and maintain.


Better Reporting

Read databases can be denormalized for analytics without impacting transactional performance.


Challenges of CQRS

CQRS is powerful but introduces additional complexity.

Some common challenges include:

  • More project structure

  • Additional handlers

  • Eventual consistency

  • Messaging infrastructure

  • Synchronizing read models

  • Increased deployment complexity

For small CRUD applications, CQRS may be unnecessary.


When Should You Use CQRS?

CQRS is an excellent choice when your application has:

  • Complex business rules

  • High read-to-write ratio

  • Microservices architecture

  • Event-driven workflows

  • Large enterprise domains

  • Independent scaling requirements

  • High-performance reporting needs


When Should You Avoid CQRS?

CQRS may not be suitable for:

  • Simple CRUD applications

  • Small internal tools

  • Basic admin portals

  • MVPs and prototypes

  • Applications with minimal business logic

In these cases, a traditional layered architecture is often simpler and more cost-effective.


Best Practices

When implementing CQRS, follow these guidelines:

  • Keep commands focused on a single business action.

  • Keep queries read-only.

  • Use immutable DTOs.

  • Validate commands using FluentValidation.

  • Keep controllers thin.

  • Place business rules inside the domain layer.

  • Publish domain events after successful commands.

  • Use asynchronous messaging for updating read models.

  • Cache frequently used queries using Redis.

  • Monitor command failures and message retries.


CQRS with Clean Architecture

CQRS fits naturally into Clean Architecture.

Presentation Layer

↓

API Controllers

↓

MediatR

↓

Command / Query Handlers

↓

Domain Layer

↓

Infrastructure

↓

SQL Server

↓

Azure Service Bus

↓

Read Database

This architecture promotes loose coupling, testability, scalability, and maintainability.


Frequently Asked Interview Questions

What is CQRS?

CQRS is an architectural pattern that separates write operations (Commands) from read operations (Queries), allowing each side to evolve independently.


Does CQRS require two databases?

No.

Many applications start with a single database while maintaining separate command and query models. Separate databases can be introduced later if needed.


Is CQRS the same as Event Sourcing?

No.

CQRS separates reads and writes.

Event Sourcing stores every state change as an event.

They are often used together but solve different problems.


Why is MediatR commonly used?

MediatR decouples controllers from business logic by routing commands and queries to their appropriate handlers.


What is eventual consistency?

It means the read model may not reflect the latest write immediately, but it will become consistent after asynchronous processing completes.


Final Thoughts

CQRS is not just another design pattern—it is a strategic architectural approach that enables applications to scale efficiently while keeping business logic organized and maintainable.

By separating reads from writes, organizations can independently optimize each workload, simplify domain logic, improve performance, and build systems capable of handling enterprise-scale traffic.

When combined with Clean Architecture, Domain-Driven Design (DDD), MediatR, Event-Driven Architecture, Azure Service Bus, RabbitMQ, and Microservices, CQRS becomes one of the most effective patterns for developing modern cloud-native .NET applications.

Like any architectural pattern, CQRS should be applied where it adds value. For complex enterprise systems with demanding scalability and business requirements, it is an excellent choice. For smaller CRUD applications, however, the additional complexity may not be justified.

Understanding where and how to apply CQRS is what separates experienced software architects from developers who simply follow trends. Use it thoughtfully, and it can become a cornerstone of building robust, scalable, and maintainable enterprise applications.

Domain-Driven Design (DDD) Explained with a Real-World Banking Project (.NET)

 Domain-Driven Design (DDD) is a software design approach introduced by Eric Evans. It focuses on modeling software around the business domain rather than technical implementation details.

Simply put:

DDD = Understand the Business First, Then Write the Code

Instead of asking:

"Which database table should I create?"

DDD asks:

"How does the business actually work?"


Why DDD?

Imagine you are building an Internet Banking System.

Traditional Approach:

UI
 ↓
Business Logic
 ↓
Database

Problems:

  • Business logic scattered everywhere

  • Difficult to maintain

  • Hard to test

  • Database drives design

  • Tight coupling

DDD Approach:

Business Domain
      ↓
Application Layer
      ↓
Infrastructure
      ↓
Database

Business rules become the center of the application.


Real-World Project

Let's build:

Online Banking System

Features

  • Customer Registration

  • Account Creation

  • Deposit Money

  • Withdraw Money

  • Transfer Money

  • Loan Request

  • Transaction History

Large organizations using similar concepts:

  • HDFC

  • ICICI

  • SBI

  • PayPal

  • Stripe


Step 1: Understand the Business

Business experts explain:

Customer owns Accounts.

Each Account has Balance.

Customer can transfer money.

Transfer must:

  • Check sender balance

  • Deduct sender balance

  • Credit receiver

  • Record transaction

  • Send notification

Notice:

No one mentioned SQL Server.

No one mentioned Entity Framework.

Because DDD starts from business.


Step 2: Identify Domains

Banking System

├── Customer
├── Accounts
├── Loans
├── Payments
├── Notifications
├── Authentication

Each becomes its own domain.


Step 3: Identify Bounded Contexts

A bounded context defines where a particular model is valid.

Example:

+----------------------+
| Customer Context     |
+----------------------+

Customer
Address
Profile
KYC

Another context:

+----------------------+
| Banking Context      |
+----------------------+

Account
Balance
Transaction
Transfer

Loan Context:

Loan

EMI

Interest

Approval

Authentication Context:

Login

JWT

Refresh Token

Permissions

Each context is independent.


Complete Architecture

                    Banking System

      +---------------------------------------+

         Customer Context

         Banking Context

         Loan Context

         Payment Context

         Notification Context

         Identity Context

      +---------------------------------------+

Each can become its own Microservice.


DDD Layers

Presentation

↓

Application

↓

Domain

↓

Infrastructure

Let's understand each.


1. Presentation Layer

Contains

  • ASP.NET Core API

  • MVC

  • Angular

  • React

Example:

POST

/api/account/transfer

No business logic.

Only:

Receive request

Call Application Layer


2. Application Layer

Responsible for

  • Use cases

  • Coordination

  • Transactions

Example

TransferMoneyCommand

TransferMoneyCommand

↓

TransferMoneyHandler

↓

Domain

↓

Repository

↓

Save

No business rules here.

Only orchestration.


3. Domain Layer (Heart of DDD)

Contains:

  • Entities

  • Value Objects

  • Aggregates

  • Domain Events

  • Interfaces

  • Business Rules

Everything important lives here.


Example Entity

Account

Id

AccountNumber

Balance

CustomerId

Methods

Deposit()

Withdraw()

Transfer()

Notice

Not

Balance = Balance - amount

from Controller.

Instead

Account.Withdraw(amount)

Business logic belongs inside entity.


Example

public class Account
{
    public decimal Balance { get; private set; }

    public void Deposit(decimal amount)
    {
        if(amount <=0)
            throw new Exception("Invalid amount");

        Balance += amount;
    }

    public void Withdraw(decimal amount)
    {
        if(amount > Balance)
            throw new Exception("Insufficient funds");

        Balance -= amount;
    }
}

Business rule:

Cannot withdraw more than balance.

Lives inside entity.


Value Objects

Example:

Address

Street

City

State

ZipCode

Identity doesn't matter.

Two identical addresses are equal.

Example:

Address A

=

Address B

Value Objects are immutable.

Another example:

Money

100 INR

instead of

decimal

Aggregate

Aggregate groups related objects.

Example:

Customer

↓

Accounts

↓

Transactions

Customer is Aggregate Root.

Only root is accessed directly.


Example

Customer

↓

Savings Account

↓

Current Account

↓

Transactions

Don't modify child objects directly.

Go through Aggregate Root.


Repository Pattern

Instead of

DbContext.Accounts.First()

Use

IAccountRepository

Example

public interface IAccountRepository
{
    Task<Account> GetById(Guid id);

    Task Save(Account account);
}

Infrastructure implements it.


Infrastructure Layer

Contains

  • SQL Server

  • EF Core

  • RabbitMQ

  • Azure Service Bus

  • Redis

  • Email

  • Azure Storage

Nothing business-related.


Example

AccountRepository

↓

Entity Framework

↓

SQL Server

Domain Events

Suppose money transferred.

Need to

  • Send Email

  • SMS

  • Push Notification

  • Audit Log

Don't call everything directly.

Instead raise event.

MoneyTransferredEvent

Event

Notification Service

Email Service

Audit Service

Analytics

Loose coupling.


Example

public class MoneyTransferredEvent
{
    public Guid FromAccount { get; set; }

    public Guid ToAccount { get; set; }

    public decimal Amount { get; set; }
}

CQRS with DDD

Separate

Commands

Deposit

Withdraw

Transfer

Queries

Get Balance

Get Transactions

Get Customer

Never mix.


Architecture

API

↓

Command

↓

Handler

↓

Domain

↓

Repository

↓

Database

Queries

API

↓

Read Database

↓

DTO

Very scalable.


Folder Structure

BankingSystem

│

├── Banking.API

├── Banking.Application

│

├── Banking.Domain

│

├── Banking.Infrastructure

│

├── Banking.Shared

│

└── Banking.Tests

Inside Domain

Domain

│

├── Entities

├── ValueObjects

├── Events

├── Repositories

├── Aggregates

├── Exceptions

├── Enums

Application

Application

│

├── Commands

├── Queries

├── DTOs

├── Interfaces

├── Handlers

Infrastructure

Infrastructure

│

├── Persistence

├── Repositories

├── Messaging

├── Azure

├── Email

├── Redis

API

Controllers

Program.cs

Middleware

Swagger

Filters

Transfer Money Flow

Angular

↓

Transfer API

↓

Transfer Command

↓

Command Handler

↓

Account Aggregate

↓

Withdraw()

↓

Deposit()

↓

Raise Event

↓

Repository

↓

SQL Server

↓

Notification

DDD + Microservices

Customer Service

↓

Account Service

↓

Loan Service

↓

Payment Service

↓

Notification Service

Each service owns its own database and domain model.


Advantages

  • Business-focused design

  • Clear separation of concerns

  • Easier maintenance

  • Easier testing

  • Highly scalable

  • Fits microservices well

  • Encourages rich domain models

  • Better communication with domain experts

  • Reduces duplicated business logic


Challenges

  • Steeper learning curve

  • More upfront design effort

  • Can be overkill for small CRUD applications

  • Requires close collaboration with business experts

  • More classes and abstractions than simple layered architectures


Common Interview Questions

1. What is Domain-Driven Design?

A software design approach that models software around the business domain, placing business rules in the domain model rather than spreading them across controllers or data access code.

2. What is a Bounded Context?

A clearly defined boundary within which a specific domain model and vocabulary are valid. Different bounded contexts can have different models for the same real-world concept.

3. What is an Entity?

An object defined by its identity that can change over time, such as a Customer or Account.

4. What is a Value Object?

An immutable object defined by its values rather than identity, such as Money or Address.

5. What is an Aggregate?

A cluster of related entities and value objects treated as a single consistency boundary, with an Aggregate Root controlling access.

6. What is an Aggregate Root?

The main entity of an aggregate through which all changes to the aggregate are made, ensuring business invariants are maintained.

7. What is a Repository?

An abstraction for loading and saving aggregates without exposing persistence details.

8. What are Domain Events?

Events raised by the domain to signal that something important has happened (for example, MoneyTransferredEvent), allowing other parts of the system to react without tight coupling.

9. How does DDD work with CQRS?

Commands change state through the domain model, while queries read optimized data models. This separation improves scalability and keeps business logic focused.

10. When should you use DDD?

DDD is most valuable for complex business domains—such as banking, insurance, healthcare, logistics, and e-commerce—where business rules are rich and evolve over time. For simple CRUD applications with minimal business logic, a traditional layered architecture is often sufficient.


Summary

In the banking example, DDD helps you model concepts like Accounts, Customers, Money Transfers, and Transactions the way the business understands them. Business rules such as "an account cannot be overdrawn" live inside the Account aggregate, application services coordinate use cases, repositories abstract persistence, and domain events notify other services of important changes. This leads to software that is easier to evolve, test, and scale as business requirements grow.

Don't Copy

Protected by Copyscape Online Plagiarism Checker