Blog / Fintech and payments

Why wallet balances should come from a ledger

The simplest way to build a wallet is a balance column on the user record. Deposit, add to it. Spend, subtract from it. It works on the first day, and the first month. Then one day a customer’s balance is wrong, and nobody can say why.

That is the problem with an edited number: it has no memory. A ledger does.

What a ledger is

A ledger records every movement of money as an entry: what moved, how much, when, why, and the event that caused it. The balance is not stored. It is worked out by adding up the entries.

In a double-entry ledger, every movement is written twice, once where the money left and once where it arrived, so all the entries together always add up to zero. A customer deposits ₦25,000: −₦25,000 against the gateway clearing account, +₦25,000 against the customer’s wallet. If the totals ever stop adding up, something is wrong, and the entries show where.

What goes wrong with an edited balance

Race conditions. Two withdrawals of ₦30,000 arrive at the same moment from a wallet holding ₦40,000. Both read ₦40,000, both pass the check, both subtract. The balance is now −₦20,000. With a ledger and the right database constraints, the second one is rejected.

Silent corrections. Support fixes a balance by editing the number. A month later, nobody remembers why it changed.

Reversals. A deposit is reversed by the bank. With a balance column, you subtract and hope. With a ledger, you write a reversing entry that points to the original.

Audits. A partner bank asks how a customer’s balance reached ₦1,240,000. With a ledger, you show them the entries. Without one, you reconstruct it from logs, if you have them.

Never edit an entry. If something was wrong, write another entry that corrects it.

What the ledger needs

  • Append only. Entries are never updated or deleted.
  • A reference on every entry, unique per event, so the same webhook cannot write twice. See idempotency keys explained.
  • Amounts as whole numbers in the smallest unit, kobo or cents, never floating point.
  • A currency on every entry, if you will ever hold more than one.
  • Separate accounts for holds and fees, so “available”, “pending” and “held in escrow” are different numbers.
  • One database transaction around both sides of every movement.

Is it slow to add up every entry?

Not for a long time. When it does get slow, keep a cached balance that is rebuilt from the entries and regularly checked against them. The cache is a convenience. The entries are the truth.

Moving an existing wallet to a ledger

If your product already runs on a balance column, you do not have to stop it to fix this:

  1. Create the ledger alongside the existing tables.
  2. Replay your transaction history into entries, as far back as your records allow.
  3. Write an opening entry for any difference between the replayed total and today’s balance, and investigate each one.
  4. Write new movements to both for a while, and compare every day.
  5. When they match, switch reads to the ledger and retire the column.

The differences found in step 3 are often the most useful part. They are the history of every bug the old wallet had.

Where this comes from. We do this work for clients. See escrow and wallet systems, or the case study on Gamestakers.

Questions about this post? Email contact@naxfront.com. Nancy reads every one.

WhatsApp