SimuVedaSystem Design Lab Open in the lab

Payments / Intermediate / 6 min

Design a digital wallet: ledgers and races

Concurrent transfers with read-then-write create and destroy money. Locks or a double-entry ledger keep the books balanced.

Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.

In short

What it is
A wallet stores money per user and moves it between users; every transfer must take from one account exactly what it gives the other.
Why it matters
Concurrent transfers on the same account can overwrite each other, creating or destroying money unless writes are protected.
The trade-off
Locks are simple but serialise busy accounts; an append-only double-entry ledger scales and audits well but balances must be summed or cached.
Say it in an interview
"Double-entry ledger rows written in one transaction, a funds check with SELECT … FOR UPDATE on the balance row, and an idempotency key per transfer."

Check yourself

Two transfers read the same wallet balance of ₹500 at the same time, each subtracts ₹100 and writes. What is left?

Answer: ₹400, so ₹100 was lost from the books. Both write 500 − 100 = 400; the second write overwrites the first. This is the lost-update race.

Why always lock the two wallets in the same order (for example, lower id first)?

Answer: So two transfers in opposite directions cannot each hold one lock and wait forever (deadlock). A→B locking A then B while B→A locks B then A can deadlock; a fixed order prevents the cycle.

What makes a double-entry ledger safe?

Answer: Every transfer appends a debit and an equal credit, never overwriting, so the entries always sum to zero. Append-only paired entries keep an audit trail, and balances are computed from them, so money cannot silently appear or vanish.

See it happen in the simulation →

walletledgerdouble entryrace conditionlost update

More in payments