Here’s a debug build of this simple hex math game if you want to try it out. There’s a little blue box in the bottom right hand corner that will open up the debugger. index.html (386.0 KB)
(and for anyone who just wants to brush up on their hex and aren’t intending to play with the debugger: HexMath)
I learned about it through one of Spatie’s books, but you can get most of the important parts from the [free docs](The traditional application | laravel-event-sourcing | Spatie). It is also referred to as CQRS (command query resource separation).
The idea is that you separate user interactions (Events) from your models (projections/aggregates). Instead of doing normal CRUD, you store events, and then use event handlers to build your models from them.
The main advantages being:
If program requirements change, you can modify your projection (the code that converts events to models), and then rebuild them from “scratch”, rather than try to do a complicated data migration.
You have a complete log/audit trail of everything that’s ever happened, so you can do hard deletes without actually losing any data.
At work, I used it in a Twillio based calling app, where an employee needs to check in with our clients every X months to be sure they’re happy with us. Events come in from Twillio (call started, call failed, user hung up, etc) to build the call log, and depending on the disposition of the call, the client gets added to to the appropriate spot in the queue for their next call. If the queuing rules change, or we need to add a different queue, I just update the projection(s), drop the old tables, and rerun them.
With a standard CRUD app, making changes like that often can’t be made retroactively, because you have no historical data, only the latest iteration of it.
Probably the smartest example though is a bank balance. It’d be crazy if your balance was just an integer value that could change at any time for any reason. Instead, you have a ledger of transactions, and you add them all up to get the balance.
Do you happen to have experience with actually changing a projection retroactively? I can’t shake the feeling that doing so would take a very long time, because it sounds like you’d be replaying every event made in maybe the last 90 days. Ideally you’d do such a thing within a few minutes, but I can imagine this easily taking up to a day or even more.
P.S. what about debugging using all events? Stepping through individual events is probably quite fast, but having to reload from a daily backup and then replay all the events that day still sounds like a long process. Is this a practical concern, or is it usually quite fast to get to the relevant event?