Writing · Backend · accounting · PostgreSQL
Refuse, don’t repair: building accounting books that can’t go wrong
By Saksham Srivastava4 min read

Most bugs in business software aren’t crashes. They’re wrong numbers that got saved, sat quietly for weeks, and then someone spent a weekend finding and fixing them.
I build the backend for a grain-trading business: orders, lorries, bills, payments and the books, across several firms. It runs on 872 API endpoints and 8,951 automated tests. The rule that shaped most of it fits in three words:
Refuse, don’t repair.
If data is wrong, it never gets in. Then nobody has to find it and fix it later.
1. A voucher that doesn’t balance is refused at the door
Double-entry bookkeeping has one law: for every voucher, total debits = total credits. The usual approach is to save the voucher and run a report later that flags the ones that don’t balance.
I flipped it around. The check runs when the voucher is posted, inside the same database transaction. If the totals differ, the post fails and nothing is written — for every firm, from every screen, whoever posts it.
Here’s a simplified version of the idea in PostgreSQL:
-- runs once per voucher, at commit time, after all of its lines are in
create function voucher_must_balance() returns trigger as $$
begin
if (select coalesce(sum(debit), 0) - coalesce(sum(credit), 0)
from voucher_lines where voucher_id = new.voucher_id) <> 0 then
raise exception 'voucher % does not balance', new.voucher_id;
end if;
return null;
end $$ language plpgsql;
create constraint trigger voucher_balanced
after insert or update on voucher_lines
deferrable initially deferred
for each row execute function voucher_must_balance();deferrable initially deferred matters. A voucher is written line by line, so it’s unbalanced halfway through. The check waits until the whole transaction commits, then judges the voucher as a whole.
The API checks the same rule first, so the user gets a clear message. The database check is the backstop for anything that slips past the API — a script, a bug, a future endpoint.
2. Payments find their bills in a fixed order
Money comes in, and someone has to decide which bill it pays. If people do that by hand, you get bills marked unpaid that were paid, and the reverse.
Background workers do it instead, in a strict order, stopping at the first rule that matches:
- By link: the payment already names its bill.
- By exact amount: one open bill for that party has exactly this amount.
- By batch: a set of open bills adds up exactly to the payment.
- Oldest first: whatever’s left settles the oldest open bills.
The order is the important part. Most payments match by rule 1 or 2, which can’t be wrong. Only what’s left falls through to the looser rules. Every match is recorded with the rule that made it, so any settlement can be explained.
3. A release doesn’t take traffic until it proves itself
The same idea, applied to deploys: a new server takes traffic only after its health check passes. If it doesn’t pass, the old server keeps serving and nobody notices. Downtime for the people using the apps: zero.
4. Tests guard the rules, not just the code
8,951 tests sounds like a lot. Most of them aren’t clever. They pin down the rules in plain cases:
- an unbalanced voucher is refused, in every firm;
- a payment with a link never falls through to “oldest first”;
- an edit that would unbalance a posted voucher is refused.
If a change breaks one of these, it doesn’t ship.
What this buys you
- Nobody runs a “find the bad data” report — there’s nothing to find.
- Fewer support calls, because refusals come with a reason the user can act on.
- You can trust the numbers. That matters more than any feature on the dashboard.
The best bug fix is the bug that can’t be written down in the first place.
I’m Saksham Srivastava, a full-stack & AI engineer in Lucknow and co-founder of Sqaron Technologies. Work with me or see what I’ve built.
- Python
- PostgreSQL
- Backend
- Architecture
- Also on DEV
Read nextReleases that prove themselves: zero-downtime deploys for a backend people use all day25 Nov 2026 · 5 min All writing