Why Event Sourcing? Event Sourcing is a concept that becomes increasingly popular day by day. Even ThoughtWorks has brought it into it’s latest Technology Radar. Let’s do a quick overview of ES one more time. In essence event sourcing is about persisting data in a way that preserves every single bit of information. It’s about representing objects as a sequence of events that took place through the time and led them to the current state. For instance, if I were to persist information about my pocket money (i.e.: 67 EUR), I could simply save the latest state somewhere in a variable or database: Balance: 100 EUR Now, whenever there is a change, we would overwrite this value with the new value (discarding the previous one).
Balance: 67 EUR Simple and elegant (and works perfectly in a large number of scenarios). Got from ATM: 100 EUR Bought metro tickets: -12 EUR Grabbed a lunch: -8 EUR Found a coin: 1 EUR Took taxi: -14 EUR Balance: 100 - 12 - 8 + 1 - 14 = 67 EUR Storage Ignorance GotMoneyFromAtm! Flexibility Concerns. Retroactive Event. Automatically correct the consequences of a incorrect event that's already been processed.
Enterprise applications are about capturing information from the world, doing various computations on that information, and initiating further actions in that world. The computations they carry out and the actions they initiate can only be as accurate as the information they receive. If you get error in the input, you get errors in the output. CQRS, Task Based UIs, Event Sourcing agh!
Many people have been getting confused over what CQRS is.
They look at CQRS as being an architecture; it is not. CQRS is a very simple pattern that enables many opportunities for architecture that may otherwise not exist. CQRS is not eventual consistency, it is not eventing, it is not messaging, it is not having separated models for reading and writing, nor is it using event sourcing. I want to take a few paragraphs to describe first exactly what CQRS is and then how it relates to other patterns.
CQRS Command and Query Responsibility Segregation Starting with CQRS, CQRS is simply the creation of two objects where there was previously only one. When most people talk about CQRS they are really speaking about applying the CQRS pattern to the object that represents the service boundary of the application. CustomerService Applying CQRS on this would result in two services CustomerWriteService CustomerReadService That is it. Task Based UI A task based UI is quite different from a CRUD based UI. Why use Event Sourcing? Udi and I agree on probably 95% of what we talk about, one of the places that we have differing opinions is in the use of Event Sourcing I use the term as described previously to mean the rebuilding of objects based on events, not the definition that is currently on the bliki.
To me this is an important distinction and I figured it would be worthwhile to write a post on why I feel the way I do, I explained parts of it in the previous post about CQRS and Event Sourcing but I wanted to talk not just about how the patterns are symbiotic but also some of the other reasons I use event sourcing. Using a RDBMS To start with let’s go through the alternative architecture, that is to rebuild objects from something that saves current state on the write side. Event Sourcing. Capture all changes to an application state as a sequence of events.
We can query an application's state to find out the current state of the world, and this answers many questions. However there are times when we don't just want to see where we are, we also want to know how we got there. Event Sourcing ensures that all changes to application state are stored as a sequence of events. Not just can we query these events, we can also use the event log to reconstruct past states, and as a foundation to automatically adjust the state to cope with retroactive changes. How it Works The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.
Let's consider a simple example to do with shipping notifications. Figure 1: A simple interface for tracking shipping movements. Reversing Events. Event Sourced Architectures for High Availability.