CodingNodes Space play · ←→ step · R restart

The problem

A wallet is the simplest and the hardest of the money systems, because it is the one where the money is ours. A card charge moves money we hold on behalf of a merchant; a wallet holds money that belongs to the user, and the system's entire job is to move that money between users and to the outside world, without ever creating it, destroying it, or moving it twice. The balance a user sees is the most load-bearing number in the product, and it has to be exactly right, at every moment, even under a burst of concurrent spends.

The design that gets that right is a ledger, not a counter. A wallet is an account, and its balance is not a stored number that is incremented and decremented — it is the sum of a double-entry ledger, derived. A transfer is two entries written together — the sender's debit and the receiver's credit — guarded so the sender's balance cannot go negative. That guard, and the race it closes, is the whole of the correctness story: two concurrent spends that both pass a naive balance check is an overdraft, and the fix is to make the check and the debit one atomic step. An idempotency reference makes a retried transfer not a double debit, and the ramp is where the wallet meets the payment system and the bank. We will build the account and its derived balance, the atomic transfer, the overdraft race and the guard that closes it, the idempotency, and the ramp — and close the chapter with one transfer that is debited from one wallet and credited to another, with the balance never negative, no double debit, and the system's total money unchanged.

Requirements

Functional — what it must do

  • Hold: a wallet holds a balance belonging to its owner, and that balance is always the sum of the ledger that produced it — nothing more, nothing less.
  • Transfer: money moves from one wallet to another atomically, the sender is debited and the receiver is credited as one step, and the sender's balance never goes negative.
  • Move in and out: a wallet can be funded from the outside world and paid out to it, through a ramp that talks to the payment system and the bank, and the ramp is reconciled against the bank's record.

Non-functional — how well it must do it

  • No overdraft: the sender's balance is never negative, no matter how many spends land at the same instant — the guard that enforces this is part of the atomic step, not a check before it.
  • No double debit: a transfer that is retried, timed out, or resent with the same reference returns the first outcome and does not debit the sender twice.
  • Conservation: the total money in the system is created only by a funded ramp and destroyed only by a paid-out ramp — a transfer between two wallets changes nothing about the total, so the sum of all balances moves only when money crosses the ramp.
  • Traceability: every movement of money is a ledger entry, so the balance of any wallet at any time is recomputable from the record, and the audit is a query, not a reconstruction.

Scale — how much it must take

  • On the order of millions of wallets, with a peak-hour burst of concurrent transfers and spends, a balance that is read far more often than it is written, and a ramp that reconciles against the bank on every cycle.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Wallets
millions
The accounts. A balance is read far more often than it is written, so the read path has to be cheap.
Balance
derived, not stored
The balance is the sum of the ledger, not a counter — so it cannot drift out of step with the transfers that produced it.
Transfer
atomic, two entries
The sender's debit and the receiver's credit are one step, guarded so the sender's balance never goes negative.
Concurrency
a peak-hour burst
Many spends land at the same instant; the overdraft guard has to hold under that, not just in the calm case.
Idempotency
one reference → one transfer
A retried transfer with the same reference returns the first outcome, never a second debit.
Conservation
the total is constant
A transfer between wallets changes the total by zero; money enters and leaves only at the ramp.

Takeaways

Part 1

An account, not a counter

A wallet holds money that belongs to the user, and the balance is the load-bearing number. The design that keeps it exactly right is a ledger — a double-entry record — and the balance is derived from it, not stored as a counter.

  1. 1
    The money is ours, and it cannot be invented

    The defining property of a wallet is that the money in it belongs to the user, and the system's job is to move it, not to create or destroy it. That is a different contract from a card charge, where the system holds funds on behalf of a merchant. In a wallet, the balance a user sees is the promise, and the promise is that it is exactly the money the user put in, minus exactly the money the user moved out, plus exactly the money other users sent. Every one of those is a ledger entry, and the balance is their sum. Get that sum wrong and you have either created money out of nothing or erased money that is owed — both are losses, and both are the failures the design exists to prevent.

  2. 2
    The balance is derived, not stored

    The naive wallet is a stored balance that goes up on a credit and down on a debit. It is simple, and it is wrong in the way that matters: every slip, every missed entry, every double debit is a permanent error in a number with no memory of why it is what it is. The design that survives is a ledger — an append-only record of every debit and every credit, each a double-entry — and the balance is the sum of that record, derived on read. A slip is still a slip, but it is a slip in a record you can read, and the balance is always recomputable from the entries that produced it. The ledger turns the balance from a number you trust into a number you can prove.

