What's an Event Stream

Most of the software applications need some kind of persistence mechanism or database so that the information can be retrieved later on even after restarting the software.

In event sourcing based systems, the most important storage is the event storage that keeps the event log and it's organized in event streams.



An event stream can be defined as a group of related events where the order is respected. When using an event storage, we usually create streams associated to a single instance of an aggregate, so that later on we can access this stream, read one by one the events in order and hydrate them in an instance of our aggregate in order to do something with it (e.g: validate a new transaction as per business rules).

If we have an aggregate called SalesOrder which produces events such as ProductAdded, ProductCancelled, PaymentAccepted, etc. we would have a stream for every instance of this aggregate. Personally I like naming my streams with the aggregate's name followed by its unique identifier, so if we had a look at the event storage we could find event streams such as:

SalesOrder-00000000-0000-0000-0000-000000000001

SalesOrder-00000000-0000-0000-0000-000000000002
SalesOrder-00000000-0000-0000-0000-000000000003

and each one of these streams would contain events that affected that specific instance of the SalesOrder.

As a general rule, the best way to organize streams in an event storage is in such a way as they want to be read. 

In most of cases in event sourcing you'd want to replay all the events for a specific stream in order to instantiate an aggregate for each write operation, but that does not mean that you can't play with event streams in a different way. Maybe some people find value in storing event streams in a cross-aggregate manner. As an example let's think of a stream that contains all the events related to email addresses within an application. Wouldn't you think there is some value in it if you want to replay all these events in order to have a projection where you can validate whether an email already exists?

As always, there are many ways to achieve the same goal, and the real value is in doing so in an efficient and simple way by choosing the right trade-offs.

Comments