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.

Get started - freeMid and Senior open as you complete Junior projects
Data Engineerevent-sourcingcqrsprojection

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:

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 - free

What you'll practice

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

The Outbox Pattern (Reliable Events)Data projectHow to Build an Exactly-Once Kafka PipelineData projectIdempotent ETL Pipeline (INSERT ON CONFLICT)Data projectData roadmapStep by step to hiredData interview questionsSTAR answersAll Data projectsProjects hub

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
Get started - free