hardSystem Design

What are event sourcing and CQRS?

823 views
01

Understand the problem

Explain event sourcing and CQRS.

event-sourcingcqrs
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Derive state by folding events
Run Playground
# Event sourcing: events are the source of truth; state is a fold of them.
events = [
    {"type": "Opened",    "balance": 0},
    {"type": "Deposited", "amount": 100},
    {"type": "Withdrew",  "amount": 30},
    {"type": "Deposited", "amount": 50},
]

def project(events):                       # build a read model from the log
    balance = 0
    for e in events:
        if e["type"] == "Opened":     balance = e["balance"]
        elif e["type"] == "Deposited": balance += e["amount"]
        elif e["type"] == "Withdrew":  balance -= e["amount"]
    return balance

print("current balance:", project(events))   # 120
# You can replay to any point in time, audit every change, or build a
# brand-new projection from the same events.
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.