Event Sourcing in Practice: Build a CQRS Projection From an Event Log
In event sourcing the event log is the source of truth and every read table is a projection derived from it. You build that projection - replay the log in order, apply each event, reject the invalid ones - and prove the rebuild gives the same balances every time.
Event sourcing and CQRS in one paragraph
Instead of updating a row in place, the system appends an event that says
what happened: ACCOUNT_OPENED, MONEY_DEPOSITED, MONEY_WITHDRAWN. The
current state is computed by replaying those events. CQRS (Command Query
Responsibility Segregation) is the split that follows: writes append to the
log, reads go to projections built for querying.
A projection is a pure replay
def rebuild_projection():
cur.execute("TRUNCATE accounts")
cur.execute("SELECT id, aggregate_id, type, payload FROM events ORDER BY id")
for event in cur.fetchall():
try:
apply_event(event, cur)
except ValueError:
continue # invalid event: skip it, do not stop the replay
conn.commit()
Three rules keep it correct:
- Order matters. Replay
ORDER BY id. A withdrawal applied before the deposit it depends on gives a different (wrong) state. - Validate in
apply_event. Opening an account twice, depositing to an account that does not exist, or withdrawing below zero are rejected, so a bad event cannot corrupt the projection. - Deterministic. The same log always produces the same table. That is what lets you drop a projection and rebuild it at any time.
Where it is used, and what it costs
Event sourcing fits domains where history is the product: ledgers, orders, inventory, audit trails. The costs are real: queries need projections, a full replay gets slow as the log grows (production systems snapshot state at checkpoints), and event schemas have to be versioned with care.
What you do in the project
You implement apply_event and rebuild_projection against a Postgres
event log that includes two invalid events, then rebuild the projection
twice and confirm the balances match exactly.
Do real production work, free. Start with Junior projects in a live cloud workspace - every project you complete lands on your portfolio.
Get started - freeWhat you'll practice
- Replaying an append-only event log in order to derive state
- Validating events and skipping invalid ones without stopping the replay
- Proving a projection rebuild is deterministic
FAQ
What is event sourcing?
Event sourcing stores every change as an immutable event in an append-only log instead of overwriting rows. The current state is derived by replaying the events, so the full history is always available.
What is a projection in CQRS?
A projection is a read model built from the event log, shaped for the queries the application needs, such as a table of current account balances. It can be dropped and rebuilt from the log at any time.
Is event sourcing the same as CQRS?
No. CQRS separates the write model from the read model; event sourcing stores state as a log of events. They are often used together because an event log is a natural write model and projections are natural read models.
Keep learning
Do real production work, free. Start with Junior projects in a live cloud workspace - every project you complete lands on your portfolio.
Get started - free