Command Query Responsibility Segregation
Subject: Computer and Technology noun /kəˈmænd ˈkwɪr.i rɪˌspɑːn.səˈbɪl.ə.ti ˌsɛɡ.rɪˈɡeɪ.ʃən/ 1 definition
- software-architecture
- system-design
- domain-driven-design
- distributed-systems
-
Definition (noun)
Entry #244 · UpdatedCommand Query Responsibility Segregation (CQRS) is a software architectural pattern that separates an application's state-changing operations (commands) from its data-retrieval operations (queries) by giving each path a distinct model. Extending Command-Query Separation from individual methods to system-level boundaries, it replaces a single shared CRUD model with a write model focused on enforcing business rules and invariants alongside one or more read models tailored for efficient queries. This structural boundary allows read and write workloads to scale, optimize, and evolve independently, though backing them with separate data stores typically introduces eventual consistency.
Common Forms
Single-Database CQRS: Separates command handlers and domain write models from lightweight query projections in application code while sharing one database, keeping reads strongly consistent without replication overhead.
Separate-Store CQRS: Pairs a transactional write database with one or more independent, query-optimized read stores—such as search indexes or denormalized caches—updated asynchronously through domain events or change data capture.
Event-Sourced CQRS: Records write-side state changes as an append-only sequence of immutable domain events rather than overwriting current records, enabling projectors to build, scale, or replay specialized read views on demand.
Example
An e-commerce checkout service processes
PlaceOrdercommands against a normalized PostgreSQL database to validate inventory and record transactions atomically. Each committed order emits anOrderPlacedevent that asynchronously updates an Elasticsearch read store, allowing customers and support agents to filter order histories in milliseconds without placing read load on the transactional database.