Simply store what happened.
ActaDB is an embedded event store for Rust. It keeps the events of an application in one file, in the order in which they were stored, and it never changes or removes one.
- 1EmailRegisteredada@example.com
- 2EmailRegisteredbob@example.com
- 3EmailRegisteredcleo@example.com
- 4EmailRegistereddan@example.com
Every event gets the next position. A stored event stays as it is.
Two Functions
The store has two functions, query and append. A query returns the events of the event types you ask for. An append stores new events, and it can carry one condition: store them only if no event of these types was stored since my read.
for _attempt in 1..=3 {
let selection = store.query(&Query::event_types(["EmailRegistered"]))?;
// compute your result and your event from selection.records
match store.append(vec![event], Some(selection.token))? {
Commit::Appended { .. } => return Ok(Registration::Registered),
Commit::Conflict => continue,
}
}
Ok(Registration::NotProcessed)
The token comes from the read and goes into the append. If another event of these types arrived in between, the store answers with a conflict and stores nothing. The application then reads again and tries once more. Here it gives up after three attempts and reports that the request was not processed.
This condition is all an application needs to keep a rule. An email address is registered only once, and stock is reserved only while it is available.
The Idea Behind It
Event Sourcing should be the standard way to build business applications. An application that stores what happened keeps every fact, and it can answer questions that nobody asked when the events were written. Most teams I talk to still avoid it, because the usual setup brings many concepts and a lot of technology before the first event is stored.
ActaDB needs none of this. An event is a position, a type and some bytes. The store never opens a payload, so you write the first event without designing tags or a payload index.
What It Is Made For
ActaDB is made for business applications that are built from events. You store what happened and write one feature slice, then the next one. Nothing has to be designed up front. An append and a request that reads only what is new take the same time at every store size I measured, and the page Insights has the numbers.
One more point decides whether it fits. A condition looks at event types and not at single items. If two requests work on different items with the same event types at the same time, one of them gets a conflict and reads again. The page How it works shows this with two email addresses.
Several instances of an application can work on the same storage file. The page Insights shows what I measured and where the limits are.
Get Started
ActaDB is young. Its version is 0.1.0, it is not on crates.io yet, and its interface can still change. Until it is published you can use it from the repository.
Create a project with cargo new and add ActaDB to its Cargo.toml.
[dependencies]
actadb = { git = "https://github.com/ricofritzsche/actadb" }
This small program stores one event and reads it back. It goes into src/main.rs.
use std::path::Path;
use actadb::{Event, EventStore, Query, StoreError};
fn main() -> Result<(), StoreError> {
let store = EventStore::open(Path::new("events.redb"))?;
let event = Event {
event_type: "EmailRegistered".to_string(),
payload: b"ada@example.com".to_vec(),
};
store.append(vec![event], None)?;
let selection = store.query(&Query::event_types(["EmailRegistered"]))?;
for record in &selection.records {
println!("{} {}", record.position, record.event_type);
}
Ok(())
}
Run it twice with cargo run. The first run prints one record.
1 EmailRegistered
The second run prints two.
1 EmailRegistered
2 EmailRegistered
The second run stored the address again. None means that the append has no condition, so the store takes every event. To keep an address unique, the append needs the token of a read, as shown above.
The payload is bytes. ActaDB does not look inside, so you can use JSON, MessagePack or plain text. The tool actadb-inspect lists the records of a file, also while the application runs. With --follow it stays open and prints every new record. It shows MessagePack as JSON, text as text and everything else as hex.
Next, read how it works or run the example.
The Name
Acta is Latin for “things that have been done”. In ancient Rome the Acta Diurna were the daily public notices of what had happened in the city, posted in places such as the Forum. ActaDB keeps this kind of record for an application: what happened, in the order it was stored.