Part 2

The double-entry ledger

Every movement of money is two entries: a debit on one side and a credit on the other, written together as one record. The double-entry is what keeps the books balanced by construction, and what makes the balance a derived, provable quantity.

  1. 1
    Every transfer is two entries

    A transfer is not one ledger entry; it is two. The sender's wallet is debited — the amount leaves their balance — and the receiver's wallet is credited — the amount is added to theirs. Both entries are written together, as one logical record with two sides, and the double-entry is what keeps the books balanced: the total of all debits always equals the total of all credits, by construction. A balance is the net of one wallet's entries, and because every entry has a matching counterpart, a balance that is wrong is a record that is unbalanced — and an unbalanced record is visible, not hidden.

  2. 2
    The total is conserved

    Because every transfer is a debit on one wallet and a credit on another, a transfer between two wallets changes the total money in the system by exactly zero. The sum of all balances moves only when money crosses the ramp — in, through a funding, or out, through a payout. That is the conservation law of the wallet system, and it is the invariant the reconciliation checks: if the total has moved and no ramp entry accounts for it, something is wrong. The conservation is not a goal the design strives for; it is a property the double-entry gives it for free, and it is what separates a wallet that is correct from one that is merely self-consistent.

  3. 3
    The account is the identity

    A wallet is not just a balance; it is an account, and an account is a balance plus an identity. The identity is what the money belongs to — a user, verified — and it is the key that the ledger is organized by. Every entry references the account it moves money in or out of, and the balance of an account is the net of its entries. Separating the identity from the balance is what lets the same design serve millions of wallets: each is an account, each has a ledger, and each balance is the net of its own entries. The account is the unit of the system, and the ledger is the unit of the record.

Part 3

The atomic transfer

A transfer moves money from one wallet to another as one step: the sender is debited and the receiver is credited together, guarded so the sender's balance cannot go negative. The atomicity is the point — there is no moment in which the money is in neither wallet or in both.

  1. 1
    One step, two sides

    A transfer is a single operation with two effects: the sender's wallet is debited and the receiver's wallet is credited. These two effects are not two steps; they are one step, written together as a double-entry record. That is the atomicity, and it is what makes the transfer safe. If the debit and the credit were two separate operations, a crash between them would leave the money in a limbo — debited from the sender but not yet credited to the receiver — and the total would be wrong. Writing them together means there is no such moment: the money is either in the sender's wallet or in the receiver's, never in between and never in both.

  2. 2
    The guard: the sender cannot go negative

    The atomic step is not just 'write the two entries'; it is 'write the two entries, but only if the sender's resulting balance is not negative.' That guard is the overdraft prevention, and it is part of the atomic step, not a check that runs before it. The service computes the sender's balance after the debit and refuses the transfer if it would go negative, all in the same step that writes the entries. A transfer that is refused leaves no trace: no partial debit, no orphaned credit, no half-move. The guard is what makes 'you cannot spend money you do not have' a property of the write, not a promise the system hopes to keep.

  3. 3
    A refused transfer is clean

    Because the guard is in the atomic step, a refused transfer is a clean no-op: nothing is written, the sender's balance is unchanged, and the receiver's is unchanged. There is no cleanup to do, no partial state to roll back, no 'the debit went through but the credit did not'. The refusal is the system telling the sender 'you do not have enough for this', and the sender can see it and act on it. That cleanliness is a direct consequence of making the debit, the credit, and the guard one step — split them, and a refusal becomes a rollback, and a rollback is a second failure mode to design for.

Part 4

The overdraft race

Two spends land at the same instant, and both pass a balance check that ran before them. Check-then-debit is the classic lost-update, and it is the overdraft. The fix is to fold the check into the debit, so the balance is tested at the moment of the write, not at an earlier, stale moment.

  1. 1
    Two spends, one balance

    The sender's balance is 100, and two spends of 60 each arrive at the same instant. If each spend checks the balance and then debits, both see 100, both pass the 'is it ≥ 60?' check, and both debit 60 — and the balance is now -20. The money is gone, the sender is in the red, and the system has created a liability it did not have before. Nothing in either spend was wrong on its own; the failure is the gap between the check and the debit, and it is that gap that has to be closed. This is the overdraft race, and it is the one correctness failure of the wallet that a naive design actually produces.

  2. 2
    Fold the check into the debit

    The fix is to make the check and the debit one atomic step: a compare-and-set that says 'debit the sender by the amount and credit the receiver by the amount, but only if the sender's resulting balance is not negative', executed by the storage layer in a single operation. Now the two concurrent spends race to that single step, and exactly one of them can succeed: the first one's debit drops the balance below 60, and the second one's compare-and-set sees that the sender's resulting balance would be negative and refuses. Only one spend goes through, the other is declined, and the balance never goes negative. The race is closed by construction, not by hoping the two spends do not interleave.

  3. 3
    The loser is declined, not lost

    A compare-and-set that refuses is not an error to swallow — it is the answer 'the sender does not have enough for this one.' The spend that lost the race is declined to its caller, with a reason, and the caller can act on it — retry later, ask for a smaller amount, or tell the user the funds are not there. The spend that won the race committed, and the balance is exactly the sender's original balance minus that one amount. Two spends, one success, one decline, and the balance never negative: the overdraft is closed, and both outcomes are clean and explainable.

