Event-driven state in modern web apps
If you store only the final state instead of the intent, you lose the story of how your data arrived.
- Event Sourcing
- TypeScript
- Realtime
- Postgres
Traditional CRUD updates destroy history: `UPDATE accounts SET balance = balance - 50`. Event sourcing records every action as an immutable fact, giving your application an unbreakable audit trail and real-time multiplayer superpowers.
in short
Key takeaways
- 01
Events are immutable history
You can never edit the past; you can only append a new event that compensates for a mistake.
- 02
Projections are disposable
If your read model is wrong, delete it and replay the event log from timestamp zero.
- 03
Real-time becomes trivial
Pushing events over WebSockets keeps multiple open tabs in identical synchronization with zero polling.
the details
The deep dive
Why CRUD falls apart at scale
In typical applications, database tables represent the current snapshot of the world. When a bug corrupts a row, or two users perform simultaneous concurrent edits, determining what actually happened requires forensic archaeology through server log files.
With event-driven state, every mutation is an append-only event in an event log. The current account balance is simply a projection: events.reduce(applyEvent, initialBalance).
export type LedgerEvent =
| { type: "FUNDS_DEPOSITED"; cents: number; at: string }
| { type: "FUNDS_WITHDRAWN"; cents: number; at: string };
export function foldEvents(initial: number, events: LedgerEvent[]): number {
return events.reduce((balance, event) => {
switch (event.type) {
case "FUNDS_DEPOSITED": return balance + event.cents;
case "FUNDS_WITHDRAWN": return balance - event.cents;
}
}, initial);
}Accountants don't use erasers. When they make an error, they write an offsetting entry. Software engineers should do the same. ā Soren Patel, Staff architect