Event sourcing : But why ?

Abhinav Vinci
4 min readFeb 27, 2023

--

Part 2: https://medium.com/p/1040b1005a5d

Event sourcing is a software design pattern that has been gaining popularity in recent years. At its core, event sourcing is a way of storing data as a sequence of events, rather than storing the current state of the system.

What is Event Sourcing?

At its simplest, event sourcing involves storing data as a sequence of events. Each event represents a state change in the system, and is stored in an append-only log or database. By storing events rather than current state, the system can reconstruct the state at any point in time by replaying the events in the correct order.

Typical these information are stored in an event store which publishes events which can be used for mulitple purposes like to maintain materialized views of entities. [this makes reads faster]

For example, let’s say you’re building a banking application. Rather than storing the current balance of a user’s account, you would store a sequence of events representing each transaction that has occurred.

https://learn.microsoft.com/en-us/azure/architecture/patterns/event-sourcing

Why Use Event Sourcing?

1. Auditability : Because events are stored in an append-only log, it is easy to audit the system and track changes over time. This can be particularly useful in industries such as finance, healthcare, or government, where regulatory compliance is important.

2. Reconstructing State is easy : Because events represent state changes, it is easy to reconstruct the state of the system at any point in time by replaying the events. This can be useful for debugging or for creating historical reports.

3. Scalability : Event sourcing can be more scalable than traditional database systems because it allows for distributed systems and parallel processing of events. This makes it well-suited for systems that need to handle high volumes of data or that need to be highly available.

4. More Flexible : Because events can be processed and reprocessed in different ways, event sourcing can be more flexible than traditional database systems. For example, you could use different algorithms to calculate account balances or to generate reports.

Challenges of Event Sourcing

  • More Complex : Event sourcing can be more complex to implement than traditional database systems because it requires careful consideration of event schema and event processing.
https://stackoverflow.com/questions/56728979/event-sourcing-why-a-dedicated-event-store
  • Difficult to create complex queries or to support ad-hoc queries : Querying event-sourced data can be more complex than querying traditional database systems because it requires reconstructing the state of the system from the events. This can make it more difficult to create complex queries or to support ad-hoc queries.
  • High Storage Requirements : Because events are stored in an append-only log, event sourcing can require more storage than traditional database systems. This can be particularly challenging for systems that generate a high volume of events.

Use Cases:

  • Financial applications, such as trading systems or banking applications. In these systems, it is critical to maintain an accurate and complete history of all transactions and changes to account balances. By using event sourcing, all transactions and changes are recorded as events, which can be easily queried and audited later on. This can help with regulatory compliance and also provide a clear and complete picture of the state of the system at any given time. PayPal uses event sourcing in their payment processing system to maintain a complete history of all transactions, balances, and account changes.
  • In E-Commerce applications, where it is important to maintain a complete history of all orders, changes to orders, and customer interactions. By using event sourcing, all of these events can be stored and easily queried, allowing for detailed analytics and insights into customer behavior and order fulfillment. Amazon uses event sourcing in their retail and e-commerce systems to maintain a complete history of all orders and customer interactions.
  • Others — Uber: Uber uses event sourcing to manage and track ride requests, driver assignments, and trip histories in their transportation platform. LinkedIn: LinkedIn uses event sourcing in their messaging system to maintain a complete history of all messages, conversations, and updates.

Definitions:

Materialized View pattern : Generate prepopulated views over the data in one or more data stores when the data isn’t ideally formatted for required query operations. This can help support efficient querying and data extraction, and improve application performance.

In Next blog:

  • Event Sourcing alternatives.
  • Example code
  • Why is it more scalable
  • Internals: Architecture in Detail , How is it implemented

--

--