Explain event sourcing and CQRS.
Skip to solutionKEEP THE
hardSystem Design
What are event sourcing and CQRS?
823 views
01
Understand the problem
event-sourcingcqrs
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Step 1: Outline use cases and constraints
Gather requirements and scope the problem. Ask questions to clarify use cases and constraints. Discuss assumptions.
Use cases
We'll scope the problem to handle only the following use cases
- User needs to understand
event-sourcingto make architecture de
Solution ready — 2 min read
Classified // press E to declassify
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.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 56 of 99 decoded in the System Design track. One more won't hurt.