Part 5

The reference: one transfer per intent

A transfer that times out is retried, and a retry with the same reference must not be a second debit. The reference is the anchor: it is what separates 'the same transfer, tried again' from 'a new transfer', and it is what makes a retried transfer a no-op.

  1. 1
    The retry is indistinguishable from a new transfer

    From the system's point of view, a retried transfer and a brand-new transfer look identical: same sender, same receiver, same amount. The only thing that separates them is the reference — a value the caller generates once, at the moment they decide to transfer, and sends with every attempt of that logical transfer. A retry carries the same reference; a genuinely new transfer carries a new one. Without that reference, the system cannot tell the two apart, and the safe behavior — treating every request as a new transfer — is exactly the one that double-debits the sender.

  2. 2
    The reference maps to the one real transfer

    The system keeps a map from reference to the transfer it created. The first request with a reference creates the transfer — the double-entry debit and credit — and records the reference → transfer mapping. A later request with the same reference does not create a transfer; it looks up the mapping and returns the outcome of the transfer that already exists. So a retry — whether it arrives before the first has finished or long after — returns the first result, and the sender is debited once. The reference is the anchor of exactly-once transfer: it is what turns at-least-once delivery of a transfer request into exactly-once movement of money.

  3. 3
    The reference is a contract, and it is the caller's

    The reference is generated by the caller, not the wallet system, and that is deliberate. The caller is the one who knows what a logical transfer is — who knows that these two requests are the same payment and those three are three different ones. The wallet system cannot know that; it can only honor the reference it is given. So the contract is: the caller sends a stable reference per logical transfer, and the system guarantees one transfer per reference. The system's job is to make the guarantee hold; the caller's job is to make the reference stable. Get the reference wrong and the guarantee is only as good as the reference.

Part 6

The ramp: where the wallet meets the world

A wallet's money comes from the outside world and goes back to it. The ramp is that boundary: a funding moves fiat into a balance, a payout moves a balance out to a bank, and the ramp is where the wallet meets the payment system and the bank — and where the reconciliation runs.

  1. 1
    In and out, through the ramp

    The total money in the wallet system is constant across transfers — a transfer between two wallets changes nothing about the total. The only way the total changes is at the ramp: a funding moves money in from the outside world, and a payout moves money out to it. A funding is the user paying with a card or a bank account to add to their balance; it is a debit at the source and a credit to the wallet. A payout is the reverse — a debit from the wallet and a credit to the bank. The ramp is the one place where the wallet's closed system opens to the world, and it is the one place where the total is allowed to move.

  2. 2
    The ramp is the payment system's job

    The ramp is not a new mechanism; it is the payment system from the previous chapter, facing the wallet. A funding is a charge of the user's card, and a payout is a settlement to the user's bank — both are operations the payment system already performs, and the wallet consumes them as entries in its ledger. That reuse is deliberate: the wallet does not reimplement card processing or bank settlement, it treats them as the two operations that move money across its boundary. The wallet's ledger records the wallet side of each ramp entry, and the payment system records the outside side, and the two are reconciled against each other.

  3. 3
    Reconciliation closes the ramp

    The wallet has a record of the ramp entries it processed, and the bank has a record of the movements it made, and these are two records kept by two parties that can disagree. A reconciliation runs per cycle and compares the two — the wallet's ramp entries against the bank's statement — and finds the gap. A funding the wallet recorded but the bank did not receive, a payout the wallet sent but the bank did not credit: each is a gap, and each is a real problem named. The reconciliation is what makes the ramp trustworthy, and it is the same mechanism as the payment system's reconciliation, applied to the wallet's boundary. The ramp is where the money crosses the world, and the reconciliation is the proof that it crossed exactly once.

Part 7

The invariants, held together

