<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Aditya's Newsletter: System Design]]></title><description><![CDATA[This includes all the learnings about system design, interview problems, deep dives]]></description><link>https://adityatrivedi17.substack.com/s/system-design</link><image><url>https://substackcdn.com/image/fetch/$s_!tWEc!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F068dc990-80e4-4463-b5d0-ad204c8e1f2b_1086x1086.png</url><title>Aditya&apos;s Newsletter: System Design</title><link>https://adityatrivedi17.substack.com/s/system-design</link></image><generator>Substack</generator><lastBuildDate>Tue, 21 Jul 2026 07:00:50 GMT</lastBuildDate><atom:link href="https://adityatrivedi17.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Aditya Trivedi]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[adityatrivedi17@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[adityatrivedi17@substack.com]]></itunes:email><itunes:name><![CDATA[Aditya Trivedi]]></itunes:name></itunes:owner><itunes:author><![CDATA[Aditya Trivedi]]></itunes:author><googleplay:owner><![CDATA[adityatrivedi17@substack.com]]></googleplay:owner><googleplay:email><![CDATA[adityatrivedi17@substack.com]]></googleplay:email><googleplay:author><![CDATA[Aditya Trivedi]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Leetcode System Design]]></title><description><![CDATA[The first thing people usually do while designing systems is jump directly into databases, microservices, Kubernetes, Redis, queues, and scaling.]]></description><link>https://adityatrivedi17.substack.com/p/leetcode-system-design</link><guid isPermaLink="false">https://adityatrivedi17.substack.com/p/leetcode-system-design</guid><dc:creator><![CDATA[Aditya Trivedi]]></dc:creator><pubDate>Tue, 02 Jun 2026 17:15:59 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/01792558-c281-489e-890d-7ae67b3e7043_1731x909.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The first thing people usually do while designing systems is jump directly into databases, microservices, Kubernetes, Redis, queues, and scaling.</p><p>That is the wrong approach.</p><p>When designing systems like LeetCode, start with a simple question:</p><blockquote><p>&#8220;What exactly are we trying to build?&#8221;</p></blockquote><p>System design is not about throwing fancy technologies into a diagram.<br>It is about solving a problem step-by-step.</p><p>So let us build this like a story.</p><h2>Step 1 &#8212; Understanding the Product</h2><p>Before touching architecture, define the features.</p><p>For a LeetCode-like platform, the core requirements are:</p><ul><li><p>Authentication</p></li><li><p>Problems / Problem List</p></li><li><p>Submissions</p></li><li><p>Leaderboard</p></li><li><p>Discussions</p></li></ul><p>Now let us expand what &#8220;<strong>Submissions</strong>&#8221; actually means.</p><p>A submission is not just code.</p><p>It contains:</p><ul><li><p>Problem statement reference</p></li><li><p>User code</p></li><li><p>Programming language</p></li><li><p>Test cases</p></li><li><p>Submission status</p></li><li><p>Execution result</p></li><li><p>Memory usage</p></li><li><p>Runtime</p></li><li><p>Timestamps</p></li></ul><p>This becomes the heart of the entire system.</p><p>Everything else is secondary.</p><div><hr></div><h2>Step 2 &#8212; The Most Important Rule</h2><p>Whenever a user submits code:</p><blockquote><p>Save it immediately to the database.</p></blockquote><p>Do not first execute the code.</p><p>Do not first validate it.</p><p>Do not first send it somewhere else.</p><p>Persist it first.</p><p>Why?</p><p>Because systems fail.</p><p>Workers crash. Containers die. Servers restart. Queues get delayed.</p><p>But if the submission is already stored safely in the database, you never lose user data.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_TMY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_TMY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 424w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 848w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 1272w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_TMY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png" width="1456" height="239" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:239,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!_TMY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 424w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 848w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 1272w, https://substackcdn.com/image/fetch/$s_!_TMY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F122f5626-f6a2-4715-8912-e41a75fede08_1742x286.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>That is why in the architecture:</p><ul><li><p>Client sends submission</p></li><li><p>Primary server receives it</p></li><li><p>Entry is created in PostgreSQL immediately</p></li></ul><p>Only after that does further processing begin.</p><p>This is an extremely important system design principle.</p><div><hr></div><h2>Step 3 &#8212; Thinking About Scale</h2><p>Now let us talk about the &#8220;<em><strong>fancy words</strong></em>&#8221;.</p><p>People hear terms like:</p><p><strong>CAP Theorem, Horizontal Scaling, Throughput, Latency, Fault Tolerance, Availability</strong></p><p>And immediately try to force them everywhere.</p><p>Do not do that.</p><p>Use concepts only where they actually matter.</p><p>For LeetCode, the biggest challenge is not authentication.</p><p>It is code execution.</p><p>Because executing user code is:</p><ul><li><p>CPU intensive</p></li><li><p>Dangerous</p></li><li><p>Slow</p></li><li><p>Unpredictable</p></li><li><p>Resource heavy</p></li></ul><p>One user might submit:</p><pre><code><code>while True: 
    pass
