Get your tea, and think about your bank passbook for a minute. A passbook does not just say “balance: ₹42,300”. It lists every deposit and withdrawal, with dates. The balance is something you work out from the entries. If the balance and the entries ever disagree, you trust the entries.
That is the whole idea behind event sourcing. Everything else is detail, and some of the detail is where people get hurt.
State versus history
Most applications store current state. A row in an accounts table has a balance column. When money moves, you update the number. The previous value is gone, unless you built a separate audit table, which people usually do later and imperfectly.
Event sourcing flips this. You store the sequence of things that happened as immutable events: AccountOpened, MoneyDeposited (₹5,000), MoneyWithdrawn (₹1,200). Events are named in the past tense because they are facts. You never update or delete one. To find the current state, you replay the events from the start and apply each in order. A mistake is fixed by adding a new event (a correction), not by editing history.
What do you get for this? Several things that are awkward otherwise:
- A complete, trustworthy audit trail, because the log is the data, not a copy of it.
- The ability to answer “what did this account look like on 3 March?” by replaying up to that date.
- The ability to build new views of the data later, from history you already have. If finance decides next year they want a report nobody planned, you can generate it from the events.
- A natural stream of events for other systems to react to.
The obvious worry, and snapshots
“Replaying thousands of events to show a balance sounds slow.” It would be, so systems take snapshots: every so often (say every 100 events) they save the computed state with the position it reached. To load an account, read the latest snapshot and replay only the events after it. For many business entities, which have tens or hundreds of events rather than millions, replay is fast even without snapshots.
Where CQRS comes in
Command Query Responsibility Segregation, a term from Greg Young building on Bertrand Meyer’s earlier command-query separation, means using different models for changing data and for reading it. A command says “do this” (withdraw ₹1,200) and may be rejected if rules fail. A query asks for information and changes nothing.
It pairs naturally with event sourcing. A raw event log is a poor thing to query: “show me all accounts with a balance above ₹1 lakh” would mean replaying everything. So a separate process listens to the events and maintains read models, ordinary tables shaped exactly for the screens and reports that need them. The write side is the event log; the read side is one or more tables kept up to date from it.
The catch is that the read side usually lags behind the write side by a short time. A user who deposits money and immediately refreshes the page might see the old balance. This is eventual consistency, and you have to design the user experience around it, for example by showing a “processing” state or returning the new value from the command itself.
Note that you can use CQRS without event sourcing, such as a normal database for writing and a search index for reading. And you can, in principle, use event sourcing without a fancy read side. They are separate ideas that happen to fit together.
What it costs
Here I want to be direct, because blog posts about this pattern are often too enthusiastic. Event sourcing has real costs.
Events live forever, so their shape matters forever. If you decide in year two that MoneyDeposited needs a currency, what happens to the million old events without one? You need versioning and “upcasting” logic that translates old events when they are read. Changing a database column is easy by comparison.
Querying is indirect, as discussed. Reports need projections, and projections need building, monitoring and sometimes rebuilding from scratch when you find a bug in them.
Deleting data is hard. Privacy rules such as India’s Digital Personal Data Protection Act, 2023 give people certain rights over their data, including erasure in defined circumstances, and an append-only log that you promised never to change sits awkwardly with that. Teams use techniques like keeping personal details outside the events, or encrypting them with a per-person key that can be discarded. This needs design up front, and you should take legal advice on what your obligations actually are.
Finally, there is the plain learning curve. Most developers know how to work with tables. Debugging a system where state is derived and the read side is lagging is a different skill, and a new team will be slower for months.
So when should you use it?
When the history is the business. Ledgers, payments, trading, inventory movements, anything in which “how did we get here?” is a first-class question and auditors will ask it. Also where the same facts must feed many downstream consumers.
When should you avoid it? For ordinary create-read-update-delete applications: a staff directory, a product catalogue, a settings screen. A table with a few audit columns, or a history table populated by triggers or the application, gives you most of the benefit at a fraction of the effort. A useful middle path is to apply event sourcing only to the one part of your system that needs it, such as the ledger, and keep everything else conventional.
If your team is choosing it mainly because it sounds modern, that is the signal to stop and write down what problem it solves for you.
Figures and names in the examples are illustrative.
Leave a Reply