The wallet's correctness is a small set of invariants, each enforced by a different piece: the ledger for traceability, the double-entry for conservation, the atomic guard against overdraft, the reference against double-debit, and the ramp reconciliation against the outside world. Held together, they are the design.

  1. 1
    The invariants, and the piece that holds each

    The wallet's correctness is not one guarantee; it is a small set of invariants, each with the piece that enforces it. The balance is traceable, and the ledger is the piece. The total is conserved, and the double-entry is the piece. The balance is never negative, and the atomic guard is the piece. A transfer is never double-debited, and the reference is the piece. And the ramp is trustworthy, and the reconciliation is the piece. None of these is sufficient alone, and together they are the design. That is the whole of the wallet: a ledger, a double-entry, a guard, a reference, and a reconciliation, each holding one invariant, and the balance — the number the user sees — falling out of all five.

  2. 2
    The balance is the product of all five

    The balance a user sees is not any one of these pieces; it is the product of all five working together. The ledger gives it traceability, the double-entry gives it conservation, the guard keeps it non-negative, the reference keeps it from being double-debited, and the reconciliation keeps it honest against the world. Remove any one and the balance is exposed to the failure that piece was there to prevent. That is the difference between a wallet that is correct and one that is merely consistent: consistency is holding the invariants in the calm case, and correctness is holding them under the burst, the retry, and the bank's disagreement at the same time. The wallet is the five pieces, and the balance is what they produce.

  3. 3
    The wallet, in one frame

    Put the pieces together and the design is clear. A wallet is an account, and its balance is the net of its ledger. A transfer is a double-entry written atomically, guarded against overdraft and made idempotent by a reference. The total is conserved across transfers and moves only at the ramp, where the wallet meets the payment system and the bank, and the reconciliation closes the loop. One wallet, one balance, always the sum of its entries, never negative, never double-debited, and honest against the world — the five invariants, held together, are the wallet.

★ Finale

End to end

One wallet's life: funded through the ramp, then a transfer that debits one wallet and credits another atomically, with a concurrent spend that loses the race and is declined, and a retried transfer that returns the first outcome — the balance never negative, no double debit, and the total of the system unchanged except at the ramp.

  1. 1
    The whole system

    Every piece in one frame. A wallet is an account whose balance is the net of its ledger. A transfer is a double-entry written atomically, guarded so the sender's balance never goes negative, and made idempotent by a reference. The total is conserved across transfers and moves only at the ramp, where the wallet meets the payment system and the bank. One wallet, one balance, always the sum of its entries, never negative, never double-debited, and honest against the world.

  2. 2
    Funded, and the total moves at the ramp

    A user funds their wallet: a card is charged through the payment system, and the wallet's balance goes up. This is the one moment the total money in the system changes, because the money has crossed the ramp from the outside world. The wallet records the credit, the payment system records the charge, and the two are reconciled. The wallet now holds real money, and that money is the user's — the system holds it, and its job is to move it without ever inventing or erasing it.

  3. 3
    A transfer, against a rival spend

    The sender transfers an amount to the receiver. At the same instant, a rival spend of the same size arrives. Both reach the atomic guard, and exactly one can go through: the winner's debit and credit commit, and the sender's balance drops by that one amount. The loser's compare-and-set sees that the sender's resulting balance would be negative and refuses — it is declined, with a reason, and the sender's balance is never negative. One transfer commits, one spend is declined, and the balance is exactly what it should be.

  4. 4
    Retried, and the reference makes it a no-op

    The transfer times out in flight, and the sender's client retries it with the same reference. The system looks up the reference, finds the transfer it already created, and returns that transfer's outcome — no new debit, no second credit. The reference did its job: the retry is a no-op, and the sender is debited exactly once. The balance is the sum of the one transfer, the total is conserved, and the wallet is honest — never negative, never double-debited, and the money moved exactly once.

🌐 In the wild

In the wild

Digital wallets are the backbone of every P2P and mobile-money platform. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.

  1. 1
    The pattern, piece by piece

    Each box is a standard building block of a wallet. Anchoring them to the classes of production systems that run that piece turns the abstract design into the shape behind the balance a user trusts with their money, and the transfer that moves it exactly once.

  2. 2
    Why the same pieces keep winning

    Independent wallets converge on the same pieces because each one removes a real failure: the ledger removes the balance-drift failure, the double-entry removes the unbalanced-books failure, the atomic guard removes the overdraft failure, the reference removes the double-debit failure, and the ramp reconciliation removes the unexplained-movement failure. That convergence across independent systems is the evidence the design is load-bearing.