What is CQRS Pattern?
CQRS stands for:
Command Query Responsibility Segregation
CQRS is a design pattern that separates Read operations and Write operations.
Instead of using one model for both reading and writing, CQRS uses two separate models:
-
✅ Command → Write (Create, Update, Delete)
-
✅ Query → Read (Get data)
Simple Example
Without CQRS:
UserService
├── GetUsers()
├── CreateUser()
├── UpdateUser()
└── DeleteUser()
With CQRS:
Commands
├── CreateUserCommand
├── UpdateUserCommand
└── DeleteUserCommand
Queries
└── GetUsersQuery
This makes code cleaner and more maintainable ✨
Why Use CQRS?
CQRS helps with:
✅ Separation of concerns
✅ Better performance
✅ Scalable applications
✅ Cleaner architecture
✅ Easier testing
Command vs Query
| Type | Purpose | Example |
|---|---|---|
| Command | Changes data | CreateUser |
| Query | Reads data | GetUsers |
Example Without CQRS
public class UserService
{
public User GetUser(int id) { }
public void CreateUser(User user) { }
public void UpdateUser(User user) { }
}
Everything mixed together 😬
Example With CQRS
Query
public class GetUserQuery
{
public int Id { get; set; }
}
Handler:
public class GetUserQueryHandler
{
public User Handle(GetUserQuery query)
{
// Get user logic
}
}
Command
public class CreateUserCommand
{
public string Name { get; set; }
}
Handler:
public class CreateUserCommandHandler
{
public void Handle(CreateUserCommand command)
{
// Create user logic
}
}
CQRS with MediatR (Most Common in .NET)
Most **ASP.NET Core applications use MediatR with CQRS.
Example:
public class GetUserQuery : IRequest<User>
{
public int Id { get; set; }
}
Handler:
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, User>
{
public Task<User> Handle(GetUserQuery request, CancellationToken cancellationToken)
{
// logic
}
}
When Should You Use CQRS?
Use CQRS when:
✅ Large applications
✅ Complex business logic
✅ High performance required
✅ Microservices architecture
✅ Many reads and writes
When NOT to Use CQRS
Avoid CQRS when:
❌ Small applications
❌ Simple CRUD apps
❌ Over-engineering risk
CQRS adds extra complexity
Real World Example
E-commerce Application:
Commands:
-
Create Order
-
Update Order
-
Cancel Order
Queries:
-
Get Orders
-
Get Order Details
-
Get User Orders
CQRS + Entity Framework
Commands usually use:
-
Entity Framework Core for writes
Queries often use:
-
Raw SQL
-
Dapper (for performance)
Interview Best Answer
If interviewer asks:
What is CQRS pattern?
You can answer:
"CQRS stands for Command Query Responsibility Segregation.
It separates read and write operations into different models, improving maintainability, scalability, and performance.
It's commonly used with MediatR in ASP.NET Core applications."
CQRS Architecture (Simple View)
Controller
↓
Mediator
↓
Command / Query
↓
Handler
↓
Database
Advantages
✅ Clean architecture
✅ Scalable
✅ Better performance
✅ Easier testing
Disadvantages
❌ More files
❌ More complexity
❌ Harder for small apps
Summary
| Feature | CQRS |
|---|---|
| Separates Read/Write | Yes |
| Scalable | Yes |
| Complex | Yes |
| Best for Large Apps | Yes |
