CQRS (Command Query Responsibility Segregation) is an architectural pattern that separates read and write operations into distinct models, enabling independent optimization and scaling of each concern. Originally coined by Greg Young building on Bertrand Meyer's Command-Query Separation (CQS) principle, CQRS has become essential in event-driven architectures and microservices where read-heavy workloads, complex business logic, or high scalability requirements demand specialized handling. The pattern enables polyglot persistence (different databases for reads and writes), eventual consistency, and performance optimization by allowing each model to use data structures, schemas, and technologies best suited to its purpose. While CQRS introduces complexity through separate codebases and consistency management, it unlocks horizontal scalability, reduces contention, and provides clear separation between state-changing commands and side-effect-free queries—making it invaluable for systems requiring audit trails, complex read views, or independent scaling of read and write throughput.
What This Cheat Sheet Covers
This topic spans 11 focused tables and 106 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core CQRS Concepts
| Concept | Example | Description |
|---|---|---|
CreateOrderCommand(userId, items)UpdateProfileCommand(email, name) | • Intent to change state • carries all data needed to perform a write operation • processed synchronously or asynchronously • returns success/failure but not domain data. | |
GetOrderByIdQuery(orderId)SearchProductsQuery(filters, pagination) | • Side-effect-free read operation • returns data without modifying system state • can be cached, optimized, or denormalized for performance | |
class CreateOrderHandler : def handle(cmd): ... | • Executes business logic for a single command type • validates, applies domain rules, persists changes, and publishes domain events. | |
class GetOrderHandler : def handle(qry): ... | • Fetches data from the read model • performs filtering, pagination, sorting • no business logic—only data retrieval and transformation | |
Order aggregate with apply()methods, event sourcing | • Normalized schema optimized for writes • enforces business rules, consistency, and transactional boundaries • stores events or current state |