<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saksham Srivastava</title>
    <description>The latest articles on DEV Community by Saksham Srivastava (@saksham653).</description>
    <link>https://dev.to/saksham653</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4262054%2F86e954d0-0ca3-4614-b728-87f51e2354e3.jpg</url>
      <title>DEV Community: Saksham Srivastava</title>
      <link>https://dev.to/saksham653</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saksham653"/>
    <language>en</language>
    <item>
      <title>Refuse, don't repair: building accounting books that can't go wrong</title>
      <dc:creator>Saksham Srivastava</dc:creator>
      <pubDate>Tue, 24 Nov 2026 07:32:47 +0000</pubDate>
      <link>https://dev.to/saksham653/refuse-dont-repair-building-accounting-books-that-cant-go-wrong-3119</link>
      <guid>https://dev.to/saksham653/refuse-dont-repair-building-accounting-books-that-cant-go-wrong-3119</guid>
      <description>&lt;p&gt;Most bugs in business software aren't crashes. They're &lt;strong&gt;wrong numbers&lt;/strong&gt; that got saved, sat quietly for weeks, and then someone spent a weekend finding and fixing them.&lt;/p&gt;

&lt;p&gt;I build the backend for a grain-trading business: orders, lorries, bills, payments and the books, across several firms. It runs on &lt;strong&gt;872 API endpoints&lt;/strong&gt; and &lt;strong&gt;8,951 automated tests&lt;/strong&gt;. The rule that shaped most of it fits in three words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Refuse, don't repair.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If data is wrong, it never gets in. Then nobody has to find it and fix it later.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. A voucher that doesn't balance is refused at the door
&lt;/h2&gt;

&lt;p&gt;Double-entry bookkeeping has one law: for every voucher, &lt;strong&gt;total debits = total credits&lt;/strong&gt;. The usual approach is to save the voucher and run a report later that flags the ones that don't balance.&lt;/p&gt;

&lt;p&gt;I flipped it around. The check runs &lt;strong&gt;when the voucher is posted&lt;/strong&gt;, 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.&lt;/p&gt;

&lt;p&gt;Here's a simplified version of the idea in PostgreSQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- runs once per voucher, at commit time, after all of its lines are in&lt;/span&gt;
&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;voucher_must_balance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="k"&gt;trigger&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; $$
&lt;span class="k"&gt;begin&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nb"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;voucher_lines&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;voucher_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;voucher_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    &lt;span class="n"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="s1"&gt;'voucher % does not balance'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;voucher_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; $$ &lt;span class="n"&gt;language&lt;/span&gt; &lt;span class="n"&gt;plpgsql&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;constraint&lt;/span&gt; &lt;span class="k"&gt;trigger&lt;/span&gt; &lt;span class="n"&gt;voucher_balanced&lt;/span&gt;
  &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="k"&gt;insert&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="k"&gt;update&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;voucher_lines&lt;/span&gt;
  &lt;span class="n"&gt;deferrable&lt;/span&gt; &lt;span class="n"&gt;initially&lt;/span&gt; &lt;span class="n"&gt;deferred&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;row&lt;/span&gt; &lt;span class="n"&gt;execute&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;voucher_must_balance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;deferrable initially deferred&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Payments find their bills in a fixed order
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Background workers do it instead, in a strict order, stopping at the first rule that matches:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;By link&lt;/strong&gt;: the payment already names its bill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;By exact amount&lt;/strong&gt;: one open bill for that party has exactly this amount.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;By batch&lt;/strong&gt;: a set of open bills adds up exactly to the payment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Oldest first&lt;/strong&gt;: whatever's left settles the oldest open bills.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A release doesn't take traffic until it proves itself
&lt;/h2&gt;

&lt;p&gt;The same idea, applied to deploys: a new server &lt;strong&gt;takes traffic only after its health check passes&lt;/strong&gt;. If it doesn't pass, the old server keeps serving and nobody notices. Downtime for the people using the apps: zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Tests guard the rules, not just the code
&lt;/h2&gt;

&lt;p&gt;8,951 tests sounds like a lot. Most of them aren't clever. They pin down the rules in plain cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an unbalanced voucher is refused, in every firm;&lt;/li&gt;
&lt;li&gt;a payment with a link never falls through to "oldest first";&lt;/li&gt;
&lt;li&gt;an edit that would unbalance a posted voucher is refused.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a change breaks one of these, it doesn't ship.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this buys you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nobody runs a "find the bad data" report&lt;/strong&gt; — there's nothing to find.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fewer support calls&lt;/strong&gt;, because refusals come with a reason the user can act on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can trust the numbers.&lt;/strong&gt; That matters more than any feature on the dashboard.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The best bug fix is the bug that can't be written down in the first place.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;I'm Saksham Srivastava, a full-stack &amp;amp; AI engineer in Lucknow and co-founder of Sqaron Technologies. More of my work: &lt;a href="https://saksham.sqaron.dev" rel="noopener noreferrer"&gt;saksham.sqaron.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>postgres</category>
      <category>backend</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
