What is the difference between Event Sourcing and Command Query Responsibility Segregation (CQRS)?
Short answer: Event Sourcing and CQRS are related patterns, but they serve different purposes and are often used together: Event Sourcing: Focuses on how state changes are stored and communicated.
Explain a bit more
It stores events instead of the current state of an entity. The events can be replayed to rebuild the state, ensuring that every state change is traceable and auditable. CQRS: Separates the command (write) and query (read) operations into distinct models. In CQRS, the write model (or command) is responsible for modifying data, and the read model (or query) is optimized for querying data. This separation allows for optimizations in both reading and writing. How they relate: Event Sourcing can serve as the write model in CQRS. Events are stored as part of the write process. The read model in CQRS can be a materialized view (a denormalized representation) that is optimized for querying, which may be updated asynchronously based on the events.
Real-world example (ShopNest)
After payment succeeds, ShopNest publishes OrderPaid. Inventory and Notification services react independently—no giant distributed transaction.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png