How Simple Can Event Sourcing Be?

Event Types Only, Measured With One Million Events

How Simple Can Event Sourcing Be?
Image licensed under the Unsplash+ License.

In the last years I was sure that my way of doing Event Sourcing was as simple as it gets. Last week a discussion in the DCB Campfire community, organized by Sara Pellegrini and Bastian Waidelich, showed me a simpler one. I was skeptical, so I built it and measured it with up to one million events.

This article was originally published here.

Tags Are Assumptions

I have written a lot about Event Sourcing without aggregates. The idea is always the same. A command reads the events it needs with a query, computes its result, and appends new events only if the result of that query still holds at write time. Ralf Westphal and I call this Command Context Consistency. Dynamic Consistency Boundaries (DCB) describe the same conditional append and add tags: the writer attaches tags to an event, and a query selects events by type and tags.

Tags were the part I argued about. A tag is a decision at write time about how an event will be found later, and in my experience such upfront decisions are mostly assumptions. Therefore my queries used the event types and the attributes of the payload directly. I knew the cost. The store has to query into every payload, so a payload can never be opaque to it, and the checks get slower with a long history and a lot of traffic. My answer was always that most applications do not need to scale from the first day, and I think many of them never will. For this reason I called tags an optimization for later. But I never rejected them.

Event Types Are Enough

The discussion was about tags, and I was invited because of my articles on this topic. Jef Wauters pushed back with an argument I did not expect. If I want it pure, he said, it is enough to query the event types a command needs. The query then needs neither tags nor a look into the payload.

I was skeptical. A query by event type returns the events of all items, and every new event of one of these types counts as a conflict, also when it belongs to an unrelated item. However, the consistency holds. The query by type returns more events than the command needs, so every change that matters to the command is part of it. The cost is a larger read and, from time to time, a retry. The DCB specification allows such a query as well: a query item can consist of event types only [1].

Take a warehouse. The capability Reserve Stock may only reserve what is available, and four event types change the available quantity of an item. This is its complete query:

Query::event_types([
    GOODS_RECEIVED,
    STOCK_RESERVED,
    RESERVATION_RELEASED,
    STOCK_ADJUSTED,
])

The capability gets the events of these types, adds up the ones for its item, and appends StockReserved under the condition that no event of these four types was stored since its read. Otherwise the store answers with a conflict and the capability reads again. After three attempts it gives up and returns “not processed”.

Figure 1: Two reservations for different items overlap. The second append ends in a conflict, and the capability reads again.

Event Sourcing as the Standard

What I like about this version is how little the store has to know. An event is a position, a type and some bytes. The store has two functions, query and append, and it never opens a payload. You do not have to design tags or a payload index before you write the first event.

Figure 2: What the store keeps for an event: a position, a type and bytes.

I think this matters beyond the discussion about tags. In my view Event Sourcing should be the standard way to build business applications. Many teams I talk to avoid it, because they see too many concepts and too much technology. Storing what happened is the simplest thing an application can do. It needs concepts and an integration that are just as simple.

The open question was whether this holds in practice. So I built a warehouse stock example with a small event store in Rust and put it under load.

Let’s Calculate

Take an in-house application that stores 1,000 events per day. Across an eight-hour working day that is one event every 29 seconds. Such an application holds 100,000 events after 100 days, 500,000 after about 16 months, and 1,000,000 after 1,000 days, which is 2.7 years.

The list shows what one call costs at these sizes. The first time is the typical call (the median). The time in brackets is the slow call (the 95th percentile). I measured on a Mac with Apple silicon, with the capabilities called directly, one call after the other.

  • 1,000 events, after one day: Reserve Stock 7.5 ms (11.1 ms), Stock Overview 0.4 ms (0.6 ms)
  • 10,000 events, after ten days: Reserve Stock 10.4 ms (13.4 ms), Stock Overview 4.4 ms (6.3 ms)
  • 100,000 events, after 100 days: Reserve Stock 47.8 ms (53.4 ms), Stock Overview 44.7 ms (51.1 ms)
  • 1,000,000 events, after 2.7 years: Reserve Stock 480 ms (1,167 ms), Stock Overview 506 ms (581 ms)

These times have two parts, the read and the append. The read is the part that grows. It took about ten times as long whenever the store held ten times as many events, because a capability reads all events of its types. I did not measure 500,000 events. With this growth I expect about a quarter of a second there.

A first optimization for the reads could be persistent projections. A query such as Stock Overview would then answer from a stored result, and the events would no longer be read on every call.

The append took about 6 ms at every size. I measured this with Receive Goods, which appends without a condition. The condition adds one check inside the same write. The store looks up in its index of event types whether an event of the queried types was stored after the read. It does not read the events again. Only one write runs at a time, so nothing can be stored between the check and the write.

In practice, this means that 95 of 100 calls finish in under 60 ms during the first 100 days. After almost three years a reservation takes about half a second.

Conflicts and Retries

Conflicts are rare at this kind of traffic. In a second test 600 commands arrived with random gaps, as a mix of reservations, receipts, counts and stock overviews. 360 of them check the stock before they store. At two commands per second and 100,000 events in the store, 315 of these 360 finished in the first attempt, 36 in the second, seven in the third, and two were not processed. At 1,000 events all of them were processed, 353 in the first attempt.

For scale: two commands per second are about 170,000 commands per day. That is far more than an application with 1,000 events per day sends.

The Limits

The same tests show the limits. At ten commands per second and 100,000 events, 36 of 360 commands were not processed. At 50 commands per second the store could not keep up at that size, and 348 of 360 were not processed. When eight threads sent reservations without a pause, two thirds of them were not processed, at every store size. In addition, this store writes one batch at a time, which gave about 150 writes per second on my machine. A query loads all matching events into memory, and only one process can open the storage file.

For many applications it might be enough to start with event types alone. If you want to optimize later, you have different options. For slow queries, persistent projections come first, because a query then answers from a stored result. For commands, you can narrow the query with values from the payload, if the payload is not opaque to the store, or with tags. A narrower query reads fewer events and conflicts with fewer commands. I think the write performance needs no optimization in many cases, because writing an event took about 6 ms at every store size.

Count the events your own application stores per day, and check in the list how long it takes to reach the last entry.

Conclusion

I come back to the core: simply store what happened. An application that does this only appends events. It needs no model of the current state that has to be designed upfront and kept correct with every change. I think many approaches of the past decades made Event Sourcing look like a complex thing. It is the simplest way I know to keep the data of an application, and it should be the standard and not the exception.

Cheers!

Sources

[1] DCB specification

Subscribe to Rico Fritzsche

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe