Learn / System Design
Continue where you left off ×
The log is the truth (event sourcing and CQRS)
The Ledger Everyone Reads · Open board
The book on the counter that nobody may erase. That lesson used the book to tell many readers what happened. This one goes a step further and asks what happens if the book is not a copy of the truth but the truth itself.
Before you start. You need The Ledger Everyone Reads (event-driven & the log) : a log is a book you only add to, and any reader can read it from the top. You need Charged Twice (idempotency & exactly-once) : the same message can arrive twice, so a reader must cope with repeats. And the sibling lesson The write that has to happen twice (CDC and the outbox) showed how to get changes out of a database into such a book. Here the book comes first and the database is built from it.
The idea in 30 seconds
Event sourcing stores the list of things that happened, such as “deposited 500”, instead of the current number, and works the current number out from the list.
An event is a fact in the past tense, and the event store only ever has new events added to the end, never edited or deleted.
Replay means starting from nothing and applying every event in order to rebuild the state, and stopping early gives the state at any earlier moment.
A projection is a stored view built from the events for one screen or report, and it can be deleted and rebuilt, because the events are the truth.
CQRS splits the part that takes requests and changes things from the part that answers questions, so each can be shaped for its job.
The price is that reads can lag behind writes, and mistakes are fixed by adding a correcting event, not by editing.
Read the story
Lesson 29 of 73 in System Design
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year , or $19.50 for your first year with LAUNCH50 , ends 1 Nov, 18:30 UTC .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1494: a Venetian merchant who may not use an eraser Most software keeps the number and throws away the story 2005: Fowler writes down the habit, and names the lines events Replay: the balance is the lines added up The projection: a view you can throw away CQRS: writing and reading become two different jobs What it costs Terms it explains: event, event store, replay, projection, CQRS, command, query, snapshot.
Review System Design
Mark as read Saved in this browser, and to your account when you sign in.