Back to Platform & Architecture
platform
CQRS
Command Query Responsibility Segregation
CQRS — Overview
Command Query Responsibility Segregation
CQRS separates write model (commands, business rules) from read model (optimized queries). Commands update event store; projections build read-optimized views. Pairs naturally with event sourcing and Kafka.
// Command side
@Service
public class TransferCommandHandler {
public void handle(TransferCommand cmd) {
Account from = eventStore.load(cmd.getFromAccount());
from.withdraw(cmd.getAmount());
Account to = eventStore.load(cmd.getToAccount());
to.deposit(cmd.getAmount());
eventStore.save(from.events());
eventStore.save(to.events());
}
}
// Query side — separate read model
@GetMapping("/accounts/{id}/balance")
public BalanceView getBalance(@PathVariable String id) {
return balanceProjection.findById(id); // Materialized view
}Tip: Eventual consistency between command and query side — UI must handle stale reads gracefully.