How to build a double-entry ledger: 8 rules

Felix Koeslag
CTO & Co-Founder

Willem Pino
Engineering Team
We build payments infrastructure for platforms and marketplaces. Every payment we process ends up as lines in our own ledger, and every payout we make is calculated from it. These are the lessons we learned building it. Some come straight from double-entry bookkeeping. Others only show up once a ledger runs in software, at volume, with other systems writing to it.
If you're building a ledger of your own, start here.
The terms we use
Account: anything that can hold a balance, such as a merchant, a bank account or a payment provider.
Subledger: a log of balance changes for one account, with one fixed meaning, such as the bank balance of one bank account. Classic bookkeeping calls this an account. We say subledger to keep it apart from the accounts above.
Balance: the sum of all changes on a subledger.
Ledger: a group of subledgers that can only move balance among each other.
Journal: one event, booked as a set of balance changes that sum to zero.
Journal line: one balance change on one subledger.
One example runs through this post: a platform that takes a payment of €65 through Rootline and books it in its own ledger. It's a simple case, without a separate capture for example, but it shows the principles. The platform books in three ledgers. Receivable holds what other companies owe it, Rootline among them. Money holds the balance of its bank account. Fees holds the fees it pays to other companies, such as Rootline.
1. Write down what every balance means
Give every subledger a one-sentence definition of what its balance means. For example, the balance on our open bank statements subledger is money that arrived on our bank account and that we haven't attributed yet. The table below has such a sentence for every subledger in the example. If nobody can write that sentence for a subledger, two teams will book two different things on it.

2. Use double entry
Book every event as a journal with two or more lines, and make the lines sum to zero. Money that shows up in one place has to come from another. That also means you can trace every euro through the system, from the bank statement it arrived on to the payout it left in.
Say a credit appears on your bank statement. Book one line on your bank balance subledger, so it matches what the bank reports. Book the other on a subledger for bank statement lines you haven't explained yet, because at that moment you don't know whose money it is. Without that second line, nothing stops a bug from allocating more money than ever arrived.
Store each amount either as one signed number or as separate debit and credit columns. We use a signed number: debits negative, credits positive. Both work. Pick one and use it everywhere.
Here is the example payment, booked as three journals, one per column. Journal 1 books the payment as done: Rootline now owes the platform €65. Journal 2 is the bank statement case from above. Rootline pays out a batch of payments in one transfer of €2,500, so the platform books all of it on open bank statements. Journal 3 reconciles the payment: the platform matches €64.50 of the transfer to it, and books the €0.50 fee that Rootline kept. The other €2,435.50 stays on open bank statements until the platform reconciles the rest of the batch.

3. Make journals atomic and immutable
Book a journal completely or not at all. Once it's booked, never change it. If it was wrong, book a new journal that corrects it, or one that inverts it.
You get an audit trail with no extra work. Every balance at any moment can be rebuilt from the journals, and every correction shows up as its own event.
4. Record event time and insert time
Give every journal two timestamps. The event time is when it happened in the world. The insert time is when you booked it. Take journal 2 in the example: the transfer can arrive on Tuesday, while you only download the bank statement on Wednesday morning. Its event time is Tuesday and its insert time is Wednesday.
Use the event time to report what happened on a given day. Use the insert time to know what your system knew at any moment. An auditor, or an engineer chasing a bug, will ask exactly that.
5. Require a note on every manual journal
Sooner or later someone books a journal by hand: a refund sent through the bank portal, a fee charged twice. Make the reason a required field, with who booked it, why, and a link to the ticket. A year later an auditor or a colleague will ask about that line. Without a note, that question is impossible to answer. With a note, it's trivial.
6. Choose consistency over availability
Prefer a ledger that is down for an hour over a ledger that accepts a wrong journal.
Make the ledger fail fast. Check every journal before you book it: the lines sum to zero, each subledger is allowed for the account it books on, and the payment is in the state the journal expects. If anything is off, reject the journal.
Then put a queue between the systems that produce events and the ledger that books them, so a rejection never turns into an outage. Our payment services write their events to an outbox, and a consumer books them into accounting. If the ledger rejects an event, payments keep running and the event stays in the queue until the cause is fixed.
7. Give everything you want to track its own ledger
A ledger works for more than actual money movements. Use it to track state as well: a payment that is created, a capture that is scheduled. Give each state a subledger, and the balance on it is the amount in that state.
This removes the need for separate status tables. State tracked this way also gets the strict rules you built for your money: double entry, immutable journals and checks before every booking. Effectively you turn your accounting system into a state machine.
In the example, Receivable is such a ledger. Its balance is the amount of payments that are done but that Rootline hasn't paid out yet. Journal 1 opens that balance, and journal 3 closes it.
8. Keep real money in its own ledger
Put actual money in one ledger: what sits in your bank accounts and whose it is. Use the other ledgers for state. We have 35 ledgers, and one of them is money.
Then forbid moving balance between ledgers. That’s why we defined a ledger at the start as a group of subledgers that can only move balance among each other. Within each ledger, the lines of a journal sum to zero.A bug in your payment tracking can then never shift balance into the money ledger, and the money ledger keeps matching the bank.
Journal 3 in the example books in all three ledgers, and the lines of each ledger sum to zero on their own.
Where we went next
These rules give you a ledger you can trust. Ours goes a lot further: the payment state machine lives inside the ledger, journals are declared as state transitions, and a payment can change after it's created while the ledger works out what to rebook. That’s what lets platforms on Rootline update a split, change a fee or assign a courier after the customer has paid, without booking a single correction by hand. We'll show how in our next post.
