A double-entry ledger and a check that catches balance drift
How Habitrage stores money as ledger lines, why cents need care, and the reconcile check that compares stored balances to the ledger.
Habitrage is a personal finance platform I am building with a double-entry ledger, forecasting and a demo dataset. All the data in it is synthetic. It never connects to real bank accounts.
The drift problem
Many finance apps store a balance on an account and also keep the transactions behind it. Over time the two can disagree. A recurring payment is applied twice, a refund is missed, a rounding step loses a cent. Nobody notices until the numbers look wrong.
Double-entry in one paragraph
In double-entry bookkeeping every transaction is written as at least two lines, a debit and a credit, that add up to zero. Money is never created or lost in the data, only moved. The ledger lines are the truth. A balance is just the sum of the lines for an account.
I built the ledger on PostgreSQL using Prisma as the ORM.
The reconcile check
I wrote an automated check, which I call a data health check, that compares the stored balance with the balance computed by adding up the ledger journal lines. If they differ, the check fails. It runs in the automated tests, and it is verified active.
The idea works anywhere you keep both a total and the pieces: recompute the total from the pieces and compare.
Cents need care
Money should not be stored as floating point numbers. I used decimal values with fixed precision, Decimal(14,2). Instalments raise another small problem. Split 100.00 into three payments and you get 33.33, 33.33 and 33.33, which is 99.99. One cent has disappeared.
My rule is to put the remainder in the last instalment: 33.33, 33.33 and 33.34. The instalments add up to the original amount, and the reconcile check stays happy.
What this does not show
I tested against synthetic and demo records only. The project does not use live banking credentials or any open banking service, and I do not claim it is production ready. Its value is the design and the check.
The project page is on my portfolio.