Event sourcing
This conceptual guide explains the fundamental principles that apply when working with event sourcing.
#
DefinitionEvent sourcing is an architectural pattern in which state is not stored directly, but rather computed as-needed from events stored in an event log.
#
Benefits#
AuditabilityBy using an append-only event log, you have an easily auditable, and complete history of what has happened in your system. This means you can always understand exactly what happened and when.
#
OrthogonalityBy separating the fundamental store from the computed view (state), you separate the what has happened concern from the how to look at it concern.
#
ExtensibilityBecause you always know what has happened in the past, you can—from the future—change how to interpret the past. This is something you could never do with a traditional state-based system.
#
Challenges#
PerformanceAs the size of the event store increases, the amount of time it takes to compute a state may increase if you don't remember the previous state you computed. Snapshots (link) can help mitigate this.
#
ReasoningBecause of the separation of events and state, reasoning about the system can be more difficult unless you compute the current state.
#
MigrationsEvent schema migrations can pose serious challenges, especially if you want to migrate without deleting past events—which you can't do if they affect your state.
#
Relevance to ActyxOSActyxOS provides you with the basic tools you need to build a decentralized event sourcing system. The Event Service's persistent event streams allow you to model a distributed append-only log—indeed, that is what they were designed for. By interacting with the Event Service, you can run apps that consume these event streams, thus allowing you to compute state.
Actyx Pond
Check out the Actyx Pond – an auxiliary product to ActyxOS—which provides you with an always available, partition-tolerant event sourcing system out of the box. It also tries to mitigate some of the key associated challenges.
#
ExampleConsider, for instance, a truck being loaded with shipping boxes. At any one point in time the truck will have a loading state. If we were to track this state programmatically we might write an object as follows:
Now, whenever a package is loaded or unloaded from the truck by a worker (or robot), we might adjust the state as follows:
With this approach, we are continuously keeping track of the state and updating it as things change.
note
This is how most software systems are built, with the state being held in large databases and CRUD operations leading to state changes.
Using an event sourcing architecture we would take a different approach. Let's have a look.
Firstly we would define two types of events that may happen in our system—and that may affect our state:
We would then build an event store—more precisely an append-only event log—that we append new events to whenever they happen:
warning
Unless you have very good reasons for doing so, you should never remove an event from an append-only event log. If you want to undo something, in most cases, the right approach is to define a compensating event that undoes what a previous event may have done.
What if we now want to find out the current loading state of our truck? We need two things for this work. Firstly, an initial state, i.e. what was the loading state when the truck came off the production line. Secondly, a function that computes a state from events. Let's build both:
Now, if we want to know the current loading state of the truck we must simply call the computeState
function and pass it to our current event log.
More idiomatic implementation
In reality, you would not implement your system this way. You would, rather, define an onEvent function that takes a current state and a single event and computes a new state. Then you would repeatedly call that function for each event.
#
Learn more- Martin Fowler's introduction to event sourcing
- Event Sourcing Pattern (Microsoft)