Ananya Nair, a teacher in Kochi, opens the app of a small wallet company called PaisaPouch (invented for this article). She adds ₹1,000 from her bank. Her screen shows ₹1,000. Next day she pays ₹250 at a tiffin centre. Screen shows ₹750. The day after, the tiffin centre refunds the ₹250 and the screen shows ₹1,000 again. Where, in the company’s database, did all that happen?

In the first design a junior developer would reach for, there is a wallets table with a balance column. Three operations, three updates to one number. It works until the first complaint. Ananya says: “I was charged ₹250 twice on Tuesday.” Support opens the database and sees balance = 500. The number is current. It carries no history. Nobody can say how it got there.

Record movements, derive the balance

The fix that accountants found some centuries ago: never overwrite. Only append. Every change of money is a row in a ledger, and the balance is whatever those rows add up to. A bank passbook works exactly this way. The running total printed on the right is a convenience; the lines are the truth.

Now the second idea, which is what makes it a double-entry ledger. Money does not appear or vanish. When ₹250 leaves Ananya’s wallet, it arrives somewhere. So each business event writes at least two entries, and the entries must sum to zero. Using signed amounts, with a plus meaning the company now owes that party more:

EventAccountAmount (₹)
Top-upAnanya wallet+1,000
Cash held at partner bank-1,000
PaymentAnanya wallet-250
Tiffin centre payable+250
RefundTiffin centre payable-250
Ananya wallet+250

The sign convention here is deliberately simple. Real accounting uses debits and credits, where the same idea appears as total debits equalling total credits. If your finance team prefers that vocabulary, use theirs; the engineering requirement is identical. Ananya’s balance is the sum of her rows: 1,000 minus 250 plus 250, which is 1,000.

The pay-off is a check you can run nightly: add up every entry in the system. It must be exactly zero. If it is not, a bug has created or destroyed money, and you know before the auditors do.

Rules that make it trustworthy

  • Append-only. No updates, no deletes on ledger rows. A mistake is fixed by a new reversing entry that points to the one it corrects. The history of the mistake stays visible.
  • One transaction, all legs. The entries of one event commit together or not at all. A database transaction does this. Half an event is the worst bug in payments.
  • Integer minor units. Store paise as whole numbers. Floating point cannot represent 0.10 exactly, and small errors in sums are how reconciliation breaks at scale.
  • An idempotency key per event. Ananya’s “charged twice” is very often a retried request. A unique constraint on a business reference means the second attempt is rejected rather than posted again.

Checking the balance before spending

Summing every row for every payment is hopeless once a power user has 40,000 entries. Two answers, usually combined.

First, keep a cached balance on the wallet row, updated in the same database transaction as the ledger insert, so both change or neither does. This looks like the naive design returning, but there is a difference: it is now a derived value that can be rebuilt from the ledger and verified against it. Second, make the debit safe against two simultaneous payments by locking that wallet row, or by a conditional update that only succeeds if the balance is still at least the amount. Otherwise two ₹800 payments racing against a ₹1,000 balance may both pass the check.

Periodically store a snapshot (balance as of midnight) so reconciliation scans only recent rows.

Pending money and fees

Real payments are not instant. A card or UPI payment may be initiated, pending for seconds or hours, then succeed or fail. Model this with extra accounts rather than status flags on a balance: money moves into a “held” account when initiated, and from there to its destination on success or back on failure. Every state is still a pair of entries. The same applies to fees. If PaisaPouch charges the tiffin centre 1 percent, that is a third leg of the payment event, crediting a revenue account, so the entries still sum to zero.

Back to Tuesday

With the ledger, support searches Ananya’s entries and finds two payments of ₹250 at 10:14:03 and 10:14:05 with different references. That is evidence the client app sent two separate requests, a double tap perhaps, rather than a server retry. Or it finds one payment and her memory was wrong. Either way the conversation ends with facts, and if a refund is due it is posted as a new, linked entry.

A caution: this is the engineering shape, not legal advice. In India, a prepaid payment instrument is regulated by the RBI, and reconciliation, escrow and reporting duties apply on top. The ledger is what makes meeting them possible.

Figures and names in the examples are illustrative.

Leave a Reply

Trending