What I Learned So Far
I built ActaDB to find out how far a store this simple gets. These are the points I would want to know before using it. The numbers come from my machine, a Mac with Apple silicon.
The Append Stays Fast
Appending an event took about 6 ms at every store size I tested, from 1,000 to one million events. 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.
The Read Grows
The simplest way to use the store is to read all events of the queried types for every request. I call this way one. A request then gets slower as the store grows. In a warehouse example a reservation reads the events of four types, computes the available quantity and appends. This is what a typical reservation took.
- 7.5 mswith 1,000 events in the store
- 47.8 mswith 100,000 events in the store
- 480 mswith one million events in the store
For scale: an application that stores 1,000 events per day holds 100,000 events after 100 days and one million after 2.7 years.
A Second Way
There is an option for this. In way two the application keeps what it has read and asks the store only for the events that were stored since its last read. The time of a request then no longer depends on the size of the store.
The email example runs both ways with 100,000 registered addresses. This is what a request took there.
- 37–46 msin way one, for every request
- 6.1 msin way two, for a request that registers an address
- 0.01 msin way two, for a request that finds the address taken
The 6.1 ms are the time of the append. Only the first request of way two reads all registered addresses, and it took 52 ms.
The Difference
Both ways keep the same rule, because both append with the token of their read. They differ in what they ask of the application.
Way one
- It reads all events of its types for every request.
- It keeps nothing in the application.
- A request gets slower as the store grows.
Way two
- The first request reads all events of its types. Every later request reads only what is new.
- It keeps its result in the memory of the application. After a restart the first request reads all events of its types again.
- The time of a request does not depend on the size of the store.
- With several threads it needs one lock per request.
One Lock per Request
With several threads, way two needs one more thing. The kept result and its checkpoint have to sit behind one lock, and a request holds this lock from its read to its append. When the lock is released before the append, requests collide under load and some of them are not processed. With the lock around the whole request they do not conflict with each other.
Requests of one kind then run one after the other. The store writes one batch at a time anyway, so they lose nothing by it.
Conflicts and Retries
A query by event type has a wide conflict area. Every new event of one of these types counts as a conflict, also when it belongs to an unrelated item. At the traffic of an in-house application this is rare. In a test with two commands per second and 100,000 events in the store, 315 of 360 commands finished in the first attempt, and two were not processed.
Under heavy load it looks different. At ten commands per second and the same store size, 36 of 360 commands were not processed.
The Limits
- Several instances of an application can work on the same storage file. I tested this on one machine. I have not tested a file on shared storage yet.
- Requests of two instances can conflict with each other, because every instance has its own lock.
- The store writes one batch at a time. That gave about 150 writes per second on my machine.
- A query loads all matching events into memory.
- In way one a request gets slower with every event of its types.
If You Outgrow It
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 or with tags. ActaDB does not offer these today.
Count the events your own application stores per day, and check how long it takes to reach these sizes.