CQRS & event sourcing

Send commands that add events to a log, and watch a read view built from those events. Move the slider to rebuild the balance as it was after any earlier event.

Step through one command

The bank account acct:42 has 3 events that add up to $100. Pick a command and when the app reads the balance, then press Next step. Back takes you to the step before.

The command
The app reads the balance view

On: the view is thrown away and built again by replaying every event.

With these settings: The withdrawal became event 4, and the account is now $70. The read at 50 ms showed $100, an old balance, because the view caught up only at 200 ms.

replayBank app, acct:42Bank appacct:42Write side, checks rules, NOWWrite sidechecks rulescheckingNOWEvent store, only addsEvent storeonly addsBalance view, read sideBalance viewread sideProjector, builds viewsProjectorbuilds viewsBroker, eventsBrokerevents
  • Bank app — acct:42 — healthy
  • Write side — checks rules — healthy — NOW — checking
  • Event store — only adds — healthy
  • Balance view — read side — healthy
  • Projector — builds views — healthy
  • Broker — events — healthy
  • Command
  • Read
  • New event or view update
  • Published event
Step 1 of 7

Step 1 of 7 · Write side

Done: The app sends a command

What happens
The command asks to take $30 from acct:42.
Why
A command is a request to change something, so it can still be refused.
What changes
The write side starts to check it.
Next
The write side loads the account's events

Event log

6 more to go. Press a line to go back to that step.

Events in the store
3
Write side
$100
Balance view
$100
matches
The app read
not yet

Step 1 of 7: Write side. The app sends a command. The command asks to take $30 from acct:42.

What this simplifies. This run follows one account, with whole events and a stated 200 ms lag. A real projector's lag depends on the broker and the load. A real replay of millions of events takes a long time, which is one reason teams keep snapshots.

Try it yourself

The same account, acct:42, starting at $100. Send commands in any order, then move the slider to see the balance after any earlier event, or rebuild the view from every event.

Materialized Balance
$100
Account Owner: Alice
Total Stored Events
3 immutable events
Append-only, never updated
Applied Read Version
v3 / v3
Projection sync status

Write Model: Immutable Event Store

APPEND-ONLY LOG
v1 · ACCOUNT_OPENED10:00:00 AM
Payload: {"accountId":"acct:42","initialBalance":80,"owner":"Alice"}
v2 · MONEY_DEPOSITED10:05:00 AM
Payload: {"accountId":"acct:42","amount":50}
v3 · MONEY_WITHDRAWN10:10:00 AM
Payload: {"accountId":"acct:42","amount":30}
Dispatch Domain Commands:

Read Model: Materialized View

OPTIMIZED FOR READS
Account ID:acct:42
Account Owner:Alice
Current Balance:$100
Snapshot Version:v3
Time Travel State Slider:Version 3

CQRS & Event Sourcing Audit Log

[5:35:56 AM]CQRS & Event Sourcing Engine initialized. Read model projected up to Version 3 ($100).