Video in progress
When should you introduce CQRS in a read-heavy system, and what complexity does it add to a Spring system?
When the requirements for the read and write models differ sharply: reads are many and complex (search, aggregates, different views), while writes have strict business rules. CQRS keeps the write model clean and gives reads separate, optimized models. The price is eventual consistency, synchronization infrastructure and maintaining two models.
📖 Answer
CQRS has levels.
-
At the code level, the cheapest and often enough. Writes go through entities, domain rules and JPA; reads use separate queries, projections, DTOs with
JdbcClient. One database, one transaction, no consistency problem. -
Separate read tables: denormalized tables or materialized views in the same database, updated on write or through events.
-
Separate read stores: writes go to PostgreSQL, reads to Elasticsearch, Redis or ClickHouse. Projections are updated through events (outbox → Kafka).
You move to level 3 when you need full-text search, facets or geo search, when heavy analytical queries are choking the main database, or when reads outnumber writes by hundreds of times and need to scale separately.
The complexity it brings:
- Eventual consistency: a user adds a product, and it isn't visible in search for a few seconds. The UI has to account for it (for example by adding the new item locally).
- Rebuilding the projection: if the schema changes or there's a bug, you have to replay all events or do a full reindex. Plan for this in advance.
- A metric and an alert for projection lag.
- Don't mix it with event sourcing. CQRS doesn't require it, and event sourcing is a much bigger decision.
I start at level 1 and move to the next only if there's a measured problem.
Sign up to mark studied questions and save your progress.