As businesses grow, so does the complexity of their software. New features, new models, and new background processes all continue to be added. . .and developers struggle to make sense of it all. Yet the end user demands a swift and functional experience when interacting with your application. It is paramount to be open to alternative patterns that help tame complex, high-demand services. Two such patterns are command-query responsibility segregation (CQRS) and event sourcing (ES).
Command-query responsibility segregation is an architectural pattern for user-facing applications that extends from the now standard Model-View-Controller (MVC) pattern and is an alternative to the CRUD pattern. At its core, CQRS is about changing how we think of and work with our data by introducing two types of models: all user actions become commands, and a read-only query model powers our views. Commands and queries are logistically separated, providing additional decoupling of our application. CQRS also calls for changes in how we store and structure our data.
Enter event sourcing. Instead of persisting the current state of our domain objects or entities, we record historical events about our data. The key advantage is that we can examine our application data at any point in time, rather than just the current state. This pattern changes how we persist and process our data but is surprisingly efficient.
While each of the two patterns can be used exclusively, they complement each other beautifully and facilitate the construction of decoupled, scalable applications or individual services. Stephen Pember explores the fundamentals of each pattern and offers several examples and demonstration code to show how one might actually go about implementing CQRS and ES. Steve discusses task-based UIs and domain-driven design as he outlines some of the advantages—and challenges—that ThirdChannel has seen when developing systems using CQRS and ES over the past year.
20. –Eric Evans
“Some objects are not defined primarily by their attributes. They
represent a thread of identity that runs through time and often
across distinct representations. Sometimes such an object must
be matched with another object even though attributes differ.
An object must be distinguished from other objects even though
they might have the same attributes.”
90. THIRDCHANNEL @svpember
So Why ES?
• More than the Perfect Audit Log
• Only Structural Model That Does Not Lose Information
• Ideal for Business Analysis
93. THIRDCHANNEL @svpember
Current State is Boring
• Mine ALL ProductRemoved events
• Find ALL ProductAdded events followed by RemovedEvents
for same product within 5 minutes
• Find Average Duration between ProductAdd and OrderPlaced