</code></code></pre><p>Another user may try:</p><pre><code><code>rm -rf /
</code></code></pre><p>Another might try memory bombs.</p><p>So the entire architecture revolves around handling code execution safely and at scale.</p><div><hr></div><h2>Step 4 &#8212; High-Level Flow</h2><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vmTC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vmTC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 424w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 848w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 1272w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vmTC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png" width="1456" height="163" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:163,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!vmTC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 424w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 848w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 1272w, https://substackcdn.com/image/fetch/$s_!vmTC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4db939f5-65ed-49e5-bf49-dbcb10792d33_1964x220.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Here is the overall flow of the system:</p><ol><li><p>Client submits code</p></li><li><p>Primary server receives submission</p></li><li><p>Submission is stored in PostgreSQL</p></li><li><p>Job is pushed into AWS SQS</p></li><li><p>Worker picks job from queue</p></li><li><p>Worker sends code to execution engine</p></li><li><p>Code runs inside isolated runtime containers</p></li><li><p>Result gets stored back in database</p></li><li><p>Client polls for status updates</p></li></ol><p>Simple.</p><p>Do not overcomplicate it.</p><div><hr></div><h2>Step 5 - Why We Use a Queue</h2><p>The queue is one of the most important parts of the architecture.</p><p>In this design, <strong>AWS SQS</strong> acts as a buffer between:</p><ul><li><p>API servers</p></li><li><p>Code execution system</p></li></ul><p>Without queues, your primary server becomes overloaded very quickly.</p><p>Imagine:</p><ul><li><p>10,000 users submitting code simultaneously</p></li><li><p>Each execution taking several seconds</p></li><li><p>Some submissions hanging forever</p></li></ul><p>If the API server directly executes code:</p><ul><li><p>Requests block</p></li><li><p>Server threads get exhausted</p></li><li><p>Latency increases</p></li><li><p>Entire application slows down</p></li></ul><p>Instead:</p><ul><li><p>Save submission</p></li><li><p>Push job into queue</p></li><li><p>Return response immediately</p></li></ul><p>This makes the system responsive.</p><p>The user gets:</p><pre><code><code>{
    "submissionId": "123"
}
</code></code></pre><p>And later polls for results.</p><div><hr></div><h2>Step 6 - Polling vs WebSockets</h2><p>In the architecture diagram, the client continuously asks:</p><p><code>GET /submission/:id</code></p><p>This is polling.</p><p>Why polling first?</p><p>Because it is simpler.</p><p>Many beginners immediately jump to WebSockets.</p><p>But system design is about choosing the simplest thing that works.</p><p>Polling works perfectly fine for:</p><ul><li><p>Hackathons</p></li><li><p>MVPs</p></li><li><p>Early-stage systems</p></li></ul><p>Later, if needed, you can move to:</p><ul><li><p>WebSockets</p></li><li><p>Server-Sent Events (SSE)</p></li><li><p>Realtime updates</p></li></ul><p>Start simple.</p><div><hr></div><h2>Step 7 &#8212; The Worker / Orchestrator</h2><p>Now comes the interesting part.</p><p>The worker continuously reads jobs from <strong>SQS</strong>.</p><p>Its responsibilities are:</p><ul><li><p>Fetch submission data</p></li><li><p>Determine programming language</p></li><li><p>Send code to correct runtime</p></li><li><p>Track execution status</p></li><li><p>Handle retries</p></li><li><p>Store final results</p></li></ul><p>Think of the orchestrator as a traffic controller.</p><p>It does not execute code itself.</p><p>It delegates execution to isolated runtimes.</p><div><hr></div><h2>Step 8 &#8212; Code Execution Engine</h2><p>This is the core of the system.</p><p>The execution engine contains isolated runtimes for:</p><ul><li><p>Python</p></li><li><p>JavaScript</p></li><li><p>Java</p></li></ul><p>Each runtime runs inside containers.</p><p>Why containers?</p><p>Because user code cannot be trusted.</p><p>Containers provide isolation.</p><p>This prevents:</p><ul><li><p>File system access</p></li><li><p>Network abuse</p></li><li><p>Infinite resource consumption</p></li><li><p>Attacks on host machine</p></li></ul><div><hr></div><h2>Step 9 - Security Layer</h2><p>This is where most online judge systems become difficult.</p><p>Running user code is dangerous.</p><p>That is why the architecture includes:</p><ul><li><p>Docker isolation</p></li><li><p>No network calls</p></li><li><p>Restricted system access</p></li><li><p>CPU limits Memory limits</p></li></ul><p>For example:</p><ul><li><p>Limit execution time to 2 seconds</p></li><li><p>Limit memory to 256 MB</p></li><li><p>Disable internet access</p></li><li><p>Prevent root access</p></li></ul><p>If a submission exceeds limits:</p><ul><li><p>Kill the container</p></li><li><p>Mark submission as failed</p></li></ul><p>Never trust user code.</p><div><hr></div><h2>Step 10 &#8212; Why Separate Runtimes?</h2><p>Different languages behave differently.</p><p>Python execution is different from Java.</p><p>Java needs:</p><ul><li><p>Compilation step</p></li><li><p>JVM startup</p></li></ul><p>JavaScript behaves differently from compiled languages.</p><p>Separating runtimes gives:</p><ul><li><p>Better isolation</p></li><li><p>Easier debugging</p></li><li><p>Independent scaling</p></li><li><p>Cleaner deployments</p></li></ul><p>You can scale Python workers independently if Python traffic becomes high.</p><div><hr></div><h2>Step 11 &#8212; AWS Fargate for Scaling</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HdW2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HdW2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 424w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 848w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 1272w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HdW2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png" width="802" height="956" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:956,&quot;width&quot;:802,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!HdW2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 424w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 848w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 1272w, https://substackcdn.com/image/fetch/$s_!HdW2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53fdafc7-cc50-4ac7-9e9a-635a176b7cd1_802x956.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The execution engine is deployed on AWS Fargate.</p><p>Why?</p><p>Because execution load is unpredictable.</p><p>Some days:</p><ul><li><p>100 submissions</p></li></ul><p>Some days:</p><ul><li><p>1 million submissions</p></li></ul><p>Fargate helps automatically:</p><ul><li><p>Add containers</p></li><li><p>Remove containers</p></li><li><p>Scale based on load</p></li></ul><p>This prevents idle infrastructure costs.</p><p>You only pay for what you use.</p><div><hr></div><h2>Step 12 &#8212; Database Design Thoughts</h2><p>PostgreSQL is enough initially.</p><p>You do not need Cassandra, DynamoDB, or distributed databases on day one.</p><p>Start simple.</p><p>Possible tables:</p><p><strong>Users:</strong></p><pre><code><code>users
- id
- username
- email
</code></code></pre><p><strong>Problems:</strong></p><pre><code><code>problems
- id
- title
- difficulty
- statement
</code></code></pre><p><strong>Submissions:</strong></p><pre><code><code>submissions
- id
- user_id
- problem_id
- language
- code
- status
- runtime
- memory
</code></code></pre><p>This is enough to start.</p><p>Do not prematurely optimize.</p><div><hr></div><h2>Step 13 &#8212; Where CAP Theorem Actually Matters</h2><p>Now we can finally mention CAP theorem properly.</p><p>In distributed systems, you usually balance between:</p><ul><li><p>Consistency</p></li><li><p>Availability</p></li><li><p>Partition Tolerance</p></li></ul><p>For <strong>LeetCode</strong>:</p><ul><li><p>Availability matters</p></li><li><p>Consistency matters for submissions</p></li><li><p>Partition tolerance matters because systems are distributed</p></li></ul><p>But honestly:</p><p>You do not need deep CAP discussions for an MVP.</p><p>Most beginners force these terms unnecessarily.</p><p>Mention them only when scaling becomes genuinely complex.</p><div><hr></div><p>The biggest mistake beginners make in system design is trying to build Google on day one.</p><p>Good system design is iterative.</p><p>Start with:</p><ul><li><p>Simple API</p></li><li><p>Simple database</p></li><li><p>Queue</p></li><li><p>Worker</p></li><li><p>Isolated execution</p></li></ul><p>Then improve step-by-step.</p><p>The architecture shown here already teaches several real-world concepts:</p><ul><li><p>Async processing</p></li><li><p>Queue-based systems</p></li><li><p>Distributed execution</p></li><li><p>Container isolation</p></li><li><p>Fault tolerance</p></li><li><p>Horizontal scaling</p></li></ul><p>And most importantly:</p><blockquote><p>Always store the submission before processing it.</p></blockquote><p>That single decision saves systems in production more than people realise.</p><p>You can find the complete diagram here:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V4Tk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V4Tk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 424w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 848w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 1272w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V4Tk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png" width="1456" height="485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:485,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!V4Tk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 424w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 848w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 1272w, https://substackcdn.com/image/fetch/$s_!V4Tk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70bb0441-61f6-4389-ac36-eec3f898087e_2172x724.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://adityatrivedi17.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Mastering the Low-Level Design (LLD) of Queues: Push vs. Pull Mechanisms Explained]]></title><description><![CDATA[Asynchronous Architecture]]></description><link>https://adityatrivedi17.substack.com/p/mastering-the-low-level-design-lld</link><guid isPermaLink="false">https://adityatrivedi17.substack.com/p/mastering-the-low-level-design-lld</guid><dc:creator><![CDATA[Aditya Trivedi]]></dc:creator><pubDate>Tue, 02 Jun 2026 17:01:54 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/215f0081-4509-43b7-8083-65b95dc3f02d_1600x840.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Synchronous Architecture</h1><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!F5nt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!F5nt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 424w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 848w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 1272w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!F5nt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png" width="1183" height="442" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:442,&quot;width&quot;:1183,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!F5nt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 424w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 848w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 1272w, https://substackcdn.com/image/fetch/$s_!F5nt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc17d61e0-fc39-400e-8139-30ab3c48bb37_1183x442.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Consider a simple example of a user creating an application which has the following steps:</p><ol><li><p>User sign&#8217;s up</p></li><li><p>Insert User in DB</p></li><li><p>Send a Welcome Email to the User</p></li><li><p>Create Token</p></li><li><p>Return Response</p></li></ol><p>Now this process is synchronous, meaning each step gets executed one after the other.</p><p>This is a perfectly working system for a single user. What if the number of users are now <strong>x1,00,000.</strong> Let us look at the services which are used here:</p><ul><li><p><strong>Authentication APIs:</strong> These APIs are the ones which are built by the user and does not depend on any external dependency.</p></li><li><p><strong>Database:</strong> The database can be hosted by the user on their own server.</p></li><li><p><strong>Email Service:</strong> The email service will be a third party service. This is an external dependency.</p></li></ul><p>Now, what if the email service goes down ? This would result in the signup process to go down as it is dependent on the email service. When <strong>1,00,000 users</strong> would be constantly signing up on the service, this would result in the email service thinking that the &#8220;Users are SPAMMING&#8221; which would result in rate limiting. This is a typical problem which comes with a <strong>Synchronous Architecture</strong>.</p><div><hr></div><h1>Asynchronous Architecture</h1><p><strong>Asynchronous system design</strong> involves components or services interacting in a manner where the sender does <em>not</em> wait (or block) for the receiver&#8217;s response to continue processing. When designing an asynchronous architecture, you must be able do differentiate between which of the services can be <em>synchronous</em> and which can be <em>asynchronous</em>. In our example, the process remains the same till the signup.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_1t4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_1t4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 424w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 848w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 1272w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_1t4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png" width="1456" height="413" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:413,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!_1t4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 424w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 848w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 1272w, https://substackcdn.com/image/fetch/$s_!_1t4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5f2adfbe-d3f7-494f-a277-b9840b4b3c52_1559x442.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When we come to the task of send mail, we add the <strong>sendWelcomeEmail</strong> on to the <strong>Queue.</strong> Remember, <em>we are not sending the mail, just adding the task in the queue</em>. Let us now get into the depth of understanding what is a <strong>Message Queue</strong>.</p><div><hr></div><h1>Message Queue</h1><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bnN3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bnN3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 424w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 848w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 1272w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bnN3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png" width="1456" height="319" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:319,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!bnN3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 424w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 848w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 1272w, https://substackcdn.com/image/fetch/$s_!bnN3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F494b6a26-eca1-466d-84f5-5af265323d1c_1961x429.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The <strong>Message Queue</strong> is a queue just like the queue we stand on when purchasing tickets for a movie where the messages are kept one after the other. It consists of 2 things: <em><strong>Producer</strong></em> <em>&amp;</em> <em><strong>Consumer.</strong></em> Producer is the one who produces messages into the queue and Consumer is the one which consumes the messages produced by the producer.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bQTQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bQTQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 424w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 848w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 1272w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bQTQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png" width="1456" height="267" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:267,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!bQTQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 424w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 848w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 1272w, https://substackcdn.com/image/fetch/$s_!bQTQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9fd0ebb-295c-4dbc-81b1-614efb19660a_3175x582.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now what can be done here is that, we can have our server on post the queue where the <em><strong>Consumer</strong></em> task is to take the message, process it and then send the email. Since the email service (<em>say we are using Amazon SQS</em>) has a rate limiting of 30 emails / min, we can put the same condition on the server. This will now help the server to be available and the emails to be sent one after the other without any additional load on the server. We can even add a condition saying that once 60 minutes are crossed, server can <em>take a break</em> for a minute and then continue with the next set of emails.</p><p>This is called <strong>Asynchronous Processing.</strong></p><p>Till now we completed with something known as (<strong>HLD</strong>) <strong>High Level Design</strong>. It is a high level overview of the approach.</p><h3>Low Level Design of Queues Architecture</h3><p>There are different types of queues here:</p><ol><li><p><strong>Push Based Mechanism:</strong></p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZaC2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZaC2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 424w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 848w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 1272w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZaC2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png" width="1456" height="538" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:538,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ZaC2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 424w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 848w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 1272w, https://substackcdn.com/image/fetch/$s_!ZaC2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc111ee59-09f0-4275-ac50-090bbdbde8cc_2927x1081.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>In a push-based mechanism, the queue takes the responsibility of sending the payload to the processing functions. The consumer functions do not know what is inside the queue until the queue pushes a message to them. <strong><a href="https://www.rabbitmq.com/">RabbitMQ</a></strong> is a classic example of a system that utilises this mechanism**.** This is how it works:</p><ol><li><p><strong>Registeration:</strong> Consumer functions (workers) do not have direct access to pull from queue. Instead, they must interact with the queue&#8217;s API to register themselves, essentially telling the broker, &#8220;I am available for work&#8221;.</p></li><li><p><strong>Broker Management:</strong> The Broker stores this registration in an internal database, mapping worker IDs, and then decides which worker receives which message. It handles balancing across multiple workers.</p></li><li><p><strong>Heartbeats:</strong> To ensure workers are still active, the broker requires a continuous &#8220;heartbeat&#8221;. For example, workers might send an &#8220;I am alive&#8221; signal every 30 seconds. If a worker fails to send a heartbeat within a specific timeframe, the broker marks it as dead and stops sending it messages.</p></li></ol></li><li><p><strong>Pull Based Mechanism:</strong></p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!65ZQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!65ZQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 424w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 848w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 1272w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!65ZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png" width="1456" height="538" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:538,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!65ZQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 424w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 848w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 1272w, https://substackcdn.com/image/fetch/$s_!65ZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46d4884f-b159-4063-b048-fffa11cf05af_2928x1081.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p>In a pull-based mechanism, the broker is more passive. There is no central broker managing worker registrations; instead, the control is entirely in the hands of the consumer. <strong><a href="https://aws.amazon.com/sqs/">AWS SQS (Simple Queue Service)</a></strong> and <a href="https://bullmq.io/">BullMQ</a><strong>Q</strong> are prominent examples of pull-based systems.</p><p><strong>How it Works</strong></p><ol><li><p><strong>Polling:</strong> The workers must actively ask the queue for data. This is typically done using polling mechanisms, such as setting a <code>setInterval</code> or running a continuous <code>while(true)</code> loop to call the queue&#8217;s &#8220;pull&#8221; API.</p></li><li><p><strong>Manual Processing:</strong> The worker pulls a batch of messages, processes them, and then is responsible for explicitly discarding them or putting them back into the queue if they fail.</p></li></ol></li></ol><div><hr></div><h2>The Myth of Strict FIFO</h2><p>A massive misconception in system design is that queues always guarantee a strict First-In-First-Out (FIFO) sequence. While pushing into a queue is sequential, the processing is often not.</p><p>When you introduce parallel processors (multiple workers with different processing speeds), the order breaks down. For instance, Worker A might pick up Message 1, but a faster Worker B might pick up Message 2 and finish processing it before Worker A is done. This can lead to illogical outcomes, like sending a user a product upgrade prompt before sending their welcome email.</p><p><strong>The Solution:</strong> To solve this sequence issue in parallel environments, tools like <strong>Apache Kafka</strong> introduce the concept of <strong>partitions and keys</strong>. Kafka assigns messages with the same key (e.g., the same User ID) to the same specific worker within a <strong>consumer group</strong>. This ensures parallelism across different users while maintaining strict, sequential processing for individual user journeys.</p><div><hr></div><p>When it comes to System Design, everything is a trade-off. Neither Push nor Pull is universally &#8220;better&#8221; or &#8220;worse&#8221;.</p><ul><li><p>Choose <strong>Push-based (RabbitMQ)</strong> if you want a simple architecture with built-in deduplication and routing.</p></li><li><p>Choose <strong>Pull-based (AWS SQS)</strong> if you need granular control over the consumption rate and don&#8217;t mind the engineering overhead of building back-off and lock mechanisms.</p></li><li><p>You can even <strong>mix both</strong> in the same system! For example, using a push queue for immediate notifications and a pull queue for heavy background tasks like video processing.</p></li></ul><p>Understanding these LLD concepts is crucial for mastering asynchronous patterns and succeeding in advanced System Design interviews.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://adityatrivedi17.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Cache Strategies in Distributed Systems]]></title><description><![CDATA[Stop the Thundering Herd! Explore Jitter, Probabilistic Re-computation, SWR, and Cache Warming to keep your high-traffic systems stable during Netflix-scale spikes.]]></description><link>https://adityatrivedi17.substack.com/p/cache-strategies-in-distributed-systems</link><guid isPermaLink="false">https://adityatrivedi17.substack.com/p/cache-strategies-in-distributed-systems</guid><dc:creator><![CDATA[Aditya Trivedi]]></dc:creator><pubDate>Tue, 02 Jun 2026 16:15:28 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/aa9f1ae7-b65c-4f23-8f18-c560503127b7_1600x840.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Stop the Thundering Herd! Explore Jitter, Probabilistic Re-computation, SWR, and Cache Warming to keep your high-traffic systems stable during Netflix-scale spikes.</p><div><hr></div><h2>Why Basic TTL caching Fails?</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!y1Lm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!y1Lm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!y1Lm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!y1Lm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!y1Lm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3c38056-1367-4f99-972b-e4b65d3501fa_1024x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Basic TTL (Time to Live) is like a ticking time bomb. If you set 1 million keys to expire in exactly 3600 seconds, you are essentially scheduling a system crash for 1 hour from now. Imagine a &#8220;Flash Sale&#8221; on an e-commerce site:</p><ol><li><p>You cache the &#8220;Product Price&#8221; for 60 seconds.</p></li><li><p>50,000 people are looking at that price.</p></li><li><p>At the 60th second, the cache disappears.</p></li><li><p><strong>All 50,000 users</strong> see a &#8220;Cache Miss&#8221; and hit the Database at the exact same millisecond.</p></li></ol><div><hr></div><h1>The Roadmap to a Bulletproof Cache:</h1><h2>Step 1: Advanced Expiry (Jitter &amp; PER)</h2><h3>TTL Jitter: The Power of Randomness</h3><p>Instead of <code>TTL = 60s</code> , we use <code>TTL = 60s + random(0,10s)</code>. Now, some user&#8217;s cache expire at 60s, some at 63s, some at 70s. This turns a &#8220;Thundering Herd&#8221; into a &#8220;Predictable Stream&#8221;. It&#8217;s like a staggered release times for a crowded theatre.</p><h3>Probabilistic Early Re-computation (PER)</h3><p>This sounds fancy, but the logic is simple: The close we get to the expiration time, the higher the &#8220;chance&#8221; that a request will decide to refresh the cache early.</p><ul><li><p>At 10 seconds left, there is a 1% chance a request will refresh it.</p></li><li><p>At 1 second left, there&#8217;s an 80% chance.</p></li></ul><p>This ensures that one <em>lucky</em> user refreshes the data before it every actually hits zero and disappears</p><h2>Step 2: The &#8220;Wait or Serve Old&#8221; Strategies</h2><h3>1. Mutex / Cache Locking (The &#8220;Old Producer&#8221; Rule)</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D1k0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D1k0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D1k0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!D1k0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!D1k0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0cb5f0b7-12c4-4152-b021-a5914eaf84e1_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Imagine the cache for &#8220;Current Score&#8221; expires, 100 requests arrive.</p><ul><li><p>Without a lock: All 100 go to the DB.</p></li><li><p><strong>With a Mutex:</strong> The request &#8220;locks&#8221; the key. It says, &#8220;I&#8217;m, going to the DB to get the new score. Everyone else, stay here and wait&#8221;.</p></li><li><p>The other 99 requests, &#8220;wait&#8221; for a few milliseconds, until the first one returns and updates the cache.</p></li></ul><h3>2. Stale-While-Revalidate (SWR)</h3><p>This is the &#8220;Netflix/CDN favourite.&#8221; Instead of making users wait for the new data, you give them the <strong>old (stale) data</strong> one last time while you fetch the new data in the background.</p><ul><li><p><strong>Request arrives:</strong> &#8220;Is the data expired?&#8221;</p></li><li><p><strong>System:</strong> &#8220;Yes, it expired 2 seconds ago. But here is the old version so you don&#8217;t have to wait. I&#8217;ll go grab the new one now for the <em>next</em> person.&#8221;</p></li><li><p><strong>Result:</strong> Zero latency for the user!</p></li></ul><h2>Step 3: Proactive Measures (&#8221;The Chef&#8217;s Preparation&#8221;)</h2><h3>Cache Warming / Pre-Warming</h3><p>Think of an <strong>IPL final</strong> or a <strong>Netflix release</strong>. You <em>know</em> a million people are going to ask for the &#8220;Match Summary&#8221; or &#8220;Stranger Things Episode 1&#8221; at exactly 8:00 PM. <strong>Don&#8217;t wait for them to ask!</strong> Your system should &#8220;warm up&#8221; the cache by fetching that data and putting it in Redis at 7:55 PM. By the time the crowd arrives, the &#8220;oven&#8221; is already hot.</p><div><hr></div><h2>The Big Tradeoffs: Freshness vs. Latency</h2><p>In engineering, there is no free lunch.</p><ul><li><p><strong>SWR:</strong> Amazing speed (low latency), but some users see &#8220;stale&#8221; (old) data for a few seconds.</p></li><li><p><strong>Mutex:</strong> Perfect data (high consistency), but some users have to &#8220;wait&#8221; (higher latency) while the lock is held.</p></li></ul><div><hr></div><h3>Choosing Your Weapon</h3><ul><li><p><strong>Use Jitter ALWAYS:</strong> It&#8217;s the &#8220;low-hanging fruit.&#8221; There is almost no reason <em>not</em> to add a little randomness to your expiries to prevent synchronized spikes.</p></li><li><p><strong>Use SWR for UX:</strong> If your app feels &#8220;snappy,&#8221; users stay longer. If a user sees a post from 10 seconds ago instead of 1 second ago, they usually won&#8217;t notice. This is why <strong>CDNs</strong> (like Cloudflare) love SWR.</p></li><li><p><strong>Use Cache Warming for &#8220;The Big Event&#8221;:</strong> If you know 10 million people are coming at 8:00 PM, waiting for the first user to &#8220;trigger&#8221; the cache is a mistake. You want that data waiting for <em>them</em>.</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://adityatrivedi17.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[The Thundering Herd Problem: Explained]]></title><description><![CDATA[Imagine you&#8217;re standing outside Apple store on the release of it&#8217;s new store.]]></description><link>https://adityatrivedi17.substack.com/p/the-thundering-herd-problem-explained</link><guid isPermaLink="false">https://adityatrivedi17.substack.com/p/the-thundering-herd-problem-explained</guid><dc:creator><![CDATA[Aditya Trivedi]]></dc:creator><pubDate>Tue, 02 Jun 2026 16:02:59 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/1bcbaed3-56c7-4214-913e-fbcb7a0ffcdc_1600x840.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Imagine you&#8217;re standing outside Apple store on the release of it&#8217;s new store. The doors are locked, and 1,000 people are huddled outside, all waiting for the same new IPhone 18 Pro Max. The moment those doors swing open, everyone rushes it at once, trampling the displays and overwhelming the 2 poor security guards at the front.</p><p>That&#8217;s the <strong>Thundering Herd</strong> problem: a massive, synchronized surge of requests that crashes a system because everyone tried to access the same resource at the exact same moment.</p><div><hr></div><h1>What is the Thundering Herd Problem ?</h1><p>In technical terms, it happens when a large number of processes or clients are waiting for an event (like a lock being released, or a cache being updated). When that event finally happens, <strong>all of them</strong> wake up and try to handle it simultaneously.</p><p>Instead of one person &#8220;fixing&#8221; the problem, everyone tries to fix it, which ends up breaking the system entirely.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yE8U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yE8U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 424w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 848w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 1272w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yE8U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png" width="1456" height="267" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:267,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!yE8U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 424w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 848w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 1272w, https://substackcdn.com/image/fetch/$s_!yE8U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd090d6b9-b666-4e37-8c79-2f94457576a2_2108x387.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>Think of a standard web app setup:</p><ol><li><p><strong>The Goldilocks Zone:</strong> Usually, the App asks the Cache for data. The Cache has it, and the DB stays &#8220;chilling&#8221; and relaxed.</p></li><li><p><strong>The Expiry:</strong> The data in the Cache has a &#8220;Time to Live (TTL)&#8221;. Eventually, it expires.</p></li><li><p><strong>The Stampede:</strong> If 10,000 users ask for that data, the <em>millisecond</em> after it expires, the Cache says &#8220;I don&#8217;t have it!&#8221; to all 10,000 users. All 10,000 requests then &#8220;thunder&#8221; straight to your database to fetch the data.</p></li></ol><div><hr></div><h2>Normal Spikes v/s Thundering Herds</h2><p>Before moving forward with the actual system design, let us first go with the fundamentals of understanding the difference between normal spikes and thundering herds.</p><p>A <strong>Normal Spike</strong> is like a gold rush hour. You know more people are coming because it&#8217;s 5:00 PM. You can prepare by adding more lanes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OtUw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OtUw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OtUw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!OtUw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!OtUw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138cf292-904b-4a54-833a-eb3324a20d10_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A <strong>Thundering Herd</strong> is a &#8220;system-induced&#8221; spike. It&#8217;s often caused by your owned code!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JuYP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JuYP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JuYP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!JuYP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!JuYP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f15cc7d-845a-4de2-bc44-6d2e2b56ee0e_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For example:</p><ul><li><p><strong>The IPL</strong> <strong>Example:</strong> A million people hit &#8220;Refresh&#8221; at the exact moment a match starts.</p></li><li><p><strong>The Cache Expiry:</strong> You set your cache to expire every 10 minutes. At exactly 10:01, the system itself creates a massive hole that everyone falls into</p></li></ul><p>In distributed systems, if one database gets crushed, or goes down, the &#8220;HERD&#8221; doesn&#8217;t go away - it just moves to the next available server, knocking it down too. This is called <strong>Cascading Failure</strong>.</p><p><strong>Quick Scenario Check:</strong> Imagine you run a site for concert tickets.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZQSx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZQSx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZQSx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ZQSx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ZQSx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa258613b-0189-4663-a544-ce9697f7a10e_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ul><li><p><strong>Scenario A:</strong> Users slowly trickle in over an hour.</p></li><li><p><strong>Scenario B:</strong> You send a &#8220;Buy Now&#8221; push notification to 1 million users at once, and your cache for the &#8220;Ticket Price&#8221; expires at that exact second.</p></li></ul><p>Consider another example:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PG0N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PG0N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PG0N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!PG0N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!PG0N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e45fce-6c4b-41fd-bf76-29afa102a5d0_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Imagine you are the Lead Engineer for <strong>Netflix</strong>. A massive new season of <em>Stranger Things</em> is about to drop at exactly midnight. You have two situations:</p><ol><li><p><strong>Situation A:</strong> You have a &#8220;Coming Soon&#8221; page that updates every minute. At 11:59:59, the cache for that page expires for <strong>everyone</strong> at the same time.</p></li><li><p><strong>Situation B:</strong> People are already on the site, browsing different categories (Horror, Comedy, Sci-Fi) throughout the evening.</p></li></ol><p>Which will be a <strong>Thundering Herd Situation</strong> ? Obviously the situation A. The &#8220;event&#8221; that triggered the stampede was the <strong>Cache Expiry (TTL hitting zero)</strong> at 11:59:59. Because every single user was looking at the same page, and that page disappeared from the cache for everyone at once, they all rushed the database together.</p><div><hr></div><h2>The System Heart Attack</h2><p>When that herd hits, it&#8217;s not just &#8220;slow&#8221; - it can be fatal for your infrastructure. Here&#8217;s what happens under the hood:</p><ul><li><p><strong>Database (DB):</strong> Imagine a librarian who can handle 10 people at a time. Suddenly, 10,000 people scream the same question at her. She trips, her glasses break, and she stops answering everyone. Similarly, the DB connection pool fills up, and it stops responding.</p></li><li><p><strong>CPU:</strong> Your servers are sweating. They spend all their energy just trying to manage the &#8220;waiting line&#8221; (context switching) rather than actually fetching data.</p></li><li><p><strong>Cache:</strong> Ironically, the cache stays empty as the DB is too busy to send it the update!</p></li><li><p><strong>Latency:</strong> This is the &#8220;spinning wheel of death&#8221; for users. Requests that usually take 50ms now take 30 seconds... or just time out.</p></li></ul><div><hr></div><h2>Why it&#8217;s Dangerous in Distributed Systems</h2><p>In a big system like Netflix, services depend on each other. If the &#8220;Movie Service&#8221;, dies due Thundering Herd, the &#8220;Recommendation Service&#8221; might start failing too because it&#8217;s waiting for the Movie Service. This is a <strong>Cascading Failure -</strong> one fallen domino knocks over the whole data centre.</p><div><hr></div><h2>The Fixes (How to Tame the Herd)</h2><p>There are 3 main &#8220;weapons&#8221; we use here:</p><h3><strong>Request Coalescing (The &#8220;Group Order&#8221;)</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3rb8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3rb8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3rb8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!3rb8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!3rb8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e0cc46b-eefd-45c7-93e3-ddc74a04da6e_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Imagine 100 people want a coffee. Instead of the barista making 100 individual trips to the kitchen for beans, the first person says, &#8220;Wait! Everyone wants the same thing, I&#8217;ll go get the beans, and rest of you just wait here. &#8220;</p><p>The system only sends <strong>one request</strong> to the database. When the data comes back, it shares with all waiting user.</p><h3>Cache Locking / Mutex</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SXMQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SXMQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SXMQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!SXMQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!SXMQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306a74d9-8033-473c-8ee2-1f4edd88f75b_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When the cache expires, the first request to arrive &#8220;grabs a lock&#8221;. It tells the system, &#8220;I am currently updating the cache! Nobody else go to the Database.&#8221; Other requests see the lock and either wait or server slightly &#8220;stale&#8221; (old) data for a few more seconds.</p><h3><strong>Staggered Expiry (Adding &#8220;Jitter&#8221;)</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Pnp4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Pnp4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Pnp4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/de0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Pnp4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Pnp4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde0d37ec-b3be-4dd5-8e50-838e17e39034_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is a clever one. Instead of setting every cache to expire in exactly 600 seconds, you add a bit of randomness:</p><ul><li><p>User A&#8217;s cache expires in 600s.</p></li><li><p>User B&#8217;s cache expires in 605s.</p></li><li><p>User C&#8217;s cache expires in 597s.</p></li></ul><p>This spreads the &#8220;thunder&#8221; out over several seconds so it sounds more like a gentle rain.</p><h3>Exponential Backoff</h3><p>If a request fails (because the herd already knocked the server down), don&#8217;t let the client try again immediately! If 1,000,000 apps all retry at the exact same second, they just knock the server down again.</p><p><strong>The Fix:</strong> Each time a request fails, the client waits a bit longer before trying again (1s, then 2s, then 4s, then 8s...). It gives the &#8220;Librarian&#8221; time to stand back up.</p><div><hr></div><p>In system design, we can&#8217;t always stop the crowd from showing up, but by using tools like jitter and coalescing, we make sure our servers aren&#8217;t the ones getting trampled. Remember: a healthy system doesn&#8217;t just handle traffic; it survives the thunder.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://adityatrivedi17.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[The System Design of UPI: How India's Instant Payment Revolution Works ?]]></title><description><![CDATA[The way money is transferred in India has fundamentally changed over the last decade.]]></description><link>https://adityatrivedi17.substack.com/p/the-system-design-of-upi-how-indias</link><guid isPermaLink="false">https://adityatrivedi17.substack.com/p/the-system-design-of-upi-how-indias</guid><dc:creator><![CDATA[Aditya Trivedi]]></dc:creator><pubDate>Tue, 02 Jun 2026 15:15:56 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/dcaa6164-b651-4fb4-85c7-427d7ecad0ee_1600x840.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The way money is transferred in India has fundamentally changed over the last decade. From complex forms and waiting hours for transfers, we&#8217;ve moved to instant, seamless transactions powered by a revolutionary system: the <strong>Unified Payments Interface (UPI)</strong>.</p><p>Understanding how UPI works requires first looking back at the traditional infrastructure it replaced.</p><div><hr></div><h2>Part 1: The Traditional Approach to Payments in India</h2><p>Before UPI, the Indian payment system was heavily <strong>bank-centric</strong> and relied on multiple layers of detail and processing time.</p><h3>The Problem: Complexity and Latency</h3><p>In the traditional system, every transaction was rooted in the bank accounts of the sender and receiver. Users needed to possess a bank account with any regulated financial institution (like HDFC, ICICI, SBI, HSBC, etc.) to participate.</p><p>The key regulatory body overseeing all these banks is the <strong>RBI (Reserve Bank of India)</strong>, as illustrated in the flow below:</p><p>To make a payment, the sender required a multitude of unique details from the receiver:</p><ul><li><p>Account Number</p></li><li><p>Bank Name</p></li><li><p>Branch Code</p></li><li><p>IFSC Code (Indian Financial System Code)</p></li></ul><h3>The Old Transfer Methods</h3><p>Depending on the transaction amount and urgency, different services were used, often introducing delays:</p><p>IMPS - Immediate Payment Service</p><p>Small Transaction Range like &#8377;1,000 - &#8377;2,000 with Real-time processing</p><p>NEFT - National Electronic Funds Transfer</p><p>Medium Transaction Range like 50,000 - &#8377;1,00,000 with Batch Processing (Can take hours)</p><p>RTGS - Real-time Gross Settlement</p><p>High Transaction Range like 5,00,000 and above with Real-time processing</p><p>The major drawback, particularly with NEFT, was the latency. Users often had to take screenshots of their payment confirmations to prove a payment was initiated, even if the amount hadn&#8217;t yet reflected in the receiver&#8217;s account. The system lacked true, consistent, real-time settlement across all tiers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UwG6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UwG6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 424w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 848w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 1272w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UwG6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png" width="995" height="474" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:474,&quot;width&quot;:995,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UwG6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 424w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 848w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 1272w, https://substackcdn.com/image/fetch/$s_!UwG6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5055c3ea-3021-402c-9da3-2100e5dfb062_995x474.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h2>Part 2: How UPI Revolutionized Payments</h2><p>The <strong>Unified Payments Interface (UPI)</strong> was designed to solve the complexity and latency issues of the traditional model. Developed by the <strong>NPCI (National Payments Corporation of India)</strong>, UPI is an instant, real-time payment system that acts as a simplifying layer above the existing bank infrastructure.</p><p>The core idea was to say: &#8220;Let&#8217;s keep the banks for holding the money, but simplify the process of moving it.&#8221;</p><h3>The Key Players in the UPI System Design</h3><h4>1. NPCI (National Payments Corporation of India)</h4><p>NPCI acts as the <strong>Payment Infrastructure</strong> and the central switch. It is the core hub that connects all participating banks. Critically, the APIs provided by NPCI are <strong>not public</strong>&#8212;only a list of trusted, regulated banks (known as <em>Partner Banks</em>) can communicate with the NPCI network.</p><h4>2. PSP (Payment Service Provider)</h4><p>Since the public cannot directly communicate with NPCI, the <strong>PSP</strong> steps in. A PSP is a third-party entity that provides the user-facing application (the front-end) to facilitate electronic payment transactions.</p><ul><li><p><strong>Customer PSPs:</strong> GooglePay, PhonePe, Paytm, etc.</p></li><li><p>The PSPs have direct tie-ups with the Partner Banks to route the transaction requests.</p></li></ul><h4>3. VPA (Virtual Payment Address)</h4><p>This is the game-changer. The <strong>Virtual Payment Address</strong> is a unique identifier that abstracts (hides) all the complex bank details (Account No., IFSC, etc.) of the user. It works like an email address for money transfers, typically in the format: username@handle (e.g., aditya@hdfc, anil@icici).</p><div><hr></div><h2>Part 3: The Step-by-Step UPI Transaction Flow</h2><p>The system design of a UPI transaction uses the PSP and VPA to facilitate a rapid request-response cycle coordinated by NPCI.</p><h3>UPI Transaction Flow Diagram</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VVXT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VVXT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VVXT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!VVXT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VVXT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55d5fa33-0637-4f56-8ac6-0f274039da6e_1024x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s walk through the transaction: <strong>Aditya (@hdfc) wants to pay Anil (@icici) &#8377;5,000.</strong></p><p><strong>Step 1: User Initiates Payment via PSP</strong><br>Aditya opens a Customer PSP App (e.g., GooglePay) and scans Anil&#8217;s QR code, which contains Anil&#8217;s VPA (anil@icici). The user interface creates an <strong>Intent</strong> for the transaction:</p><ul><li><p><strong>From:</strong> aditya@hdfc</p></li><li><p><strong>To:</strong> anil@icici</p></li><li><p><strong>Amount:</strong> &#8377;5,000</p></li></ul><p><strong>Step 2: PSP Sends Request to Sender&#8217;s Bank</strong><br>The PSP App does not talk to NPCI directly. Instead, it communicates the payment Intent to the <strong>Sender&#8217;s Bank (HDFC)</strong>, with which the PSP has a tie-up.</p><p><strong>Step 3: Sender&#8217;s Bank Communicates with NPCI</strong><br>HDFC Bank receives the request and forwards it to the central <strong>NPCI</strong> network, asking for permission to debit Aditya&#8217;s account.</p><p><strong>Step 4: NPCI Coordinates Debit and Credit</strong></p><ul><li><p>NPCI first verifies the existence of the VPAs and checks the balance in the sender&#8217;s account. This is the stage where the user is prompted to enter their secure UPI PIN.</p></li><li><p>Upon successful PIN verification, NPCI instructs the Sender&#8217;s Bank (HDFC) to <strong>debit</strong> the amount from Aditya&#8217;s account.</p></li><li><p>Simultaneously, NPCI sends a <strong>Credit Request</strong> to the <strong>Receiver&#8217;s Bank (ICICI)</strong>, asking it to credit the amount to Anil&#8217;s account associated with the VPA.</p><ul><li><p><em><strong>Note</strong>:</em> If the receiver&#8217;s bank server is temporarily overloaded or busy, the transaction may fail at this point, resulting in a &#8220;Receiver&#8217;s server is busy&#8221; notification.</p></li></ul></li></ul><p><strong>Step 5: Transaction Completion and Notification</strong><br>Once the Receiver&#8217;s Bank confirms the credit, NPCI confirms the transaction. A success notification is instantly relayed back to both the sender and the receiver via their respective PSP apps.</p><div><hr></div><h2>Conclusion: The Power of Abstraction</h2><p>UPI&#8217;s brilliance lies in its system design, which successfully <strong>abstracts</strong> away the complexity of traditional banking details using the <strong>VPA</strong> and uses <strong>NPCI</strong> as a highly efficient, real-time central switch.</p><p>By leveraging a trusted third-party layer (PSPs) and a secure, central network (NPCI), the UPI system turned a multi-step, multi-hour process into a single, instant interaction, solidifying its place as one of the world&#8217;s most successful real-time payment systems.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://adityatrivedi17.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item></channel></rss>