Writing

The history is the balance

A wallet looks like a number. That is the first mistake.

The number is useful. It tells a player what they can afford, and it gives the interface something to render. But the number is not the thing I actually care about when something goes wrong. When a transfer is disputed, a job is retried, or a season closes with an impossible total, the question is always the same: what happened?

For Deadlink Protocol, the wallet is an append-only ledger. A balance is derived from the entries. It is a small decision with a lot of consequences, and most of them are good.

A pale paper ledger tape winding through a dark server room, lit by a warm amber edge light.
A ledger is a physical idea before it is a database table. The history arrives one entry at a time. Illustration generated locally for this essay.

The balance column is a conclusion

A single balance column is easy to read and easy to update. It is also a conclusion without the argument behind it.

If two requests read the same value and both write a new one, one write can disappear. If a worker retries after a timeout, a credit can happen twice. If a transfer writes a debit and a credit separately, a failure between the two writes leaves the system looking balanced while one side is missing. None of these are exotic failures. They are the normal tax of storing a total and hoping the history stays nearby.

The usual answer is another table, a log file, or a reconciliation job that runs at night. At that point the system already has history. I would rather make the history the source of truth and treat the balance as a cached answer.

A write should say what happened

In an append-only ledger, a write is a fact about the past. It is not the new total. It is the change, the reason, and the operation that caused it.

A simplified entry can look like this:

{
  accountId: "cell-42",
  delta: -250,
  reason: "operations.tick",
  operationId: "op_01J8M2R6W9",
  createdAt: "2026-11-13T21:04:12.418Z"
}

The current balance is a query over those entries:

select coalesce(sum(delta), 0)
from wallet_entries
where account_id = :account_id;

That query is not free. Nobody wants to sum a year of entries every time a player opens a menu. A snapshot or a cached balance fixes the read path. The important part is that the snapshot is allowed to be rebuilt. It is not allowed to become the only copy of the truth.

Two stacks of pale paper receipts connected by a thin amber thread on a dark surface.
A transfer is not one number becoming another. It is two entries that both have to exist, or neither of them does.

The rules I care about

A ledger without rules is just a growing table. The rules are what make the data useful:

  • A transfer creates a debit and a credit in the same transaction.
  • Every entry has an operation ID or idempotency key. A retry can be recognised instead of booked again.
  • Entries are immutable. A correction is a new entry with a reason, not an edit to an old row.
  • The balance is derived from the entries. If a cached value disagrees, the entries win.
  • The write path validates the account, the amount and the reason before it appends anything.

The database constraints are the second line of defence. The application still has to decide what a valid operation means. A unique index on the operation ID stops duplicate work from being stored. It does not tell you whether the work itself was correct.

This is the same lesson as the scheduler post, in a different place. The code can look right while the ownership model is wrong. With a ledger, the ownership model belongs to the history.

The cost is real

I do not want to make append-only sound free. It is not.

Reads need a plan. Write volume grows with activity. A twelve-month season produces a lot of history, and the history cannot be quietly deleted when a table gets large. The practical answers are not exciting:

  • Keep a snapshot or a cached balance for reads, and rebuild it from the entries when necessary.
  • Index by account and time, not by the whole entry payload.
  • Archive closed periods, but keep the entries that support the current state.
  • Make reconciliation a normal job with a clear report, not an emergency script.
  • Test retries and partial failures, not only the happy path.

The cost is worth it when the number represents money, access, ownership, or anything a player will argue about. It is too much for a counter that only needs to show a rough total and has no audit requirement.

When not to use it

A ledger is a poor fit when updates are truly independent, mistakes are cheap, and nobody needs to answer why a value changed. A profile view counter is not a financial system. A cache hit counter is not a wallet.

The decision should follow the consequence of being wrong. If the worst case is a stale number on a dashboard, a normal update is simpler. If the worst case is a player losing something they earned, the history deserves to be a first-class part of the design.

What it changes in Deadlink Protocol

Deadlink Protocol is a browser MMO with a twelve-month season and a design target of 240 players per server. The economy affects what a Cell can build, what a Syndicate can coordinate, and what a player can lose.

An append-only wallet gives the system a stable audit trail. It makes a duplicate operation a visible failure instead of a silent balance change. It also makes the code more honest. A service method that wants to move value has to describe the operation instead of assigning a new integer and moving on.

The balance is still there for the game. It is just derived from the record. The record is slower to read and harder to delete. Those are not bugs. They are the reason the number can be trusted.

A balance is a convenience. The history is the system.

The boring win

An append-only ledger is not clever architecture. It is a refusal to hide the past. The interesting part is how much ordinary engineering follows from that refusal: idempotency, transactions, derived state, reconciliation and a clear boundary around the write path.

I like systems that are still explainable when something goes wrong. A balance query is easy to explain. A missing entry is easy to find. A total that cannot be reproduced from its history is a warning sign, not a number to trust.

That is why the wallet in Deadlink Protocol works this way. Not because it is fashionable, but because the history is the part I would want if I had to answer for the number.