Domain tracks · L1Money and double-entry ledgers

Money and double-entry ledgers

Why money must never be a float, and how a ledger where every entry sums to zero keeps every rupee accounted for.

L1 Must-knowDomain tracks17 min read
Fact-checked by AI against cited sources · Human review pendingLast reviewed 21 Oct 2026 · How we check content
Senior at work · lesson 9 of 26
Section 1 of 8

What broke

A made-up story to show a common failure. Not about a real company.

This story is made up, but each part of it happens in real companies. A small wallet app gives users cashback. Each user has one row in a table with a balance column of type float. Every payment runs balance = balance + amount.

For months it looks fine. Then the finance team adds up all user balances and compares the total with the money in the bank. The numbers do not match. They are off by ₹38,412.07.

The team looks for the cause and finds three problems. Small float errors on millions of cashback payments do not add up to clean paise. A retry bug added some cashback twice. And one engineer fixed a support ticket by changing a balance by hand.

Nobody can say which part of the ₹38,412.07 came from which problem. The table only holds the current balance. It has no history. The money is gone from the records, and so is the story of how it moved.

Section 2 of 8

The idea in simple words

Two rules prevent almost all of this.

Rule one: never store money as a float. A float stores numbers as binary fractions. Most decimal amounts, like 0.1, cannot be written exactly in binary. So 0.1 + 0.1 + 0.1 == 0.3 is false in Python and most other languages. 1 The error is tiny, but money systems add millions of numbers, and the errors stay.

Instead, store money as an integer in minor units. A minor unit is the smallest unit of a currency: paise for rupees, cents for dollars. ₹10.50 becomes 1050. Stripe's API works this way: you send 1000 to charge 10 US dollars. 3 Another option is an exact decimal type, like NUMERIC in Postgres or Decimal in Python. Python's docs suggest its decimal module for accounting. 1

Always keep the currency next to the amount. 1050 means nothing without "INR". And currencies differ. The official ISO 4217 list says the Indian rupee has 2 decimal places, the Japanese yen has 0, and the Kuwaiti dinar has 3. 2

Rule two: record every movement of money as double-entry. Money never appears or disappears. It always moves from somewhere to somewhere. So every movement is written as at least two entries: one that takes money out of an account and one that puts it into another. 4

In this lesson we write money leaving an account as minus and money arriving as plus. Accountants call the two sides debit and credit. Which side means "more" depends on the type of account. 4 The rule that matters is the same in both styles: the entries of one transaction must sum to zero. 5

The key idea. Never change a balance directly. Write balanced entries that never change, and calculate every balance from those entries.

Section 3 of 8

How it works

Step through a ₹1,000 card payment to a shop, with a ₹20 platform fee. Use Next and Back, or the ← and → keys.

A ₹1,000 payment as ledger entries: every transaction sums to zero Customer Holding Merchant Fees 1 · Customer pays ₹1,000−1,000+1,000−1,000 + 1,000 = 0 ✓ 2 · Split: merchant and fee−1,000+980+20−1,000 + 980 + 20 = 0 ✓ 3 · A bug forgets the fee−1,000+980−1,000 + 980 = −20 ✗Rejected: not posted 4 · Balance = sum of entries−1,000098020✓ All balances add up to 0 SeniorPath
  1. The customer pays ₹1,000. The ledger writes two entries: −1,000 for the customer and +1,000 for a holding account. The code stores them as integers in paise, like −100000. They sum to zero.
  2. The platform splits the money. It takes ₹1,000 out of holding, gives ₹980 to the merchant and ₹20 to fee income. Three entries, one transaction, and the sum is still zero.
  3. A bug forgets the fee entry. The sum is −20, not zero, so ₹20 would vanish. The ledger refuses to post it. The bug is caught before any money goes missing.
  4. Nobody stores a balance by hand. Each balance is the sum of that account's entries. Holding is back to 0, so the money finished its journey. All balances together add up to 0.
Step 1 of 4

A ledger has three parts:

  • Accounts. One for each place money can sit: a customer, a merchant, your fee income, money held for a short time.
  • Entries. One line each: account, amount in minor units, currency, and a sign.
  • Transactions. A group of entries that happen together and sum to zero. Many teams also call this a journal entry.

Entries never change

The ledger is append-only. You add new entries. You never update or delete old ones. Square's ledger, Books, works this way: its entries are effectively append-only and immutable once stored. 5 Stripe describes its Ledger as "an immutable log of events". 6

