ActaDB

How It Works

ActaDB has few parts. This page goes through them in the order in which a request uses them.

An Event Is Three Things

The store keeps a position, a type and some bytes for every event. The position is set by the store and grows by one with every event. The type is a name you choose, such as EmailRegistered. The bytes are your payload, and the store never opens them.

  1. 1EmailRegisteredada@example.com
  2. 2EmailRegisteredbob@example.com
  3. 3EmailRegisteredcleo@example.com
  4. 4EmailRegistereddan@example.com
Four events as the store keeps them: position, type and bytes. The store also notes the time at which it recorded each event.

There is one sequence for the whole application. An event does not belong to a stream or to an aggregate, and it carries no tag.

Read by Event Type

A query names event types and nothing else. It returns all events of these types in the order of their positions.

I used to query into the payload, and others use tags to find the events of one item. Both need decisions before the first event is written, and in my experience such upfront decisions are mostly assumptions. A query by type needs none. It returns more events than one request needs, so every change that matters to the request is part of the result.

The application then picks what belongs to its case. To register an email address, it looks for this address among the EmailRegistered events.

Append With a Condition

A read also returns a token. The token stands for what you read: the event types, and the point up to which the store was read. You cannot open it. You hand it back with the append.

The store then checks one thing inside the write. Was an event of these types stored after this read? If not, it stores the new events. Otherwise it stores nothing and answers with a conflict. Only one write runs at a time, so nothing can be stored between the check and the write.

Here two requests want to register an address at the same time.

  1. Request Areads the registered addresses.
  2. Request Breads them as well.
  3. Request Bappends its event. The store answers appended.
  4. Request Aappends with the token of its read. The store answers conflict and stores nothing.
  5. Request Areads again, sees the event of request B and computes its result once more.

The condition looks at the event types and not at the address. If request B registers bob@example.com, request A gets a conflict as well, although it wants ada@example.com. Request A reads again, sees that its address is still free, and its second append is stored.

A conflict is a normal answer and not an error. The application reads again and tries once more. In my examples a request gives up after three attempts and reports that it was not processed.

Read Only What Is New

Reading all events of a type for every request is the simplest way, and it gets slower as the store grows. Therefore a read also returns a checkpoint. If you keep what you built from the events, you hand the checkpoint to the next read and get only the events that were stored after it.

let selection = match &known.checkpoint {
    None => store.query(&Query::event_types(["EmailRegistered"]))?,
    Some(checkpoint) => store.query_after(checkpoint)?,
};
  1. 1EmailRegisteredada@example.com
  2. 2EmailRegisteredbob@example.com
  3. 3EmailRegisteredcleo@example.com
  4. 4EmailRegistereddan@example.com
  5. Checkpoint of the last read
  6. 5EmailRegisteredeve@example.com
  7. 6EmailRegisteredfinn@example.com
The next read returns only the two events after the checkpoint.

The store keeps nothing for you here. What you built and the checkpoint stay in your application. After a restart the first read gets all events of your types again.

What the Store Leaves Out

The page Examples shows all of this in one small program.