Event-sourced rebuild demo - account
A domain-neutral demonstration of event-sourced rebuild: a bank account whose balance
survives a restart by replaying its event log, with no in-memory snapshot. In init the
thrall rebuilds the balance from its event log; each deposit / withdraw appends an event
and updates the balance.
The event log is an opt-in retention JetStream stream (event_log = true in the manifest),
separate from the durable mailbox. With a persistent embedded JetStream (store_dir), the log
- and therefore the rebuilt state - outlives stopping and starting
aether up.
The same account is written in all three languages - main.go, account.ts, account.py -
with identical behaviour. Pick one manifest below; the interaction afterwards is the same.
Run
Build the runtime once, then run one of the three variants.
export GOTOOLCHAIN=local
mise exec go@latest -- go build -o bin/aether ./cmd/aether
Go (aether.toml):
mise exec go@latest -- go build -o bin/account ./examples/eventsourced
cd examples/eventsourced
../../bin/aether up -f aether.toml
TypeScript (aether-ts.toml):
bun install
cd examples/eventsourced
../../bin/aether up -f aether-ts.toml
Python (aether-py.toml):
cd examples/eventsourced
python -m venv .venv
.venv/bin/pip install -r requirements.txt
../../bin/aether up -f aether-py.toml
In another shell (identical for every variant):
cd examples/eventsourced
../../bin/aether cast account deposit '{"delta": 100}'
../../bin/aether cast account withdraw '{"delta": 30}'
../../bin/aether call account balance # -> 70
What to look for
Stop aether up (Ctrl-C) and start it again, then read the balance:
../../bin/aether call account balance # -> 70, rebuilt from the event log
The balance is the same after the restart even though nothing snapshotted the in-memory state -
it was reconstructed by replaying the appended events. (Deleting ./.aether-store wipes the
persisted log, so the next start rebuilds from an empty log back to 0.)
Command-key (dedup)
Each handler appends with a dedup key derived from the message id
(ctx.MsgID / ctx.msgId / ctx.msg_id):
ctx.Append(event, thrall.DedupKey(ctx.MsgID))
A signed delta is not idempotent - if the same cast is delivered twice (the mailbox is
at-least-once), a plain append would count it twice, and the fold cannot tell a replayed event
from a genuine second deposit. Keying the append on the message id makes the redelivery collapse
to a single event: two appends with the same Nats-Msg-Id land as one.
This dedup holds within the stream's duplicate window (2 min by default, set via
event_log_dedup_window_ms), not forever - it guards against real-time retry/redelivery, not
against replaying the log days later. The window is applied when the stream is first created; on
a persisted store (./.aether-store) raising it takes effect only after the store is recreated. It also keys on the message id, so it deduplicates the same
message, not two logically identical commands a client sends as two separate casts (those are two
commands, each with its own id). For client-driven idempotence, pass a domain idempotency key as
the dedup key instead. See DESIGN.md, "Command-key: dedup on the event log".