architect-handbook

Software Architect Handbook

View on GitHub

Event Sourcing

Overview

Instead of storing just the current state of the data in a domain, use an append-only store to record the full series of actions taken on that data.

The store acts as the system of record and can be used to materialize the domain objects. This can simplify tasks in complex domains, by avoiding the need to synchronize the data model and the business domain, while improving performance, scalability, and responsiveness.

It can also provide consistency for transactional data, and maintain full audit trails and history that can enable compensating actions.

Context & Problem

The typical approach is for the application to maintain the current state of the data by updating it as users work with it, often by using transactions that lock the data.

The CRUD approach has some limitations:

Solution

The Event Sourcing pattern defines an approach to handling operations on data that’s driven by a sequence of events, each of which is recorded in an append-only store. Application code sends a series of events that imperatively describe each action that has ocurred on the to the event store, where they’re persisted.

Each event represents a set of changes to the data.

The events are persisted in an event store that acts as the system of record about the current state of the data (i.e., the authorirative data source). The event store typically publishes these events so that consumers can be notified and can handle them if needed.

The application code that generates the events is decoupled from the system that subscribe to the events.

Usage

Typical uses of the events published by the store are to maintain materialized views of entities as actions in the application change them, and for integration with external systems.

In addition, at any point it’s possible for applications to read the history of events, and use it to materialize the ucrrent state of an entity by playing back and consuming all the events related to that entity. This can occur on demand to materialize a domain object when handling a request, or through a scheduled task so that the state of the entity can be stored as a materialized view to support the presentation layer.

Event sourcing is commonly combined with the CQRS pattern by performing the data management tasks in response to the events, and by materializing views from the stored events.

Advantages

Issues and considerations

Eventual consistency

The system will only be eventually consistent when creating materialized views or generating projections of data by replaying events. There’s some delay between an application adding events to the event store, the events being published, and consumers of the events handling them. During this perior, new events that describe further changes to entities might have arrived at the event store.

Multi-threaded applications might be storing events. The consistency of events is vital, as is the order of events that affect a specific entity. Adding a timestamp to every event can help to avoid issues. Another common practice is to annotate each event with an incremental identifier and if two actions attempt to add events for the same entity at the same time, the event store can reject an event.

Even though event sourcing minimizes the chance of conflicting updates to the data, the application must still be able to deal with inconsistencies that result from eventual consistency and the lack of transactions.

For example, an event that indicates a reduction in stock inventory might arrive in the data store while an order for that item is being place, resulting in a requirement to reconcile the two operations either by advising the customer or creating a back order.

Idempotence

Event publication might be at least once, and so consumers of the events must be idempotent.

They must not reapply the update described in an event if the event is handled more than once.

For example, if multiple instances of a consumer maintain and aggregate an entity’s property, such as the total number of orders placed, only one must succeed in incrementing the aggregate when an order placed event occurs.

Changing events

The event store is the permanent source of information, and so the event data should never be updated. The only way to update an entity to undo a change is to add a compensating event.

If the format of the persisteds events needs to change, perhaps during a migration, it can be difficult to combine existing events in the store with the new version. It might be necessary to iterate through all the events making changes so they’re compliant with the new format, or add new evens that use the new format.

Consider using a version stamp on each version of the event schema to maintain both the old and the new formats.

Querying

There’s no standard approach for reading the events to obtain information. The only data that can be extracted is a stream of events using an event identifier as the criteria, which typically maps to individual identities.

The current state of an entity can be determined only by replaying all of the events that relate to it against the original state of that entity.

The length of each event stream affects managing and updating the system. If the streams are large, consider creating snapshots at specific intervals such as a specified number of events. The current state of the entity can be obtained from the snapshot and by replaying any events that ocurred after that point in time.

See Master-Subordinate Snapshot Replication

When to use

When not to use

Example

A conference management system needs to track the number of completed bookings for a conference so that it can check whether there are seats still available. There are at least two ways:

Some optimizations to consider are using snapshots so that you don’t need to query and replay the full list of events to obtain the current state of the aggregate, and maintaining a cached copy of the aggregate in memory.