<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sourav Kabiraj on Medium]]></title>
        <description><![CDATA[Stories by Sourav Kabiraj on Medium]]></description>
        <link>https://medium.com/@souravkabiraj?source=rss-7b4f52ca5b5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LT_sKMGQ4uLVZ56AxGd0dQ.jpeg</url>
            <title>Stories by Sourav Kabiraj on Medium</title>
            <link>https://medium.com/@souravkabiraj?source=rss-7b4f52ca5b5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:25:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@souravkabiraj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Identify Production Errors that Your Test Suite Fails to Detect]]></title>
            <link>https://souravkabiraj.medium.com/how-to-catch-the-silent-production-errors-your-test-suite-misses-0323009cc455?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/0323009cc455</guid>
            <category><![CDATA[sentry]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[system-architecture]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Wed, 20 May 2026 08:31:00 GMT</pubDate>
            <atom:updated>2026-05-24T07:17:20.381Z</atom:updated>
            <content:encoded><![CDATA[<p>Beyond green builds: Why exceptions slip through CI/CD and how to use smart error fingerprinting to track them<em>.</em></p><p>You just shipped. 🚀</p><p>CI is green. Code review approved. Unit tests? Passing. Integration tests? Passing. Functional tests? All passing.</p><p>You close your laptop. Maybe grab a coffee. You earned it.</p><p>Three hours later, Slack explodes.</p><blockquote>“Hey, something’s broken on checkout.”<em> </em>“Users can’t log in.”<em> </em>“Payments are failing silently.”</blockquote><p>Sound familiar? It should — because we’ve all been there.</p><p>Mistakes are inevitable. That’s not pessimism, that’s engineering. The real question isn’t <em>if</em> something will break — it’s <strong>how fast you find out</strong>.</p><p>Because here’s the brutal truth: the longer a bug lives in production, the more damage it does. Users churn. Trust erodes. On-call engineers lose sleep. And that bug you could’ve caught in 5 minutes turns into a 3-hour war room.</p><p>The blast radius of a mistake is directly proportional to how long it goes undetected. 💥</p><p>So what if you could catch it at the <strong>exact moment the first user hits it?</strong></p><p>Not 3 hours later. Not after your SRE’s PagerDuty fires. Not after your CEO sees a tweet.</p><p><em>The. Very. First. User.</em></p><p>That’s not a fantasy — that’s what I want to talk about today.</p><h3>The Reliability Problem</h3><p>Every piece of code has two main kinds of flows.</p><p><strong>Positive flows</strong> are the happy paths — successful login, successful payment, order placed. These are the flows your product manager demos, your tests cover religiously, and your monitoring dashboards celebrate.</p><p><strong>Negative flows</strong> are the handled failures — wrong password, insufficient balance, network timeout. Good engineers write these explicitly. They know what failed, they return a proper error, the system recovers gracefully.</p><p>But there&#39;s a <strong>third kind</strong> — and it&#39;s the one that keeps you up at night.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*iAtefBz3gHCfMYQCYqhCqw.png" /></figure><p>These are the flows where <em>the code itself doesn&#39;t know what went wrong</em>. Your users see: <em>&quot;Something went wrong. Try again later.&quot;</em> Your logs show a stack trace no one has ever looked at. An exception flew through your application like a ghost — untracked, uncounted, unresolved.</p><p>This third category is what makes systems unreliable. And it&#39;s growing, because AI-generated code is excellent at the happy path and often skimpy on the edge cases.</p><p>So what do you do? You need a system that catches these ghosts <strong>before your users find them for you</strong>.</p><h3>Enter Sentry: Your Early Warning System</h3><p>In my recent work on system reliability, I wanted to understand how to surface these “ghost” errors before they hit a critical mass.</p><p>What surprised me wasn’t just how well error monitoring caught exceptions — it was how much was already silently failing without my knowledge. Bugs affecting hundreds of users never surfaced on our traditional infra dashboards.</p><p>Sentry became that signal. It stopped the practice of flying blind.</p><p>Sentry is an error monitoring platform that does three things that change how you operate:</p><ol><li><strong>Groups similar errors</strong> together into a single &quot;issue&quot; — so 10,000 users hitting the same bug don&#39;t create 10,000 noise tickets.</li><li><strong>Ties issues to deployments</strong> — so you know exactly <em>which release</em> introduced a problem.</li><li><strong>Tracks impact</strong> — how many users are affected, which tenants, how often.</li><li>It <strong>alerts</strong> based on your configured alert criteria.</li></ol><p>With that data, you can:</p><ul><li>Roll back a deployment within minutes, not hours</li><li>Prioritise: Any issue whether it is impacting just this one user, or is it 40% of your traffic?</li><li>Distinguish a new bug from a regression from a known issue</li></ul><p>But here&#39;s where it gets interesting. The hard part of error monitoring isn&#39;t <em>capturing</em> exceptions. Any logging library can do that. The hard part is <strong>grouping them intelligently</strong>.</p><p>Here is a demo Sentry dashboard…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f__oFTuZ-RvSylnW6s0dYQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sv3A5dJlOXyJLM4r7lNbqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEYgedzSWZTLfmAeIIXelg.png" /></figure><h3>The Fingerprinting Problem (And How Sentry Solves It)</h3><p>Imagine this exception fires in your payment service, a typical stack trace will look something like this:</p><pre>InsufficientFundsError: User 8842 has insufficient funds<br>  File &quot;django/db/backends/base/base.py&quot;, line 238, in ensure_connection<br>  File &quot;django/db/backends/utils.py&quot;, line 67,  in execute<br>  File &quot;payment/processor.py&quot;, line 34, in charge_card<br>  File &quot;payment/gateway.py&quot;, line 12, in process_payment<br>  File &quot;api/handlers/checkout.py&quot;, line 89, in post</pre><p>Five minutes later, a different user hits the same bug:</p><pre>InsufficientFundsError: User 3391 has insufficient funds<br>  File &quot;django/db/backends/base/base.py&quot;, line 238, in ensure_connection<br>  File &quot;django/db/backends/utils.py&quot;, line 67,  in execute<br>  File &quot;payment/processor.py&quot;, line 34, in charge_card<br>  File &quot;payment/gateway.py&quot;, line 12, in process_payment<br>  File &quot;api/handlers/checkout.py&quot;, line 89, in post</pre><p>Naive monitoring considers these two events as separate issues. Your dashboard shows 2 errors. No alarm. No urgency. By midnight, you have 50,000 separate issues — all the same bug, all silently categorised as &quot;different.&quot;</p><p>Sentry solves this through <strong>fingerprinting</strong>: a smart hashing pipeline that figures out which events are <em>really</em> the same problem, regardless of the noise in the stack trace.</p><h3>The Multi-Layer Cleanup Pipeline</h3><p>Before computing a fingerprint, Sentry runs your raw event through a cleanup pipeline. Each layer strips out a different source of noise.</p><h3>Layer 1: Strip Dynamic Values from Messages</h3><p>User 8842 and User 3391 are different users, not different bugs. Sentry strips variable data from exception messages, normalising them to:</p><pre>InsufficientFundsError: User *** has insufficient funds</pre><p>Now both events produce the same fingerprint input. Same bug → same issue. ✅</p><h3>Layer 2: Ignore Line Numbers</h3><p>You push a small refactor. You add five lines above charge_card. Now the stack trace says line 39 instead of line 34. Different number, same bug.</p><pre>InsufficientFundsError: User *** has insufficient funds<br>  File &quot;django/db/backends/base/base.py&quot;, line 238, in ensure_connection<br>  File &quot;django/db/backends/utils.py&quot;, line 67,  in execute<br>  File &quot;payment/processor.py&quot;, line 39, in charge_card<br>  File &quot;payment/gateway.py&quot;, line 12, in process_payment<br>  File &quot;api/handlers/checkout.py&quot;, line 89, in post</pre><p>To deal with it, Sentry strips line numbers entirely. The fingerprint is based on <em>which function</em>, not <em>where in the file</em> it is.</p><pre>InsufficientFundsError: User *** has insufficient funds<br>  File &quot;django/db/backends/base/base.py&quot;, in ensure_connection<br>  File &quot;django/db/backends/utils.py&quot;, in execute<br>  File &quot;payment/processor.py&quot;, in charge_card<br>  File &quot;payment/gateway.py&quot;, in process_payment<br>  File &quot;api/handlers/checkout.py&quot;, in post</pre><p>Deploying unrelated code changes no longer creates duplicate issues for the same bug. ✅</p><h3>Layer 3: Exclude System/Library Frames</h3><p>Here&#39;s a subtler one. You upgrade Django from 3.2 to 4.1. Internally, Django changes some of its call chains:</p><pre>OperationalError: could not connect to server<br>  File &quot;django/db/backends/base/base.py&quot;,  line 238, in ensure_connection  ← Django internals<br>⚠️File &quot;django/db/backends/utils.py&quot;,      line 99,  in executeOne         ← new in 4.1 (Django internals)<br>  File &quot;django/db/backends/utils.py&quot;,      line 67,  in execute            ← Django internals<br>  File &quot;myapp/repositories/user_repo.py&quot;,  line 45,  in find_by_email      ← your code ✅<br>  File &quot;myapp/services/auth_service.py&quot;,   line 23,  in authenticate       ← your code ✅<br>  File &quot;myapp/api/views/login.py&quot;,         line 11,  in post               ← your code ✅</pre><p>The library upgrade changed the system frames — but the bug is the same. Without filtering, Sentry creates a <em>new issue</em> after your dependency upgrade. Your old issue loses its history. You lose continuity.</p><p>That’s why Sentry marks frames as &quot;in-app&quot; or &quot;system/library&quot; and builds fingerprints using only your code (with in-app frames):</p><pre>OperationalError: could not connect to server<br>  File &quot;myapp/repositories/user_repo.py&quot;, in find_by_email<br>  File &quot;myapp/services/auth_service.py&quot;,  in authenticate<br>  File &quot;myapp/api/views/login.py&quot;,        in post</pre><p>Now, upgrade (or downgrade) your dependencies without splintering your issue history. ✅</p><h3>Layer 4: Collapse Recursive Frames</h3><p>Recursive bugs produce spectacular stack traces. A deep parser crash might look like:</p><pre>  File &quot;parser.py&quot;, line 45, in parse_node<br>  File &quot;parser.py&quot;, line 45, in parse_node<br>  File &quot;parser.py&quot;, line 45, in parse_node<br>  ... (300 more times)<br>  File &quot;parser.py&quot;, line 12, in parse</pre><p>If you include all 300 frames in the hash, two things go wrong:</p><ol><li>The fingerprint input is enormous</li><li>A recursion depth of 301 vs 300 creates a <em>different fingerprint</em> — same bug, different issue</li></ol><p>Sentry collapses the repetition:</p><pre>  File &quot;parser.py&quot;, in parse_node<br>  File &quot;parser.py&quot;, in parse</pre><p>Fingerprint is now stable regardless of how deep the recursion went. ✅</p><h3>The Full Pipeline</h3><p>Here&#39;s how it all fits together, from raw exception to grouped issue:</p><p>Remember when I said you will find out your mistake at <strong>exact moment the first user hits it. </strong>Yes! you can create an alert from Sentry when it creates a new issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bowwdf4vovVZNFjQejatuA.png" /></figure><h3>Why This Matters More Than Ever</h3><p>We’re in an era where AI writes significant chunks of production code and tests. The code looks pristine, passes CI, and handles the documented happy paths.</p><p>But beneath the surface, your risk profile just multiplied:</p><ul><li><strong>The 10x Velocity Paradox:</strong> Shipping a feature in two days instead of two weeks is incredible — but it means 10x the surface area for unprompted edge cases and silent failures.</li><li><strong>Continuous Deployment Chaos:</strong> Teams now deploy dozens of times a day. Every push is a risk window. Without instant feedback loops, good luck guessing which of today’s twelve deployments caused that 2 PM regression.</li></ul><h3>The Reality Gap</h3><p>AI models train on documentation, but production runs on chaos. AI misses the undocumented “third category” — the unmocked third-party API behavior, the corrupted database states, and the bizarre user flows.</p><blockquote><strong><em>Test coverage tells you your code does what you </em>think<em> it does. Monitoring tells you what your code </em>actually<em> does at runtime — with real users, real data, and real edge cases.</em></strong></blockquote><p>Velocity without visibility is just a faster way to break things. AI is your accelerator; monitoring is your safety net. You need both.</p><h3>The Takeaway</h3><p>Stop flying blind.</p><p>Set up your Error Monitoring Tool. Instrument your services. Tag your errors with user_id, instance_id, deployment_version and other tags.</p><p>The next outage is already happening. Make sure you find out from your telemetry — not your users.</p><p><em>If you found this useful, I write about distributed systems, backend engineering, and the realities of operating software at scale. Follow along.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0323009cc455" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Cache Lie: when Cached Data Doesn’t Match the Database]]></title>
            <link>https://souravkabiraj.medium.com/the-cache-lie-af88746b35af?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/af88746b35af</guid>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Tue, 31 Mar 2026 15:07:07 GMT</pubDate>
            <atom:updated>2026-04-01T04:35:03.282Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Your database and your cache are supposed to agree. Under enough traffic, they won’t. Here’s why — and how to fix it.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qmOcK4vUgGVqG5-K6b8iyg.png" /></figure><p>There’s a pattern so deeply baked into backend engineering that it’s basically muscle memory at this point:</p><p><strong>store your data in a persistent database, cache a hot subset of it, and watch your API latencies drop to the floor.</strong></p><p>It works. It’s elegant. It scales. Every backend engineer has written this code in some form.</p><p>And if you’re not careful, it will quietly serve your users stale, wrong data — and you won’t notice until something breaks in production.</p><p>This isn’t a niche edge case. It affects <strong>any system where the same record can be written concurrently</strong> — ride-sharing apps updating driver locations, e-commerce platforms adjusting inventory, social platforms updating follower counts, gaming backends tracking player state. The domain doesn’t matter. The race condition does.</p><p>Let’s talk about <em>cache inconsistency</em> — the sneaky, race-condition-shaped bug that lives quietly in codebases until one high-traffic day it wakes up and causes chaos.</p><h3>The “Good Enough” Code</h3><p>Most code handling the DB-cache pattern looks something like this. Ask an LLM to generate it today and you’ll get something very close — and for low-traffic use cases, it’s <em>completely fine</em>:</p><pre>FUNCTION getRecord(id):<br>    value = cache.get(id)<br>    IF value IS NOT null:<br>        // Cache HIT — fast path, return immediately<br>        RETURN value<br><br>    value = db.get(id)<br>    IF value IS null: RETURN null<br><br>    // Cache MISS — populate the cache for next time<br>    IF shouldCache(value):<br>        cache.set(id, value)<br>    RETURN value<br><br><br>FUNCTION updateRecord(id, newValue):<br>    existing = db.get(id)<br>    IF existing IS null: RETURN null<br><br>    db.set(id, newValue)     // ① Write DB<br>    cache.set(id, newValue)  // ② Write Cache<br><br>    RETURN newValue<br><br><br>FUNCTION deleteRecord(id):<br>    db.delete(id)<br>    cache.delete(id)</pre><p>Read from cache first, fall back to DB on a miss, write to both on update. Clean, readable, gets the job done. Ship it.</p><blockquote><strong><em>⚠ The trap</em></strong></blockquote><blockquote><em>The problem isn’t in what this code </em>does<em>. The problem is in what it </em>assumes<em> — that writes happen one at a time, in a nice orderly queue. In a high-traffic system, they absolutely do not.</em></blockquote><h3>Where This Bites You</h3><p>The problem surfaces anywhere two concurrent writes race to update the same key. The domain is irrelevant — the pattern is universal:</p><ul><li><strong>E-commerce</strong> — two flash sale requests both read stock = 1, both decrement, both write stock = 0 to cache. But DB ends up at -1. You just oversold.</li><li><strong>Ride-sharing</strong> — a driver’s location gets updated by two GPS pings in quick succession. The older ping’s cache write lands last. Your map shows the driver 400m behind where they actually are.</li><li><strong>Gaming</strong> — two simultaneous XP rewards both read score = 1000, both add 500, both write score = 1500. One reward is silently eaten. Your player is furious.</li><li><strong>Social platforms</strong> — a post gets two likes at the same moment. Counter in cache ends up at +1 instead of +2. Low stakes here — but multiply by a million posts and your analytics are fiction.</li></ul><p>Let’s make it concrete with the highest-stakes version of the problem: a fintech wallet. Because if the bug is going to live anywhere rent-free in your head at 2am, it’s there.</p><p>Account ACC000X has a balance of <strong>$50</strong>. Two incoming transfers arrive at nearly the same moment:</p><ul><li>TXN0001 — from ACC0001, sending $100 to ACC000X</li><li>TXN0002 — from ACC0002, sending $100 to ACC000X</li></ul><p>Both should succeed. The final balance should be <strong>$250</strong>. Here’s what actually happens:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Zjuwrn04GJ3lY3KV.png" /></figure><p>Notice what happened at the DB level too — TXN0002 read <em>before</em> TXN0001‘s write was visible, so both transactions started from $50 and added $100. That’s a separate bug (lost update at the DB layer). But even if your database handles that correctly with row-level locking, <strong>the cache write is still a race</strong>.</p><blockquote>The database ended up at $250. The cache ended up at $150. Every request hitting the cache for the next N minutes gets the wrong answer — regardless of what domain you’re in.</blockquote><p>In a social app, a stale like count is a shrug. In a ride-sharing app, a stale driver location is a bad UX. In a fintech system, it’s a compliance issue, a support ticket storm, and potentially a regulatory incident. The bug is the same. The consequences scale with your domain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*or4fmxMdY5dVpVMH.png" /><figcaption>Race Condition</figcaption></figure><h3>Enter Optimistic Concurrency Control</h3><p>There are several ways to tackle this. Pessimistic locking, event sourcing, CQRS — all valid, all with their tradeoffs. But one of the cleanest solutions for the cache layer specifically is <strong>Optimistic Concurrency Control (OCC)</strong>.</p><p>The core idea is beautifully simple: <em>every record carries a version number</em>. Before you overwrite something, you check whether your version is newer than what’s already there. If it’s not — someone beat you to it, and you gracefully back off.</p><ol><li><em>Each record has a </em><strong><em>version</em></strong><em> (a monotonically increasing integer — a timestamp or a counter works).</em></li><li><em>On every write, pass the </em><strong><em>new version</em></strong><em> along with the new value.</em></li><li><em>Only overwrite if </em>stored_version &lt; new_version<em>. If you’re behind, </em><strong><em>discard the write</em></strong><em>. The winner already wrote.</em></li><li><em>This check-and-set must be </em><strong><em>atomic</em></strong><em> — it’s a single indivisible operation, not two separate calls.</em></li></ol><h3>Making It Work — In the DB and the Cache</h3><p>For a relational database like MySQL or PostgreSQL, atomicity is straightforward. A single UPDATE statement with an OCC guard in the WHERE clause does the job perfectly:</p><pre>UPDATE records<br>SET    value    = :newValue,<br>       version  = version + 1<br>WHERE  id       = :recordId<br>  AND  version  = :base_version_for_txn;   -- ← the OCC guard<br>-- rows_affected = 0 means someone already wrote a newer version.<br>-- rows_affected = 1 means we won. Safe to proceed.</pre><p>The database’s transaction isolation handles the rest. If rows_affected == 0, you lost the race — don’t touch the cache.</p><p>For Redis (or DragonflyDB, KeyDB, etc.), there’s no native compare-and-set on arbitrary fields. You need a <strong>Lua script</strong>. Redis executes Lua scripts atomically — the entire script runs without interruption, which is exactly what you need:</p><pre>-- KEYS[1] = record key<br>-- ARGV[1] = new value (JSON blob)<br>-- ARGV[2] = new version (integer)          &lt;- returned after db record update response<br>local current = redis.call(&#39;HGET&#39;, KEYS[1], &#39;version&#39;)<br>if current and tonumber(current) &gt;= tonumber(ARGV[2]) then<br>    -- stored version is &gt;= new version; this is a stale write, bail<br>    return 0<br>end<br>redis.call(&#39;HSET&#39;, KEYS[1], &#39;value&#39;,   ARGV[1])<br>redis.call(&#39;HSET&#39;, KEYS[1], &#39;version&#39;, ARGV[2])<br>return 1   -- success</pre><h3>The Full Pattern, Cleaned Up</h3><p>Putting it all together, here’s what the safe version of the update flow looks like:</p><pre>FUNCTION Get(key):<br>    value, hit = cache.Get(key)<br>    if hit:<br>        return value                   // fast path<br>value = db.Get(key)<br>    if not found: return ErrNotFound<br>    cache.Set(key, value)             // populate cache on miss<br>    return value<br><br>FUNCTION Update(key, newValue, baseVersion):<br>    // Step 1: write to DB with OCC guard<br>    rowsChanged = db.CAS(&quot;<br>        UPDATE record<br>        SET    value   = :newValue,<br>               version = version + 1<br>        WHERE  key     = :key<br>          AND  version == :baseVersion&quot;)<br>    newVersion = baseVersion + 1<br>    if rowsChanged == 0:<br>        return ErrStaleWrite      // lost the race at DB level, stop here<br>    // Step 2: write to cache atomically via Lua CAS<br>    cache.AtomicCAS(key, newValue, newVersion)<br>    // Lua script: only overwrites if stored_version &lt; newVersion<br>    return {value: newValue, version: newVersion}  // ✓ consistent</pre><blockquote><strong><em>✓ What this guarantees</em></strong></blockquote><blockquote><em>Even if two writers race to update the cache, only the one with the </em>higher version number<em> wins. The outdated write is silently dropped. DB and cache end up telling the same story — regardless of whether the record is a wallet balance, an inventory count, a driver location, or a player score</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KADxw6Zcf9IrXuiX.png" /></figure><p>Feel free to use or contribute to the Go library I built around this pattern — <a href="https://github.com/SouravKabiraj/gocachebridge">gocachebridge</a>. PRs and feedback welcome!</p><h3>Parting thought</h3><p>Most software problems are embarrassingly easy to solve — right up until people actually start using your software.</p><h3>Referances</h3><p><a href="https://github.com/SouravKabiraj/gocachebridge">https://github.com/SouravKabiraj/gocachebridge</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af88746b35af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[You’ve written the transaction flow, tested it thoroughly, and deployed it to production.]]></title>
            <link>https://souravkabiraj.medium.com/youve-written-the-transaction-flow-tested-it-thoroughly-and-deployed-it-to-production-b83a9d92d034?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b83a9d92d034</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[transactions]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Wed, 04 Feb 2026 16:19:56 GMT</pubDate>
            <atom:updated>2026-02-04T16:19:56.988Z</atom:updated>
            <content:encoded><![CDATA[<h3>What Really Happens When Your Database Crashes During a Transaction?</h3><p>You’ve written the transaction flow, tested it thoroughly, and deployed it to production. Then it happens: the database crashes. Not a graceful shutdown, but a hard failure. Middle of the day, peak traffic. Your immediate panic isn’t about uptime — it’s about data integrity. Are those committed transactions safe? Are half-executed operations rolled back?</p><p>Picture this: Person A is sending $1,000,000 to Person B. The database is processing this transaction when suddenly — <strong>poof</strong> — the power goes out. Will Person A lose their million? Will Person B get money that doesn’t exist?</p><p>Here’s the SQL that runs behind the scenes:</p><blockquote>BEGIN<br>UPDATE account SET balance = balance — 1000000 WHERE id = “person_a”<br>UPDATE account SET balance = balance + 1000000 WHERE id = “person_b”<br>COMMIT</blockquote><p>What happens if the database crashes right after subtracting from Person A but before adding to Person B? Without some serious safeguards, we’d have a financial horror story on our hands.</p><p>Thankfully, databases have our backs. They follow a golden rule: <strong>Transactions are atomic</strong>. Either the whole thing happens, or none of it does. No half-finished transfers, no disappearing money.</p><p>But how do they pull this off? I discovered the secret when I stumbled upon an article about something called…</p><h3>The “Steal/No-Steal, Force/No-Force” Matrix</h3><p>It sounds like a spy thriller, but it’s actually how databases decide when to write data to disk. Let me break it down.</p><h4>The Dirty Page Tango</h4><p>When your transaction runs, here’s what happens behind the scenes:</p><p>1. The database grabs the needed data from disk (or memory if it’s already there)<br>2. It makes your changes in memory, marking pages as “dirty”<br>3. Eventually, those dirty pages get written back to disk</p><p>The million-dollar question (literally): **When does “eventually” happen?**</p><p>This is where our spy thriller terms come in:</p><p>- <strong>STEAL</strong> = Can write dirty pages to disk <strong>before</strong> transaction commits<br>- <strong>NO-STEAL</strong> = Must wait until commit to write dirty pages<br>- <strong>FORCE</strong> = Must write <strong>all</strong> dirty pages to disk at commit time <br>- <strong>NO-FORCE</strong> = Can write dirty pages whenever it’s convenient</p><p>Mix and match these, and you get four possible policies. Let’s see how they stack up.</p><h4>The Contenders: Four Policies Face Off</h4><p><strong>1. NO-STEAL / FORCE (The Perfectionist)</strong><br><strong>The Good:</strong> Recovery is a breeze. If the database crashes, the disk has exactly what was committed — nothing more, nothing less.<br><strong>The Bad:</strong> Painfully slow. Every commit waits for *all* data to hit disk. Not practical for real-world use.</p><p><strong>2. STEAL / FORCE (The Compromiser)</strong><br><strong>The Good:</strong> Some buffer management flexibility<br><strong>The Bad:</strong> Still slow commits, plus you need undo logging. The worst of both worlds.</p><p><strong>3. NO-STEAL / NO-FORCE (The Memory Hog)</strong><br><strong>The Good:</strong> Fast commits (just write a log)<br><strong>The Bad:</strong> Can’t free up memory used by long-running transactions. Risk of system lockups.</p><p><strong>4. STEAL / NO-FORCE (The Practical Choice)</strong><br><strong>The Good:</strong> Lightning-fast commits, efficient memory use, handles large transactions<br><strong>The Bad:</strong> Complex recovery (needs both undo AND redo operations)</p><h3>And the Winner Is…</h3><p><strong>STEAL / NO-FORCE</strong> takes the crown! 🏆</p><p>Why? Because in the real world:<br>- Database crashes are (thankfully) rare<br>- Transactions happen constantly<br>- Performance matters every single second</p><p>STEAL/NO-FORCE optimizes for the common case: fast, reliable transactions. It accepts that recovery after a crash will be more complex, but that’s a tradeoff worth making.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2igR0qiv9Nl2AvoQQF8ryg.png" /></figure><p>WAL log content after this crash…</p><pre>WAL HEADER<br>Version: 2.0<br>Database: financial_db<br>Checkpoint LSN: 0000000000<br>Timestamp: 2024-01-15 10:30:25.123456<br>========================================<br><br>LSN: 0000000100 [BEGIN_TX]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123500<br>Prev LSN: NULL<br>Type: BEGIN<br>Content: {tx_id: &quot;TX001234&quot;, isolation: &quot;READ_COMMITTED&quot;}<br><br>LSN: 0000000200 [UPDATE]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123550<br>Prev LSN: 0000000100<br>Type: UPDATE<br>Table: account<br>Page ID: 0x1A3F<br>Slot: 42<br>Row ID: person_a<br>Old Value: {balance: 5000000, id: &quot;person_a&quot;, ...}<br>New Value: {balance: 4000000, id: &quot;person_a&quot;, ...}<br>Undo Info: SET balance = balance + 1000000 WHERE id = &quot;person_a&quot;If the transaction is successfully committed, the corresponding entries will be recorded in the write-ahead log (WAL) to ensure durability and consistency.</pre><p>The WAL content if the transaction was commited properly…</p><pre>WAL HEADER<br>Version: 2.0<br>Database: financial_db<br>Checkpoint LSN: 0000000000<br>Timestamp: 2024-01-15 10:30:25.123456<br>========================================<br><br>LSN: 0000000100 [BEGIN_TX]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123500<br>Prev LSN: NULL<br>Type: BEGIN<br>Content: {tx_id: &quot;TX001234&quot;, isolation: &quot;READ_COMMITTED&quot;}<br><br>LSN: 0000000200 [UPDATE]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123550<br>Prev LSN: 0000000100<br>Type: UPDATE<br>Table: account<br>Page ID: 0x1A3F<br>Slot: 42<br>Row ID: person_a<br>Old Value: {balance: 5000000, id: &quot;person_a&quot;, ...}<br>New Value: {balance: 4000000, id: &quot;person_a&quot;, ...}<br>Undo Info: SET balance = balance + 1000000 WHERE id = &quot;person_a&quot;<br><br>LSN: 0000000300 [UPDATE]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123600<br>Prev LSN: 0000000200<br>Type: UPDATE<br>Table: account<br>Page ID: 0x1B47<br>Slot: 15<br>Row ID: person_b<br>Old Value: {balance: 1000000, id: &quot;person_b&quot;, ...}<br>New Value: {balance: 2000000, id: &quot;person_b&quot;, ...}<br>Undo Info: SET balance = balance - 1000000 WHERE id = &quot;person_b&quot;<br><br>LSN: 0000000400 [COMMIT]<br>Transaction ID: TX001234<br>Timestamp: 2024-01-15 10:30:25.123650<br>Prev LSN: 0000000300<br>Type: COMMIT<br>Content: {tx_id: &quot;TX001234&quot;, commit_lsn: 0000000400}<br><br>LSN: 0000000500 [CHECKPOINT]<br>Transaction ID: SYSTEM<br>Timestamp: 2024-01-15 10:30:30.000000<br>Type: CHECKPOINT<br>Content: {active_txs: [], min_recovery_lsn: 0000000100}</pre><h4>How Recovery Works in This World</h4><p>When a database using STEAL/NO-FORCE crashes and restarts, it:</p><p>1. <strong>REDOs</strong> committed transactions that might not have made it to disk<br>2. <strong>UNDOs</strong> uncommitted transactions that partially made it to disk</p><p>All this magic happens using the Write-Ahead Log (WAL) — a special file that records every change before it happens. It’s like having a flight recorder for your database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6rbmf16RRebMNT_Bmry2nA.png" /></figure><h3>The Bottom Line</h3><p>Next time you transfer money or make any important database transaction, you can breathe a little easier knowing that even if the lights go out, your data won’t disappear into the void.</p><p>Databases choose complexity during rare crash recovery to give you speed and reliability every day. And that’s worth its weight in… well, a million dollars.</p><p><strong>Have you ever experienced a database crash during a critical operation? Share your story in the comments!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b83a9d92d034" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How We Solved a Critical Race Condition in Banking Systems]]></title>
            <link>https://souravkabiraj.medium.com/how-we-solved-a-critical-race-condition-in-banking-systems-623c140b796d?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/623c140b796d</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[banking-technology]]></category>
            <category><![CDATA[fintech]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Sun, 28 Dec 2025 16:39:54 GMT</pubDate>
            <atom:updated>2025-12-28T16:44:27.334Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes the best solutions come from unexpected places. When we were stuck with a critical concurrency problem in our banking system, the answer didn’t come from financial engineering textbooks — it came from rate limiting algorithms used in web servers.</p><p>This is the story of how we solved a problem that was causing incorrect interest calculations and keeping our operations team up at night.</p><h3>The Midnight Challenge</h3><p>Every night at midnight, banks around the world collectively hold their breath. It’s EOD time — End of Day — and if you think your company’s batch jobs are stressful, try processing millions of loan accounts where a single mistake can trigger regulatory nightmares and very stern emails from auditors.</p><p>In an ideal world, the EOD job would be instantaneous — <em>snap</em> your fingers at exactly 00:00:00 and you’ve got a perfect snapshot. But here’s the thing about ideal worlds: they don’t exist when you’re crunching through millions of transactions. These jobs can take anywhere from minutes to hours, and during that window? Things get messy.</p><p>Let me walk you through the concurrency problem that was keeping our operations team up at night, and how we actually solved it.</p><h3>The Problem: Timing and Accuracy</h3><p>Consider this scenario at a digital bank processing hundreds of thousands of loan transactions:</p><p><strong>Loan Details:</strong></p><ul><li>Principal: $1,000</li><li>APR: 365%</li></ul><p><strong>What Happened:</strong></p><ul><li><strong>00:01</strong> — EOD job starts</li><li><strong>00:03</strong> — Customer makes a $200 payment</li><li><strong>00:03</strong> — Payment processes immediately, reducing principal to $800</li><li><strong>00:15</strong> — Interest accrual runs on the current principal of $800</li><li><strong>Result:</strong> Interest calculated = $8</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*naPf9bhA-rdyhFdv9_96hA.png" /></figure><p><strong>The Issue:</strong> The interest should have been calculated on the original $1,000 principal amount before the payment, giving us $10 in interest, not $8.</p><p>This might seem like a small discrepancy, but in banking, precision is mandatory. Banks maintain calculations accurate to 7 decimal places, which means even a $2 difference is unacceptable. When you’re dealing with millions of loans and strict regulatory requirements, every cent must be accounted for correctly.</p><h3>First Attempt: The Blackout Window</h3><p>Our initial solution was straightforward — implement a blackout period from 00:00 to 02:00 where all payments would be queued and processed only after EOD completion.</p><h3>The Success Case</h3><p>Here’s how the blackout window solved our original problem:</p><p><strong>Loan Details:</strong></p><ul><li>Principal: $1,000</li><li>APR: 365%</li></ul><p><strong>Timeline with Blackout:</strong></p><ul><li><strong>00:01</strong> — EOD job starts</li><li><strong>00:03</strong> — Customer pays $200 → Payment queued (blackout active)</li><li><strong>00:15</strong> — Interest accrual runs on original $1,000 principal</li><li><strong>Result:</strong> Interest = $10 ✓</li><li><strong>02:00</strong> — Blackout ends</li><li><strong>02:01</strong> — Queued payment processes, principal reduces to $800</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3sdDNgGfVp-Cfg5pIleDRg.png" /></figure><p>This seemed to work perfectly, but we soon discovered a critical flaw.</p><h3>The Double Payment Problem</h3><p>Here’s what went wrong:</p><ul><li><strong>00:03</strong> — Customer pays $200</li><li><strong>00:05</strong> — Customer pays another $200</li><li><strong>00:15</strong> — Accrual completes, interest = $10</li><li><strong>02:01</strong> — Both queued payments process simultaneously</li><li><strong>Problem:</strong> Both transactions read the same state (Principal: $1,000, Interest: $10) and both try to pay off the $10 interest first</li><li><strong>Result:</strong> Interest balance goes negative to -$10</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zaI3efSH6FC2FP1Db42AOw.png" /></figure><p>This was a classic race condition. We had solved one problem but created another.</p><h3>The Solution: Token Bucket Pattern</h3><p>After analyzing the problem, we found inspiration in an unexpected place — the Token Bucket algorithm commonly used for rate limiting in web systems.</p><p><strong>Token Bucket Basics:</strong> The algorithm uses a bucket that fills with tokens at a fixed rate. Each incoming request needs one token to proceed. If the bucket is empty, requests must wait. This naturally throttles traffic and prevents system overload.</p><p>We adapted this concept for our banking scenario. Instead of rate limiting, we used tokens to ensure sequential processing and post-accrual guarantees.</p><h3>How Our Implementation Works</h3><p>We created a distributed locking mechanism using Redis with a simple principle: <strong>no token, no processing</strong>.</p><p><strong>Key Concept:</strong> Each loan gets a unique token created ONLY after its interest accrual completes. Payments can only proceed if they successfully acquire this token.</p><p><strong>Redis Key Format:</strong> LOAN_ID#YYYYMMDD</p><h3>The Flow in Action</h3><p>Let’s revisit our scenario with Loan ID LOC0001234:</p><ol><li><strong>00:01</strong> — EOD job starts</li><li><strong>00:03</strong> — Customer payment (TXN001) arrives, checks for token, finds none, waits</li><li><strong>00:05</strong> — Second payment (TXN002) arrives, also waits</li><li><strong>00:15</strong> — Accrual completes for LOC0001234, interest = $10</li><li><strong>00:15</strong> — Token created in Redis: SET LOC0001234#28122025 null</li><li><strong>00:15</strong> — Both TXN001 and TXN002 detect the token</li><li><strong>00:15</strong> — TXN001 acquires lock: SET LOC0001234#28122025 TXN001 NX</li><li><strong>00:15</strong> — TXN002 tries to acquire, fails (token already locked)</li><li><strong>00:16</strong> — TXN001 completes payment, releases lock</li><li><strong>00:16</strong> — TXN002 acquires lock and processes successfully</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*HsqSgAe8qsaiU-Oo9v8rtw.png" /></figure><pre># EOD Job - After interest accrual completes<br>def complete_interest_accrual(loan_id, date):<br>    # Calculate interest<br>    interest = calculate_interest(loan_id)<br>    update_loan_interest(loan_id, interest)<br>    <br>    # Create token to signal accrual is complete<br>    token_key = f&quot;{loan_id}#{date}&quot;<br>    redis.set(token_key, null)<br>    redis.expire(token_key, 86400)  # Expire after 24 hours<br><br># Payment Processing<br>def process_payment(loan_id, amount, transaction_id):<br>    date = get_current_date()<br>    token_key = f&quot;{loan_id}#{date}&quot;<br>    <br>    # Wait for token to exist (accrual must complete first)<br>    while not redis.exists(token_key):<br>        sleep(60000)  # Wait then retry<br>        return process_payment(loan_id, amount, transaction_id)<br>    <br>    # Try to acquire the lock atomically<br>    # NX flag ensures only one transaction gets the lock<br>    lock_acquired = redis.set(token_key, transaction_id, NX=True)<br>    <br>    if not lock_acquired:<br>        # Another transaction holds the lock, retry<br>        sleep(60000)  # Wait then retry<br>        return process_payment(loan_id, amount, transaction_id)<br>    <br>    try:<br>        # We have the lock! Process the payment<br>        <br>    finally:<br>        # Always release the lock<br>        redis.set(token_key, null)</pre><h3>Why This Approach Works</h3><p>The solution leverages Redis’s NX (Not eXists) flag combined with atomic SET operations to provide several guarantees:</p><ul><li><strong>Sequential Processing:</strong> Only one transaction can hold the lock at any given time</li><li><strong>Post-Accrual Guarantee:</strong> The token only exists after interest calculation is complete</li><li><strong>Natural Queuing:</strong> Failed lock attempts automatically retry without additional logic</li><li><strong>No Lost Payments:</strong> Every transaction eventually gets processed in order</li></ul><h3>Key Takeaways</h3><p><strong>Race conditions in financial systems have serious consequences</strong></p><p>Small timing issues can lead to significant financial discrepancies, compliance violations, and audit failures. In banking, precision isn’t optional.</p><p><strong>Simple solutions can mask deeper problems</strong></p><p>Our blackout window appeared elegant and solved the original issue, but it introduced a new edge case we hadn’t anticipated. Thorough testing and consideration of concurrent scenarios is essential.</p><p><strong>Cross-domain patterns can provide elegant solutions</strong></p><p>The Token Bucket algorithm from web infrastructure proved to be an excellent fit for our banking concurrency problem. Don’t limit yourself to domain-specific solutions.</p><p><strong>Atomic operations provide strong guarantees</strong></p><p>Redis’s atomic commands gave us the consistency guarantees we needed without the complexity of distributed transaction management.</p><h3>Conclusion</h3><p>Banking systems operate at a scale where timing precision is critical and small errors can compound into major issues. By combining domain knowledge with proven concurrency patterns from other fields, we transformed a potential operational nightmare into a robust, scalable solution.</p><p>The result: accurate interest calculations, reliable payment processing, and an operations team that can finally rest easy during EOD jobs.</p><p>Have you encountered similar concurrency challenges in your systems? I’d be interested to hear about your approach.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=623c140b796d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Low-Level Design: Converting a non-linear code to a linear code]]></title>
            <link>https://souravkabiraj.medium.com/low-level-design-converting-a-non-linear-code-to-a-linear-code-1cdb2bd6360b?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cdb2bd6360b</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[low-level-design]]></category>
            <category><![CDATA[refactoring]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Sat, 03 Jul 2021 11:57:24 GMT</pubDate>
            <atom:updated>2021-07-05T10:36:11.294Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, I am going to discuss a very basic design pattern. How to convert a non-linear code (such as <strong>if-else,</strong> <strong>switch-case</strong>) to a linear code.</p><p>Currently, almost all modern languages have <strong>if-else</strong>, <strong>switch-case</strong> which help us to execute specific code for a certain condition. Which make our program non-linear.</p><pre>if (<em>condition1) </em>{<br>   executeForCondition1(...);<br>} else if (<em>condition2) </em>{<br>   executeForCondition2(...);<br>} else if (<em>condition3) </em>{<br>   executeForCondition3(...);<br>} else {<br>   executeForDefaultCase(...);<br>}</pre><pre>------------------------------------------------------------------</pre><pre>switch (condition) {<br>   case condition1:<br>      executeForCondition1(...);<br>   case condition2:<br>      executeForCondition2(...);<br>   case condition3:<br>      executeForCondition3(...);<br>   default:<br>      executeForDefaultCase(...);<br>}</pre><p>These conditional syntaxes make our program a little difficult to read (though it is not bad all the time).</p><p>This design pattern replaces the above code with only one line of code and hides all conditional operators. The code will look something like this:</p><pre><strong>executor</strong>.<strong>getHandlerFor</strong>(condition).<strong>execute(...)</strong>;</pre><p>Let&#39;s take a problem statement: A company want to design a system which assigns laptop to its employees. In this case, our <strong>condition </strong>is the employee’s department.</p><p>Normally using a conditional operator the code looks something like this.</p><pre>switch (department) {<br>   case Engineering:<br>      employee.assign(macPro16);<br>   case Sales:<br>      employee.assign(LenovoIdeaPad);<br>   case Operation:<br>      employee.assign(macPro13);<br>   default:<br>      employee.assign(DellXPS);<br>}</pre><p>Let refactor this to make it linear:</p><pre>LaptopProvider.getLaptopFor(department).assignTo(employee);</pre><p>Now only one-liner code replaced the whole conditional operator part. Let looks at how it is designed:</p><pre><strong>USE</strong></pre><pre>Employee employee = new Employee(<em>ENGINEERING</em>);</pre><pre>-----------------------------------------------------------------</pre><pre><strong>new LaptopProvider().getLaptopFor(employee.role).assignTo(employee); //</strong> This above line replaced the whole if else condition.</pre><pre>-----------------------------------------------------------------</pre><pre><strong>LaptopProvider.java</strong></pre><pre>class LaptopProvider {<br>    Map&lt;Role, Laptop&gt; roleLaptopMap<strong>;</strong><br><br>    public LaptopProvider() {<br>        roleLaptopMap = new HashMap&lt;&gt;();<br>        roleLaptopMap.put(<em>ENGINEERING</em>, new MACPro16());<br>        roleLaptopMap.put(<em>SALES</em>, new LenovoIdeaPad());<br>        roleLaptopMap.put(<em>OPERATION</em>, new MACPro13());<br>        roleLaptopMap.put(<em>MARKETING</em>, new DellXPS());<br>    }<br><br>    Laptop getLaptopFor(Role role) {<br>        return roleLaptopMap.get(role);<br>    }<br>}</pre><pre><strong>Laptop.java</strong></pre><pre>class LenovoIdeaPad implements Laptop {<br>    @Override<br>    public String getBrand() {<br>        return &quot;Lenovo Idea Pad&quot;;<br>    }<br><br>    @Override<br>    public void assignTo(Employee employee) {<br>        employee.assign(this);<br>    }<br>}<br><br>class DellXPS implements Laptop {<br>    @Override<br>    public String getBrand() {<br>        return &quot;DELL XPS&quot;;<br>    }<br><br>    @Override<br>    public void assignTo(Employee employee) {<br>        employee.assign(this);<br>    }<br>}<br><br>class MACPro16 implements Laptop {<br>    @Override<br>    public String getBrand() {<br>        return &quot;MAC Pro 16&quot;;<br>    }<br><br>    @Override<br>    public void assignTo(Employee employee) {<br>        employee.assign(this);<br>    }<br>}<br><br>class MACPro13 implements Laptop {<br>    @Override<br>    public String getBrand() {<br>        return &quot;MAC Pro 13&quot;;<br>    }<br><br>    @Override<br>    public void assignTo(Employee employee) {<br>        employee.assign(this);<br>    }<br>}<br><br>interface Laptop {<br>    String getBrand();<br><br>    void assignTo(Employee employee);<br>}</pre><pre><strong>Employee.java</strong></pre><pre>class Employee {<br>    Role role;<br><br>    public Employee(Role role) {<br>        this.role = role;<br>    }<br><br>    public void assign(Laptop lenovoIdeaPad) {<br><br>    }<br>}<br><br>enum Role {<br>    <em>ENGINEERING</em>, <em>SALES</em>, <em>MARKETING</em>, <em>OPERATION<br></em>}</pre><p>Basically, we are taking the help of <strong>Map</strong> data structure where we are setting conditions as <strong>key</strong> and keeping the executor object as the <strong>value</strong>.</p><p>Now, the question is “Can we use this pattern all the time?”. The answer is “No”. Most of the time, the if-else condition has only 2–3 conditions, which is simple. It will be over-engineering if you implement this pattern. But if there are many conditions or a scenario where the number of conditions will increase soon, you can implement this design pattern.</p><p>Thanks for reading this article and happy coding. 😊👨🏼‍💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cdb2bd6360b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Low-Level Design: Multiform Application]]></title>
            <link>https://souravkabiraj.medium.com/low-level-design-multiform-application-e3817a14f3cd?source=rss-7b4f52ca5b5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3817a14f3cd</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[system-design-project]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[low-level-design]]></category>
            <dc:creator><![CDATA[Sourav Kabiraj]]></dc:creator>
            <pubDate>Sat, 13 Feb 2021 21:06:59 GMT</pubDate>
            <atom:updated>2021-05-30T13:56:13.896Z</atom:updated>
            <content:encoded><![CDATA[<p>This post is all about the low-level design of the multiform application. In my professional career, I have created a lot of software that comes under this umbrella. Hopefully, It will be helpful in your next LLD.</p><h3>Overview</h3><p>“What is the Multiform Application?”<br>Let start with an example. Say you want to create a <strong>sign-up flow</strong> for one of your applications which takes a lot of details (<strong><em>personal details</em></strong>, <strong><em>contact details</em></strong><em>, </em><strong><em>address</em></strong><em>, </em><strong><em>personal interests</em></strong>)from the user. To take those details you can easily create one big form and take all the details. To be very honest this kind of development is extremely easy for front-end and back-end devs. But for a user, it will be a nightmare. To deal with that problem most of the designers breaks this big form into multiple smaller form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YG6AJoz_t6Eca2VNCjl7ew.png" /><figcaption>Sample sign-up flow</figcaption></figure><h3>Problems</h3><p>Now as a developer you have to deal with a lot of problems:<br><strong>1.</strong> You have to create a hell of <strong>lot of endpoints </strong>(backend problem). Front-end devs have to create multiple forms/pages.<br><strong>2.</strong> You have to keep a track of the user application path so that you can give the user an option to <strong>navigate back and forth</strong> in the application.<br><strong>3.</strong> User can exit application flow anytime and come back later, At that time you want to show your user the page from where he left. And this is a challenging scenario.[<strong>Reopened application</strong>]<strong><br>4.</strong> After all this, your organization wants to run all of the <strong>analytics</strong> to know the user behavior and all. So you have to save these data considering this.</p><h3>Low-Level Design</h3><p>Now I will propose some ideas(a framework) to deal with the multiform application. This is completely my personal opinion. You are welcome to disagree with this and form your own opinion.</p><p>Let&#39;s start with the solution approach. I named it <strong>Stage Monitor</strong>.</p><p>Let&#39;s consider each form as an application <strong>Stage</strong>. [like <em>personal details</em>, <em>contact details, address, personal interests</em>]<br>We also need an application health monitor for each and every user (I named it <strong>StageMonitor</strong>) which will store application data as a whole. It wouldn’t be wrong if I call it the nucleus of our application.</p><p>It exposes all APIs that are needed to control an application. For example:</p><ol><li><strong>Initialise</strong> API initiates an application.</li><li><strong>Proceed </strong>API submits a form/stage details and initiates next form/stage</li><li><strong>Get Stage For User </strong>API is used to fetch form details of a particular User</li><li><strong>Execute</strong> API is used to execute some commands on a certain form</li></ol><h3>Stage</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wBE5Xx6RPpi-dEQryKFipQ.jpeg" /><figcaption>Stage Model</figcaption></figure><p>Our first component is <strong>Stage</strong> which is quite equivalent to a page/form.</p><p>If we look at a page very closely, we will see users can do very few operations on it and it exactly looks like a doubly linked list node (it can have a previous page and next page).</p><p>For example: In the <strong>Sample sign-up flow </strong>image, the Address page has a previous page (i.e<strong> Contact Page</strong>) and a next page (i.e. <strong>Personal Interest Page</strong>).</p><p>Let&#39;s list down all the behaviors of page/form<br>1. Click on the Next Button.<br>2. Click on Back Button.<br>3. Run some kind of commands (say <strong>verify</strong> <strong>phone number/email Id </strong>etc.)</p><p>After seeing the previous list we can easily tell the APIs that a stage will expose:</p><ol><li><strong>Initialize</strong> API initiates a stage (defines what happens when a user creates a new stage).</li><li><strong>Submit(Next) </strong>API submits form details</li><li><strong>Rollback (Back) </strong>API gives the ability to rollback or to go back to the previous page</li><li><strong>Get Stage</strong> API is used to fetch the current page details</li><li><strong>Execute</strong> API is used to execute some commands on the form</li></ol><p>Let&#39;s create an interface to manage stage operations:</p><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/Stage.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/Stage.java</a></pre><pre>public class <strong>Stage&lt;T&gt;</strong> {    <br>    private String <strong>id</strong>;<br>    private String <strong>name</strong>;<br>    private <strong>T data</strong>;<br>    private String <strong>previousStageId</strong>;<br>    private String <strong>nextStageId</strong>;<br>    private Date <strong>createdAt</strong>;<br>    private Date <strong>updatedAt</strong>;<br>    public Stage(String name, String previousStageId) {<br>        this.name = name;<br>        this.previousStageId = previousStageId;<br>    }<br>}</pre><pre>--------------------------------------------------------------------</pre><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/StageManager.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/StageManager.java</a></pre><pre>public interface <strong>StageManager&lt;T&gt;</strong> {<br>   // initiate will be called while use tries to open perticular    page for first time</pre><pre><strong>   Stage&lt;T&gt; initiate();</strong></pre><pre>   // getStage return stage details with stage data.                                         </pre><pre><strong>   Optional&lt;Stage&lt;T&gt;&gt; getStage(String id);</strong></pre><pre>   // submit save all data of current stage and moves to next page and initiate next stage  </pre><pre>   <strong>Stage&lt;T&gt; submit(Stage&lt;T&gt; stage);   </strong></pre><pre><strong>   // </strong>execute other kind of commends which doesn’t affect application progress. [ex. Send otp, verify email etc]</pre><pre>   <strong>Stage&lt;T&gt; execute(String cmd, Stage&lt;T&gt; stage);</strong><br>}</pre><p>Now you just have to implement all these methods for every stage.</p><p>Below I have implemented ContactDetailsStage:</p><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/resume/contact_details/ContactDetailsStageManager.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/resume/contact_details/ContactDetailsStageManager.java</a><br><br>import java.util.Optional;<br><br>@Component<br>public class ContactDetailsStageManager implements StageManager&lt;ContactDetails&gt; {<br>    @Autowired<br>    StageRepository&lt;ContactDetails&gt; stageRepository;<br><br>    @Autowired<br>    EducationDetailsStageManager educationDetailsManager;<br><br>    @Override<br>    public Stage&lt;ContactDetails&gt; initiate() {<br>        return stageRepository.save(new Stage&lt;ContactDetails&gt;(&quot;contact_details&quot;, null));<br>    }<br><br>    @Override<br>    public Optional&lt;Stage&lt;ContactDetails&gt;&gt; getStage(String id) {<br>        return stageRepository.findById(id);<br>    }<br><br>    @Override<br>    public Stage&lt;ContactDetails&gt; submit(Stage&lt;ContactDetails&gt; contactDetailsStage) {<br>        if (contactDetailsStage.getNextStageId() == null) {<br>            Stage educationDetailsStage = educationDetailsManager.initiate();<br>            contactDetailsStage.setNextStageId(educationDetailsStage.getId());<br>        }<br>        Stage&lt;ContactDetails&gt; savedPersonalDetailsStage = stageRepository.save(contactDetailsStage);<br>        return savedPersonalDetailsStage;<br>    }<br><br>    @Override<br>    public Stage&lt;ContactDetails&gt; execute(String cmd, Stage&lt;ContactDetails&gt; stage) {<br>        return null;<br>    }<br>}</pre><h3>StageMonitor</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FXHhbXqSNV1kznycnZIhLg.jpeg" /><figcaption>Stage Monitor</figcaption></figure><p>Our next and last component is StageMonitor. The basic task of the stage monitor is to keep information about the application state and how the user interacts with the application. There will be exactly one <strong>StageMonitor</strong> for every user which will hold all his application <strong>Stages.</strong></p><p>First, list down all the task that the user wants to perform while they are felling an application:<br>1. <strong>Initialize</strong> the application for the current user.<br>2. Proceed to the <strong>next</strong> application page/<strong>stage</strong>.<br>3. Get the <strong>Last</strong> page/<strong>Stage</strong> of the application.<br>4. <strong>Execute</strong> some <strong>stage</strong> commands.</p><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/StageManager.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_manager/StageManager.java</a></pre><pre>public class <strong>StageMonitor</strong> {<br>    public String <strong>id</strong>;    <br>    public String <strong>userId</strong>;<br>    public String <strong>appName</strong>;    <br>    public String <strong>currentActiveStageId</strong>;<br>    public List&lt;Stage&gt; <strong>stages</strong>;<br>}</pre><p>Let’s create an interface to manage StageMonitor operations:</p><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_monitor_manager/StageMonitorManager.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/stage_monitor_manager/StageMonitorManager.java</a></pre><pre>public interface <strong>StageMonitorManager</strong> {</pre><pre>     // Initialise Application<br><strong>     StageMonitor initiateFor(String userId, String appName);</strong></pre><pre>     // proceed to next stage after submitting current stage<br><strong>     StageMonitor proceed(String userId, String appName, Stage stage);</strong></pre><pre>     // getByUserIdAndAppName returns stage monitor by user and application Id<br><strong>     Optional&lt;StageMonitor&gt; getByUserIdAndAppName(String userId, String appName);</strong></pre><pre>     // execute runs stage specific commands<br><strong>     StageMonitor execute(StageMonitor stageMonitor, Stage stage, String cmd);</strong></pre><pre>}</pre><p>You have to implement this <strong>StageMonitorManager</strong> for your application and that&#39;s all. Expose <strong>StageMonitorManager </strong>methods as API endpoints so that frontend applications can consume.</p><p>Below I have implemented a ResumeMonitorManager:</p><pre><a href="https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/resume/ResumeManager.java">https://github1s.com/SouravKabiraj/stage-monitor/blob/HEAD/src/main/java/com/opensource/stage_monitor/resume/ResumeManager.java</a><br><br>@Component<br>public class <strong>ResumeManager</strong> implements <strong>StageMonitorManager</strong> {<br>    @Autowired<br>    StageMonitorRepository stageMonitorRepository;<br><br>    @Autowired<br>    ResumeStageFactory resumeStageFactory;<br><br>    @Override<br>    public StageMonitor <strong>initiateFor</strong>(String userId, String appName) {<br>        StageMonitor stageMonitor = new StageMonitor(userId, appName);<br>        StageManager personalDetailsStageManager = resumeStageFactory.getStageManagerByName(&quot;personal_details&quot;);<br>        Stage personalDetailsStage = personalDetailsStageManager.initiate();<br>        stageMonitor.setCurrentActiveStageId(personalDetailsStage.getId());<br>        stageMonitor.addStage(personalDetailsStage);<br>        return stageMonitorRepository.save(stageMonitor);<br>    }<br><br>    @Override<br>    public StageMonitor <strong>proceed</strong>(String userId, String appName, Stage stage) {<br>        StageManager stageManager = resumeStageFactory.getStageManagerByName(stage.getName());<br>        Stage submittedStage = stageManager.submit(stage);<br>        StageMonitor fetchedStageMonitor = stageMonitorRepository.findByUserIdAndAppName(userId, appName).get();<br>        fetchedStageMonitor.updateStage(submittedStage);<br>        if (submittedStage.getNextStageId() != null) {<br>            Optional&lt;Stage&gt; newlyCreatedStage = stageManager.getStage(submittedStage.getNextStageId());<br>            if (newlyCreatedStage.isPresent()) {<br>                Stage newStage = newlyCreatedStage.get();<br>                newStage.setPreviousStageId(fetchedStageMonitor.getLastStageId());<br>                fetchedStageMonitor.setCurrentActiveStageId(newStage.getId());<br>                fetchedStageMonitor.addStage(newStage);<br>            }<br>        }<br>        return stageMonitorRepository.save(fetchedStageMonitor);<br>    }<br><br>    @Override<br>    public Optional&lt;StageMonitor&gt; <strong>getByUserIdAndAppName</strong>(String userId, String appName) {<br>        Optional&lt;StageMonitor&gt; stageMonitorList = stageMonitorRepository.findByUserIdAndAppName(userId, appName);<br>        return stageMonitorList.isEmpty() ? Optional.empty() : Optional.of(stageMonitorList.get());<br>    }<br><br>    @Override<br>    public StageMonitor <strong>execute</strong>(StageMonitor stageMonitor, Stage stage, String cmd) {<br>        StageManager stageManager = resumeStageFactory.getStageManagerByName(stage.getName());<br>        stageManager.execute(cmd, stage);<br>        return stageMonitor;<br>    }<br>}</pre><p>Please find the following link for Github link where I used this stage monitor to implement the resume builder application:<br><a href="https://github1s.com/SouravKabiraj/stage-monitor">https://github.com/SouravKabiraj/stage-monitor</a></p><h3>Conclusion</h3><p>If you implement your next application using this design you have to create only 4 endpoints. Plus adding applications or new stages to an existing application is also easy. Just implement StageManager/StageMonitorManager and you are good to go…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3817a14f3cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>