CQRS

Write on one side, read on the other · Part of Architecture

Key idea

Practice

7 questions · about 4 min

Explained step by step

CQRS

CQRS (Command and Query Responsibility Segregation) is a pattern that splits a system's actions into commands and queries. Greg Young first described it. The core idea is separating them, because they play very different roles, so each side is optimized as needed. It's like a restaurant with 2 counters: one only takes orders and the other only hands them out; neither slows the other at rush hour.

CQRS is often used together with event sourcing. In that case, the event store is the write model and the official source of truth. The read model is built from the events, as materialized views ready to read. It's like an accounting ledger (the events) and the monthly statement your bank sends (the view): the statement is built from the ledger, and if it's lost, it's rebuilt.

CQRS has advantages: reads and writes scale separately, optimizing and changing the architecture is easier, it stays close to business logic with loose coupling, queries avoid complex JOINs and the boundaries of the system's behavior are clear. It's like a supermarket with tills only for paying and aisles only for browsing: if 10 times more people are browsing, you add aisles, not tills.

CQRS helps when: read performance must be tuned separately from write performance; the system will change over time, with several model versions or business rules that change often; it integrates with other systems, especially with event sourcing, where the failure of 1 part shouldn't take down the others; and for more security, controlling who can write. In a small app with 10 users, though, it's extra complexity. It's like having a separate kitchen and dining room: worth it in a restaurant, not at home.

Command

A command is an instruction to perform a task: it aims to change something and doesn't return data, only whether it worked. A query is a request for information that doesn't change the system's state or have side effects. «Transfer $50» is a command; «view my balance» is a query. It's like at a bank: depositing changes your account and you only get a receipt; checking the balance changes nothing.

Read model

CQRS uses 2 separate data models: one for writing and one for reading, each built for its task and often stored in physically separate places. The read model usually has highly denormalized materialized views, ready to read without JOINs. It's like a kitchen and a shop window: the kitchen prepares everything in order; the window shows it already assembled so the customer sees it at a glance.

Eventual consistency

CQRS has disadvantages: the app's design is more complex, messages between the 2 models can fail or be duplicated, you must live with eventual consistency (the read model updates a little later) and it costs more to maintain. It's like changing a price at the supermarket till: the aisle sign updates a few minutes later, and meanwhile shows the old price.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Write on one side, read on the other