So how do you fix a mistake? You post a reversal: a new transaction with the same entries and opposite signs. Then you post the correct one. TigerBeetle, a ledger database, follows the same rule: once transfers are recorded, they cannot be erased. 7 The wrong entry stays in the history, next to its fix. An auditor can see what happened, when, and why.

Martin Fowler notes that some systems let an entry change while it is still "open", then lock it once it is booked. 8 Payment systems often use the same idea as pending and posted entries.

Balances come from entries

A balance is the sum of all entries for one account. Square describes a balance as "essentially summing up all of the changes" to that account. 5 Summing millions of rows on every read is slow, so real systems also store a cached balance. They update it in the same database transaction as the new entries. The entries stay the truth. If the cache is ever wrong, you rebuild it from the entries.

Retries must not post twice

A ledger has the same retry problem as a payment API. If the "post transaction" call times out and the caller retries, you can post the same money twice. Give every ledger transaction a unique key from the event that caused it, such as the payment id. Make that key unique in the database, so a repeat is rejected. This is the idea from Idempotency, retries and timeouts, applied to money.

Reconciliation: check against the outside world

Your ledger can be perfectly balanced and still be wrong. It may miss a payment that the bank processed. Reconciliation means you regularly compare your ledger with outside records, such as bank statements and payment provider reports. Every difference is a missing, late or wrong entry that someone must explain.

Stripe uses a neat trick inside its ledger. Money that is "on the way" sits in a clearing account. When all steps finish, that account should go back to zero. A balance that is not zero points to a missing, late or wrong transaction. 6 Stripe also checks that every id in the source systems has a matching ledger event. 6

Rounding

Integers do not remove rounding. They make it visible. Split ₹100.00 between three people and you get 3,333 paise each, with 1 paisa left over. Decide where the leftover goes, for example to the first person, and write it as an entry. Pick one rounding rule and use it everywhere. A common choice is "round half to even": a tie goes to the nearest even digit. It is the default in Python's Decimal. 9 Write it down in the code, not in someone's head.

Section 4 of 8

Trade-offs

ApproachExact?CostUse it when
Float for amountsNo Small errors build up.Easy at first, painful later.Never for money. Fine for charts and estimates.
Integer minor units + currencyYes You must know each currency's decimal places and convert for display.Most payment and wallet systems. Simple and fast.
Decimal type (NUMERIC, Decimal)Yes , if you create it from strings, not floats.Slower, and every language handles it a bit differently.You need fractions of a minor unit, like interest or FX rates.
One balance column, updated in placeNo history, so you cannot prove it.Cheap to build.Toy projects only.
Double-entry ledger, balances derivedYes , and every change is explained.More rows, more design work, and busy accounts can slow writes.Any system that holds or moves real money.

Simple rule: integers in minor units with a currency code, in an append-only, double-entry ledger. Add a cached balance only when reads get slow.

One cost needs a warning. Some accounts, like the platform's own fee account, appear in almost every transaction. Many writes then wait on the same balance row. Modern Treasury calls this the "hot account" problem, and handles it by batching entries for that account. 10 This breaks the same-transaction rule on purpose. The cached balance can be a little behind: Modern Treasury targets 60 seconds. In practice, nine in ten entries are processed within 1 second. 10

Section 5 of 8

At work: how a senior uses this

  • In design reviews, search for float and double near any money field. Ask: "Where is the currency?"
  • Ask: "Where does this money come from, and where does it go?" If nobody can name both accounts, the design is not ready.
  • Add a check in code that refuses any transaction whose entries do not sum to zero. Treat a failure as a bug, never as a warning.
  • Ban UPDATE and DELETE on the entries table. Use database permissions, not only a team rule.
  • Give every ledger transaction a unique key from the event that caused it, so retries cannot post twice.
  • Run reconciliation every day. Give every difference an owner and a deadline.
  • Keep accounts that should return to zero, like money that has been sent but has not arrived yet. Alert when they do not return to zero. 6
  • Use the same pattern for things that are not money: reward points, credits, gift cards, even stock in a warehouse.
Section 6 of 8

With AI

What to ask the agent

"Search this codebase for money stored or calculated as float or double. List every place, with the file and line, and how you would change it to integer minor units."

"Write a property-based test: generate random ledger transactions and assert that every posted transaction sums to zero, and that every account balance equals the sum of its entries."

"Split ₹100.00 between 3, 7 and 9 people. Show the paise each person gets, and prove that nothing is lost or created."

