Event Sourcing

 

Introduction


Event Sourcing has been used in production by some of the world's biggest companies (including Netflix and Walmart) for years, providing them with the platform to rapidly scale, iterate and evolve their systems, and establishing a data model that delivers a strong competitive advantage.

In this guide, we discuss what Event Sourcing is, why you'd use it, the range of benefits it provides and we break down the jargon.

What is Event Sourcing?


Event Sourcing is an architectural design pattern where changes that occur in a domain are immutably stored as events in an append-only log.

This provides a business with richer data as each change that occurs within the domain is stored as a sequence of events which can be replayed in the order they occurred. This means you’re able to see more than just the current state of your domain - you can see what lead up to the current state. 

ES-Basics-Diagram-2

In addition, as events also contain the context of the change – the ‘what’, ‘when’, ‘why’ and ‘who’ - an event-sourced system has a wealth of information that can be incredibly valuable to the business.

Event Sourcing has a wide variety of applications across many sectors, including finance, logistics, healthcare, retail, government, transport, video game development and many more.

An Event Sourcing Example

Let's look at an order process as an example to further explain how Event Sourcing works:

A customer places an order and an invoice is raised with the order details. The current status of the domain is 'outstanding' with the amount the customer owes, in this case '$200'. 

The customer receives the invoice and pays the bill, the current status is then updated to show the outstanding balance as zero. ES-Basics-Diagram-1

In an event sourced system, the status change would be captured as an event 'OrderPaymentReceived' and stored in the append-only log in the order in which it occurred.

ES-Basics-Diagram-3

Another example, is an order discount. Let's say the customer placed an order, then a discount was applied. In a system that only captures the current state, you'll see the 'Outstanding' amount changed from $200 to $150 and won't know why. In an event sourced system, the change is captured in the event 'DiscountApplied', giving the context of the change - in this case a discount was applied.

ES-Basics-Diagram-5

This is how the event 'DiscountApplied' would look when implemented in code using C#.

public string OrderId { get; set; }
	public double DiscountAmount { get; set; }
	public double NewTotalAmount { get; set; }
C#

Eventual Consistency

Eventual consistency is the idea that, in a distributed system, all the different parts of the system will eventually return all the same values. It is a form of weak consistency; not all the parts of the system may return the same values, and certain conditions will need to be met or actions taken in order to make sure all the different parts of the system are consistent. 

There’s a misconception that an eventually consistent system will be inaccurate due to the time delays involved. The time taken to return the same values may not be defined within the system, but the time frames are within the millisecond to seconds range, rather than large spans of time.     

No matter what kind of database or structure you use, eventual consistency is something you will have to deal with: it’s not a problem specific to Event Sourcing. There will always be a delay between an input being received, being recorded to storage, then called out again. One of the first misconceptions about Event Sourcing is that eventual consistency will be a major problem. This is no more of a problem for Event Sourcing as it is for any other pattern of storing data, and handling it will depend on your use case. Depending on the event store, implementation changes (do not have to be) eventually consistent. Most of the stores allow strong consistency when appending events.


Comments

Popular posts from this blog

Maxpooling vs minpooling vs average pooling

Percentiles, Deciles, and Quartiles

Understand the Softmax Function in Minutes