ActaDB

Every Seat Only Once

Three desks book seats. Every desk is its own process, and all three write to the same event store. A seat is booked only once, also when all three desks ask for it at the same moment.

Watch It

The video shows one run of the example in about a minute. The stream of events is at the top of the window, and the three desks are below it.

One run of the example, recorded in a terminal. The video has sound.

Run It

You need tmux, because it shows the four programs in one terminal window. The README of the example says how to install it on macOS, Linux and Windows. Then you run one script from the root of the repository.

git clone https://github.com/ricofritzsche/actadb
cd actadb
examples/seat-booking/run.sh

The script builds the example, creates an event store in the temporary folder of your system and opens the window. When you end the example, it removes the event store again.

Book a Seat

Desk one has the cursor when the window opens.

  1. Type A1 and press Enter. Desk one answers booked A1 at position 1, and the event appears in the stream.
  2. Press Ctrl-b and then the right arrow key to change to desk two, and type A1 again. Desk two answers taken A1 by desk 1, and nothing new is stored.
  3. Press Ctrl-b and then a. From now on every key goes to all three desks at the same moment. Type B2. One desk books the seat, and the other two answer conflict, reading again and then taken.
  4. Type quit at a desk to end the example.

The Whole Booking

A desk is one program in one file. This function is the whole booking.

/// Reads all bookings, checks whether the seat is among them, and appends the booking
/// with the token of the read. The store refuses the append when another desk stored
/// a booking after the read. The desk then reads again.
fn book(store: &EventStore, desk: &str, seat: &str) -> Result<Booking, StoreError> {
    for _attempt in 1..=3 {
        let selection = store.query(&Query::event_types([SEAT_BOOKED]))?;
        let taken_by = selection.records.iter().find_map(|record| {
            let payload = String::from_utf8_lossy(&record.payload);
            let (booked_seat, by) = payload.split_once(" by ")?;
            (booked_seat == seat).then(|| by.to_owned())
        });
        if let Some(by) = taken_by {
            return Ok(Booking::Taken { by });
        }
        // The payload of the event is the seat and the desk, for example "A1 by desk 2".
        let seat_booked = Event {
            event_type: SEAT_BOOKED.to_owned(),
            payload: format!("{seat} by {desk}").into_bytes(),
        };
        match store.append(vec![seat_booked], Some(selection.token))? {
            Commit::Appended { first_position, .. } => {
                return Ok(Booking::Booked {
                    position: first_position,
                });
            }
            Commit::Conflict => println!("  conflict, reading again"),
        }
    }
    Ok(Booking::NotProcessed)
}

Notice that the desks do not know each other. A desk reads all SeatBooked events and appends with the token of its read. When another desk stored a SeatBooked event after this read, the store answers with a conflict and stores nothing. The desk then reads again and answers with what it finds.

What It Stores

In the run of the video the three desks sent eight requests. This is what the event store holds afterwards, as the stream shows it.

1       1796069431732   SeatBooked      text:A1 by desk 1
2       1796069445440   SeatBooked      text:B2 by desk 1
3       1796069450625   SeatBooked      text:C3 by desk 2

Each line has the position of the event, the time at which it was recorded, its type and its payload. Desk three asked for B2 and for C3 at the same moment as the other two desks, and it was too late both times.

> B2
  conflict, reading again
  taken B2 by desk 1
> C3
  conflict, reading again
  taken C3 by desk 2

The stream is actadb-inspect with --follow. This tool lists the events of a store and then prints every new one.

Several Instances, One Store

The three desks are three instances of one program, and nothing connects them except the event store. The rule that a seat is booked only once is kept in one place, in the append with its condition. It works the same way for one process and for three.

The page How It Works explains this condition. The page Examples shows the same rule with email addresses in one process.