System Design Drill Day 18: Wallets, Payouts and the Double-Entry Ledger
How to design a wallet and payout system that never loses or creates money, using an append-only double-entry ledger.

Finance asks why a customer's wallet says NPR 340. The wallets table has a balance column, and it says 340. Nothing records how it got there, so nobody can prove it's right.
Accounting solved this a long time ago with double-entry bookkeeping, and almost every good answer to "design a payment system or wallet" is built on it. The wallet in this drill is for an online marketplace, with eSewa and Khalti top-ups, split checkout, refunds and weekly merchant payouts, all on a double-entry ledger.
Core concepts
Don't keep a single balance column that you edit over and over. Every money movement is recorded as an immutable transaction made of two or more signed entries that always add up to zero. Money moves between accounts; it never appears or disappears.
A few rules come with it. Amounts are stored as integer paisa (BIGINT), never floats. The ledger is append-only, so a mistake or refund is fixed by posting a new reversing transaction, never by UPDATE or DELETE. An account's true balance is the sum of its entries; a cached balance row is fine for speed, as long as it's updated in the same database transaction as the entries. And every ledger transaction carries a unique idempotency key so retries can't post twice.
The ledger also has to be reconciled daily against the payment gateway's settlement report (eSewa or Khalti). The gateway is a separate system, and the two will drift.
Accounts aren't only users. The design also needs system accounts such as gateway clearing, merchant payable, platform commission and payout clearing.
Pause and think: Why integer paisa and not a
DECIMALor a float in rupees?
Show answer
Floats can't represent many decimal amounts exactly, so tiny rounding errors creep in and sums stop matching. Storing the smallest unit as an integer makes every addition exact, and "entries sum to zero" becomes a check you can actually rely on. DECIMAL is exact in MySQL, but PHP reads it back as a string and turns it into a float the moment you do maths on it. Integers in the smallest unit stay exact everywhere, in the DB, in PHP and in JSON.
The design problem
The marketplace is adding a customer wallet. Customers top up through eSewa or Khalti, refunds go back into the wallet, and checkout can be split between wallet and card. Merchants are paid weekly, minus a 5% platform commission. The stack is Laravel, MySQL and Redis queues, at about 500k money transactions a month.
Money must never be lost or created, and a wallet can never go negative. Everything has to stay safe under client, webhook and queue retries and under concurrent checkouts, and finance must be able to reconcile against the gateway every day.
Sketch the tables, the entries for top-up, checkout, refund and payout, and how overdrafts are prevented.
Try the problem before reading the solution.
Questions to think about
- Why not just run
UPDATE wallets SET balance = balance - 80000? What do you lose? - A customer has NPR 1,000 and taps "pay with wallet" for two NPR 800 orders on two devices at the same moment. Walk through exactly what stops the overdraft.
- Khalti says a top-up succeeded, but your webhook never arrived. How do you find out, and how do you fix it?
Model solution
Tables
The ledger needs four tables:
accounts(id,owner_type,owner_id,type,currency)ledger_transactions(id,idempotency_keyUNIQUE,type,referencesuch asorder_id,created_at)ledger_entries(id,transaction_id,account_id, signedamountin paisa)account_balances(account_id,balance,version)
A single LedgerService::post() writes the transaction, entries and balances inside one DB::transaction and rejects anything whose entries don't sum to zero.
Figure 1: Four tables. The entries are the truth, the balance row is a cache updated in the same transaction.
Flows (signed entries that net to zero)
A top-up of NPR 1,000 posts Khalti clearing −1,000 and customer wallet +1,000.
A checkout of NPR 800, paid 500 from the wallet and 300 by card, posts customer wallet −500, gateway clearing −300, merchant payable +760 and platform commission +40.
A refund is a new reversing transaction with the opposite signs, linked to the original; old rows are never edited.
The weekly payout posts merchant payable −X and payout clearing +X, and payout clearing is settled when the bank confirms the transfer. Its idempotency key is payout:{merchant_id}:{week}, and a short hold period lets late refunds land first.
Figure 2: The split checkout from above. Minus 800 out, plus 800 in, net zero.
Q1: why not a plain UPDATE?
A plain update loses the audit trail, so nobody can say why the balance is 340. You can't reconcile or replay, a bug can corrupt money silently with no way back, and nothing proves that money is conserved. A ledger gives you history, provability and nightly integrity checks: all entries sum to 0, and each cached balance equals the sum of that account's entries.
Q2: the double tap
Inside the transaction, run SELECT balance FROM account_balances WHERE account_id = ? FOR UPDATE, check that the balance is at least 800, then insert the entries and update the balance. The second request blocks on the row lock, reads 200 once the first commits, and is rejected.
Figure 3: The row lock makes the second request wait and then see the real balance.
A conditional UPDATE ... SET balance = balance - ? WHERE account_id = ? AND balance >= ? with an affected-rows check works as well. The database is the source of truth here, so no Redis lock is needed (compare Day 8). When one transaction touches several balances, lock accounts in id order to avoid deadlocks. The idempotency key checkout:{order_id}:wallet means a retried request returns the existing result instead of charging again.
Q3: the missing webhook
Before redirecting to Khalti, create a pending top-up row. The webhook verifies the signature, confirms with the gateway's verify API, and posts the ledger entry with the key topup:{gateway_txn_id}, so duplicate webhooks do nothing.
A scheduled job polls the gateway for pending top-ups older than about 10 minutes and posts or expires them. A daily reconciliation job compares Khalti's settlement file with the entries on the Khalti clearing account, and mismatches go to a queue for finance, to be fixed with an adjustment entry rather than an edit.
Scale
500k a month averages about 0.2 transactions a second, which MySQL handles easily. The real risk is hot accounts. The platform commission account is touched by every order, so its balance row becomes a lock bottleneck. There are a few ways out:
- skip the cached balance on system accounts and compute it from entries,
- split it into N sub-accounts, or
- post commission asynchronously.
Interview one-liner
"Append-only double-entry ledger in integer minor units. Every transaction sums to zero and has a unique idempotency key. Balances are updated under a row lock in the same database transaction. Corrections are reversing entries, and I reconcile daily against the payment provider."
Conclusion
If money can change without leaving an entry behind, you can't prove any balance is right, so make the ledger append-only from day one. As volume grows, keep an eye on the system accounts. The commission account is the row most likely to start queuing checkouts.
Try it yourself
Build the four tables above in a scratch Laravel project and write LedgerService::post() so it rejects any transaction whose entries don't sum to zero. Post a top-up, a split checkout and a refund.
Write a quick integrity check that sums all entries (it should be 0) and compares each cached balance with the sum of its entries. Finally, fire two concurrent wallet payments at the same account and confirm only one succeeds.
This is part of my daily System Design Drills series.
Comments
Join the discussionClear breakdown — the append-only ledger and idempotency keys are exactly what keep payouts trustworthy.



