- Read and write workloads are often asymmetrical, with very different performance and scale requirements.
- Separate a service into two parts: the command side and the query side.
- The command side modules and data model implement create, update, and delete operations (CUD).
- The query-side modules and data model implement queries (R).
- The query side keeps its data model synchronized with the command-side data model.
- Command-side database and query-side database can use one database or separated databases.
- If separate command-side and query-side databases are used:
- They can use different types of database. For example, one database might be SQL, another one might be No-SQL.
- They can use different data schema. For example, the schema of query-side database can be optimized for queries.
- They can be scaled differently.
- If separate command-side and query-side databases are used, they must be kept in sync.
- There are several options for synchronization:
- CQRS is often used along with Event Sourcing.
- The command-side uses event sourcing.
- The command-side persists application state by storing sequence of events in the event store.
- The event store is the official source of information.
- The same event in the command-side can be used to notify other components, in particular, to notify the query-side.
- The command-side and the query-side can be designed and tuned differently.
- The command-side and the query-side may have difference requirements in performance, scalability (workload), security, etc.
- Simplify the command-side and the query-side in design and implementation.
- Supports multiple denormalized views of data.
- Increased complexity.
- Dealing with the replication lag between the command-side and the query-side.
Topic | Consideration | Possible Solution Options |
---|---|---|
Concurrency | Multiple concurrent updates to the same database record. | |
Messaging | If the query-side keep synchronized with the command-side by events, the event handler in the query-side must be able to handle duplicated events and event message failures. | |
Eventual consistency | A client that updates the command side and then immediately executes a query might not see its own update. The client needs to detect the query data is stale or not. |