CQRS Pattern — Separate Read/Write Models in Spring
Fast recall
Quick revision
Core answer
Understanding of scalability trade-offs, eventual consistency, and domain model design
Conflating CQRS with Event Sourcing — they are separate patterns that are often combined but neither requires the other
Explain the scalability motivation (read replicas for query side), mention Axon Framework for full CQRS/ES, and discuss eventual consistency between write and read models
Interview-ready guide
Interview answer
CQRS separates the model used to write state from the model used to read state. The motivation is that writes need a rich domain model with business invariants and validation, while reads need fast, flat projections tailored to specific views — and a single model optimized for both typically does neither well.
In a Spring Boot application, lightweight CQRS means separate service classes. A command service annotated with @Transactional works with the full domain model — Order entity with business rules, state transitions, validation. A query service annotated with @Transactional(readOnly = true) works with Spring Data interface projections — flat DTOs that fetch only the columns needed for a specific view. The readOnly flag tells Hibernate to skip dirty checking entirely, which gives a measurable performance improvement.
The more advanced form uses separate databases for reads and writes. Commands write to a normalized relational database. Events derived from those writes populate a read-optimized store — Elasticsearch for search, Redis for dashboards, a denormalized read DB for fast queries. This enables independent scaling but introduces eventual consistency — the read model may briefly lag behind the write model.
Two things interviewers specifically test for: first, CQRS is not Event Sourcing. They're independent patterns that are often combined but neither requires the other. Second, most applications don't need full CQRS with separate databases. Lightweight CQRS — separate services, same database, read-only transactions — is the pragmatic starting point that gives you the separation benefits without the infrastructure overhead.
CQRS Pattern — Separate Read/Write Models in Spring
Direct Answer
CQRS (Command Query Responsibility Segregation) separates the model used to modify state (commands) from the model used to read state (queries). Write operations go through a domain model with business logic; reads use optimized, flat projections. In Spring, this is often implemented with separate service classes and repositories, and JPA projections or read-only views for queries.
Common Mistakes & Pitfalls
Conflating CQRS with Event Sourcing — they are separate patterns that are often combined but neither requires the other
Check your understanding
Answer these without looking back at the article.
- What is the difference between CQRS and Event Sourcing?
- How do you handle eventual consistency between the write and read models?
- When is CQRS overkill and a single model is sufficient?
Related interview prep categories
2Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.