Skip to content
Darin Larimore
All posts

Event sourcing versus the activity log I actually use

6 min read
On this page
  1. The activity log
  2. The ledger
  3. What full event sourcing would add
  4. The migration argument
  5. One known gap
  6. The short version

I have been reading about event sourcing, the pattern where you store what happened as a sequence of events and derive the current state by replaying them, instead of storing the state and updating it in place. It sounded familiar, because every app I have built in the last few years has an append-only activity table with an actor, a timestamp and a description on every row. I assumed I had been doing a casual version of event sourcing. I had not. The two tables in my ordering portal that look alike are doing different jobs, and comparing them is the clearest way I have found to explain the pattern.

The activity log

Every record in the portal has an activity trail. Orders, products, vouchers, users, funding sources, categories. The entry point is one static method.

Activity::record($order, ActivityType::StatusChanged, 'Order marked shipped', [
    'from' => 'processing',
    'to' => 'shipped',
]);

The type is an enum with about thirty cases, from Created and Updated through Drawdown, Shipment, LoggedIn, TwoFactorEnabled and ImpersonationStarted. The actor resolution has one rule I care about: during a "view as customer" session the row is attributed to the manager doing the impersonating, never to the customer. There is a withoutRecording wrapper for bulk imports so a migration of ten thousand orders does not write ten thousand "created" rows nobody will read.

Here is the test that tells you what kind of table it is. Truncate it. The app keeps working. Orders still have statuses, vouchers still have balances, users still have roles. You have lost the story of how things got that way, which matters to the humans running the program, but no state depended on it. The description is free text and the metadata is whatever the caller felt like attaching. It is written for a timeline component, not for a computer to reconstruct anything.

The ledger

Voucher balances are the other table. A voucher is the funding a partner organisation draws down when it orders supplies, and every change to a balance is a row in voucher_transactions.

/** Append-only ledger entry. Never updated or deleted, corrections are new rows. */
class VoucherTransaction extends Model
{
    protected $fillable = ['voucher_id', 'order_id', 'amount', 'balance_after', 'reason'];
}

The balance is the sum of the rows.

public function balance(): float
{
    return (float) $this->transactions()->sum('amount');
}

There is also a current_amount column on the voucher, but it is a cached mirror. The only method allowed to touch it writes the ledger row and refreshes the mirror inside one database transaction, so they cannot disagree. Placing an order writes a debit, refunding writes a credit, a manual adjustment writes whichever with a reason. Nothing is ever edited. If an admin entered the wrong number, the fix is another row.

Run the same test. Truncate voucher_transactions and every balance in the system is gone, because the balance was never stored as a fact, it was derived from the events. That is event sourcing, scoped to one aggregate. The rows are the truth and the balance is a projection, kept in current_amount for cheap reads and rebuildable at any time with a SUM.

What full event sourcing would add

If I took the ledger idea and applied it everywhere, an order would not have a status column. It would have OrderPlaced, OrderShipped, OrderRefunded events, and the status would be computed by replaying them. Every aggregate would work that way, every event would carry a schema version so old rows could still be replayed after the code changes, and every list page would read from a projection that a worker keeps up to date.

I have not needed that, and the cost is real. You cannot UPDATE your way out of a problem. Every read model is something you maintain. Reporting against raw events is painful, which is why the projections exist, and then you are maintaining those too. For a portal where the heart of the product is "did this partner have the funding, and what is left", the ledger gives me the one thing event sourcing is really for, an auditable derivation of the number people argue about, and plain Eloquent handles the rest.

The migration argument

The strongest case against replaying everything came from the data migration. Both legacy stores, Craft Commerce and WooCommerce, had years of plugin rows that looked like events: voucher redemptions, wallet debits, store-credit coupon uses. I tried to replay them into the new ledger and the totals did not reconcile with the balances the clients were actually honouring. Plugins had been swapped, refunds had been applied by hand, some rows were missing.

So every imported voucher got exactly one opening-balance transaction equal to the legacy balance, with the history imported as plain order records that draw nothing down. Replaying would have manufactured a history that never happened. Event sourcing only pays off when the events were trustworthy at the moment they were written. On a greenfield app you can promise that from day one. On a decade of inherited data you cannot, and a single honest opening balance beats a thousand reconstructed events.

One known gap

The ledger reads the current balance and then writes the new row, inside a transaction but without a row lock on the voucher. Two orders placed against the same voucher in the same instant could both read the same balance. At this program's volume, a few orders a day per partner, I have accepted that. The fix is a lockForUpdate() on the voucher at the top of recordTransaction, and writing it down here is my way of not forgetting it.

The short version

An activity log is for people. It explains what happened and who did it, and nothing is computed from it. A ledger is for money, or anything else where the derivation has to be auditable: append-only rows, a derived total, a cached mirror. Full event sourcing is what you reach for when replay is a requirement, not a nicety, and when you control the events from the first one.