Microservices Patterns: Event Sourcing

What is Event Sourcing? It is a different way of structuring the business logic and persisting aggregates.

Crishantha Nanayakkara
Cloud Native Daily

--

Microservices Patterns Series — Part 06

In a typical traditional persistence model, we store the “current state” of an aggregate on a relational database table. If we are to track any events that happened around the aggregate, we will basically use transaction log files to store them. However, these transactional logs may not give you the complete picture of a transaction state. It will probably give you some high-level information related to it. So let's find out what Event Sourcing can offer you in this aspect.

Event Sourcing

Event Sourcing is a different way of structuring the business logic and persisting aggregates. It persists an aggregate as a sequence of events and each event represents a state change of the aggregate.

By doing this, it helps to preserve the history of aggregates, which is valuable for auditing and regulatory purposes. With this history, it will provide some valuable information related to all the events that occurred for each entity. This is a way better approach compared to the traditional transactional log method.

Event Store

In Event Sourcing, event aggregates are stored as a sequence of events in a database, which is known as an Event Store.

In this approach, applications can insert or retrieve aggregate events by their primary key. At any given point, the aggregate “current state” could be derived from the event store value for the particular aggregate. With this, you will have a less chance of loosing any valuable information related to the aggregate that you are concerned with.

Event Publishing

Once events are stored in an event store, these events could be published to an Event Broker for its subscribers to consume them further. You can use an Event Broker (i.e. Apache Kafka) to handle this situation (See Figure 01).

Figure 01 — Event Sourcing

The above (Figure 01), is quite similar to the choreography-based saga transactional approach, which we discuss under Saga Pattern. In this, each microservice is subscribed to events published by other microservices, where there is no central orchestration.

Some of the key benefits of Event Sourcing are:

  1. Can reliably publish events whenever the state of an aggregate changes
  2. It can provide an audit log that is guaranteed to be accurate.
  3. Stores the entire history of each aggregate allowing to be queried them at a later stage

However, there are some drawbacks as well:

  1. Learning curve could be more than the traditional state handling. Moving from a traditional monolithic application to a more event-based microservices application could be a difficult proposition for a programmer or a designer
  2. Handling multiple versions of an event-based application could be tricky. If an aggregate schema changes over time, you will probably need to have another event store version to store the new changes in the schema.
  3. Deleting data could be tricky since event sourcing is there to preserve the history of aggregates.
  4. Querying an event store could be challenging. The CQRS (Command and Query Responsibility Segregation) method is specifically used for this.

Conclusion

So it was a brief introduction to Event Sourcing in general. Hope you got some good insight into the Microservice Event Sourcing Pattern by reading this blog. You can find my other blogs related to Microservices Patterns and Concepts using the following links.

  1. Microservices Patterns: Inter Process Communication
  2. Microservices Patterns: The Circuit Breaker Pattern
  3. Microservices Patterns: The Service Discovery Patterns
  4. Microservices Patterns: The Saga Pattern
  5. Microservices Patterns: Business Services Patterns

Thank You!

References

  1. Microservices Patterns: with Examples in Java [Book] — By Chris Richardson, Manning Publications, 2018
  2. Event Sourcing Pattern: https://microservices.io/patterns/data/event-sourcing.html
  3. Event Sourcing (Martin Fowler) : https://www.youtube.com/watch?v=ck7t592bvBg
  4. Many Meanings of Event Driven Architecture (Martin Fowler): https://www.youtube.com/watch?v=STKCRSUsyP0&t=202s
  5. Building a CQRS event store with Amazon DynamoDB — https://aws.amazon.com/blogs/database/build-a-cqrs-event-store-with-amazon-dynamodb/

--

--

Crishantha Nanayakkara
Cloud Native Daily

Enterprise Architect, Consultant @ FAO (UN), Former CTO, ICTA Sri Lanka