ASP.NET Core Web API Interview Question #179

When should a Web API use CQRS?

Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced

Quick Interview Answer

CQRS separates command operations that change state from query operations that read state. It can be useful when read and write models have significantly different requirements, workflows are complex, or independent optimization provides value.

Detailed Explanation

CQRS separates command operations that change state from query operations that read state. It can be useful when read and write models have significantly different requirements, workflows are complex, or independent optimization provides value.

CQRS adds concepts and infrastructure, so it should not be adopted automatically for simple CRUD APIs. The complexity should be justified by the application's domain and scaling needs.

Code Example

public record GetOrderQuery(int Id);
public record CreateOrderCommand(
    int CustomerId,
    IReadOnlyList<OrderLineDto> Lines);

// Query and command handlers can evolve independently.