What to check yourself

  • Amounts are integers or exact decimals in every layer: database, code, API and JSON. A float in the middle breaks the whole chain.
  • Decimal values are created from strings, not from floats.
  • The currency travels with every amount, and the code uses each currency's correct decimal places.
  • The agent did not add an UPDATE to "fix" a balance. Fixes are new entries.
  • The entries and the cached balance change in the same database transaction (except for hot accounts, as above).
  • Tests cover a retry of the same transaction and a split with a leftover paisa.
Section 7 of 8

Check yourself

1. A wallet app stores balances as a float. Why is this a problem?

Show answer

Many decimal amounts, like 0.10, cannot be stored exactly, so small errors build up. Floats store binary fractions. 0.1 is only close, so sums can drift by tiny amounts that never go away.

2. You store amounts as integers in minor units. How do you store ₹10.50 and ¥500?

Show answer

1050 and 500 The rupee has 2 decimal places, so ₹10.50 is 1050 paise. The yen has 0, so ¥500 is just 500.

3. A new ledger transaction has entries −1,000, +980 and +15. What should the ledger do?

Show answer

Reject it. The entries sum to −5, not zero, so this is a bug. Every transaction must sum to zero. If it does not, money was created or lost by mistake.

4. Yesterday a fee entry was posted with the wrong amount. How do you fix it?

Show answer

Post a reversal of the wrong entry, then post a new correct entry. Entries are never edited. A reversal keeps the full history, so anyone can see what happened and why.

5. All payments have settled, but your "holding" account still shows ₹20. What does this most likely mean?

Show answer

An entry is missing, late or wrong. An account that should go back to zero tells you that some money did not finish its journey.

Your progress is saved only in this browser. Export it to keep it safe.

Was this clear?
Report a mistake (opens GitHub in a new tab)
Section 8 of 8

Go deeper

Show 10 sources and related lessons
  1. Python documentation, “Floating-point arithmetic: issues and limitations” (opens in a new tab)
    Why 0.1 cannot be stored exactly in binary, with small examples you can run yourself.
  2. SIX Group, ISO 4217 currency codes (the official maintenance agency and its code lists) (opens in a new tab)
    The official currency list. Its "minor unit" column says how many decimal places each currency has.
  3. Stripe documentation, “Supported currencies”: specify amounts in minor units (opens in a new tab)
    A real payment API that takes every amount as an integer in the currency's smallest unit.
  4. Lucas Rocha, “Accounting for Developers, Part I: The Fundamentals”, Modern Treasury (opens in a new tab)
    Debits, credits and double-entry, explained for engineers with small examples.
  5. Square Engineering, “Books, an immutable double-entry accounting database service” (2019) (opens in a new tab)
    How Square built an append-only ledger where every journal entry balances to zero.
  6. Ilya Ganelin, “Ledger: Stripe's system for tracking and validating money movement”, Stripe (2024) (opens in a new tab)
    An immutable ledger at large scale, and how clearing accounts and data checks find missing money.
  7. TigerBeetle documentation, “Debit / Credit: the schema for OLTP” (opens in a new tab)
    A ledger database built around double-entry. Transfers cannot be erased, only reversed.
  8. Martin Fowler, “Accounting Entry” (analysis pattern, 2005) (opens in a new tab)
    A classic pattern write-up on entries, accounts and when entries become immutable.
  9. Python documentation, “decimal — Decimal fixed-point and floating-point arithmetic” (opens in a new tab)
    Exact decimal arithmetic and its rounding modes, including ROUND_HALF_EVEN, the default.
  10. Matt McNierney, “Behind the Scenes: How We Built Ledgers for High Throughput”, Modern Treasury (2024) (opens in a new tab)
    The "hot account" problem in a real ledger, and how batching entries reduces the contention.

Case studies: Square: Books: an immutable double-entry accounting service · Stripe: Ledger: checking every money movement with double entry · Uber: Moving a trillion ledger entries without downtime

Related: Idempotency, retries and timeouts · Reconciliation · The payment lifecycle

Next on this line: Reconciliation and The payment lifecycle.

If you have not read it yet: Idempotency, retries and timeouts. For a ledger stored as a stream of events, see Event-driven architecture and event sourcing.

Term

See full entry

Jump to a section

  1. 1What broke
  2. 2The idea in simple words
  3. 3How it works
  4. 4Trade-offs
  5. 5At work
  6. 6With AI
  7. 7Check yourself
  8. 8Go deeper