Articles

Why Event Sourcing? Part 2 - The Unique Feature of Event Sourcing

Stephen Tung  |  14 November 2023

 

Introduction

In part 1 I have explained how traditional databases are not ideal to manage complex and core business data because they are largely designed around transactional consistency.

On the other hand, event sourcing is designed with eventual consistency in mind. It serves more as an application pattern for systems that are easier to understand and simpler to change.

In this article, we will explore the key concepts of event sourcing and its unique features.

Event Sourcing and the State Transition Database

Current State vs Historical Events

Data can arguably be represented in one of two ways:

  1. Current state vs
  2. Historical events

For example, your credit card account can be represented in its current state, with properties like the account holder name, a $0 balance, and a $1,000 credit limit.

Or it can be represented as a series of historical events: You opened an account, made a series of purchases, paid off your credit card on time, and your credit limit got upgraded. And these events collectively make up the same current state.

Current state is a summary that’s straightforward to understand.

Historical events are a story that leads to deeper insight.

7_Current vs Historical events

Traditional vs State Transition Database

Traditional databases, like relational and document databases, generally focus on capturing the current state. They do not retain past data once it is updated or deleted.

On the other hand, event sourcing uses a "state transition" database. This type of database focuses on logging historical events, ensuring that past data is retained even when it's updated.

Relationship between Event Sourcing and State Transition Database Event Sourcing is an application pattern that uses a state transition database.

The state transition database is made up of 2 concepts: events and the log.

For more information about the state transition database see, watch this webinar.

State Transition Concept #1: Granularity of Events

An event is something important that occurred in the application.

Every event has a “name” that represents the business action that took place. This is a more granular data representation that allows for all kinds of analysis.

For example:

  • CreditCardOpened
  • CreditCardDelivered
  • CreditCardSuspended
  • CreditCardLost

Traditional databases, on the other hand, don’t often capture data at this granularity because it is optimized for a specific scenario.

For example, capturing CreditCardStatus = "open" or "closed" , might be enough for a particular high level business report. But this loses the intentions and deeper insights behind.

8_Granularity

State Transition Concept #2: Completeness of the Log

The log on the other hand captures a complete history of all events which is not generally discarded.

Compared to traditional databases that overwrite data upon every change, this gives a more complete timeline of your data.

9_Completeness

State Transition - the Richer Database

With both the granularity of events and the completeness of a log, this makes the state transition a much richer database model that give extra depth and breadth to your data.

This is like the difference between a high-definition video vs a grainy photo.

10_Data Graph

Unique Feature of Event Sourcing: Reliably Generate Unlimited Perspectives across Space and Time

Flexibility of State Transition Database

The state transition database gives you the flexibility to freely transform your data.

This is like watching an entire TV series from start to finish, which allows you to see things from all sorts of angles. Traditional databases on the other hand are like only watching the final episode of the final season, giving you an extremely narrow point of view.

Auditability of State Transition Database

The state transition database also gives you the auditability to make sure any constructed interpretation can be verified.

This is like being able to rewind and rewatch a certain scene in a TV series to settle a heated discussion between you and your friend. In contrast, the traditional database is like cable TV which only broadcasts the latest scene, which is hard to travel back in time to review.

Reliably Generate Unlimited Perspectives Across Space and Time

With this flexibility and auditability, state transition databases give you the ability to generate virtually unlimited perspectives across space and time which are still true to the source.

The unlimited perspectives across space represents the numerous models that it can transform into. Some of these models are not possible with traditional databases due to the context that is lost when the state is changed.

For example, with your credit card transactions, it is simple to calculate all sorts of things like your current balance, overdue amount, spending habits, and accrued reward points.

The unlimited perspectives across time represents how each of these models can be reshaped to its past, present, or even future form. The past can be calculated by going back in time with the event log. And the future can be predicted with machine learning when enough past events are available.

For example, with your credit card transactions, it is easy to travel to the past and pull out your previous payment amounts. And with these historical payments we can estimate what your next future payment can be.

Summary

11_Summary

In this article we have learnt that:

  • Traditional databases often capture the current state only but state transition databases capture historical events,
  • State transition databases are made up of a log of named events, which form the most complete and granular history of change,
  • This forms a very rich data model that has the flexibility to see from many perspectives, and the auditability to verify them, and
  • This allows it to reliably generate unlimited perspectives across space and time.

In the next and final article in our 3 part series, we will example how this unique feature leads to a few benefits that are useful for this increasingly complex world.


Photo of Stephen Tung

Stephen Tung Stephen has been a software practitioner and leader focused on simplifying and tackling the heart of complex business problems. He discovered DDD/CQRS/ES 15 years ago and has never looked back since. He's the father of three, living in Hong Kong, and enjoys to zen out when there is a moment.