<?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[The Scalable Thread]]></title><description><![CDATA[One well-researched system design article simplified like you're five, every two weeks!]]></description><link>https://newsletter.scalablethread.com</link><image><url>https://substackcdn.com/image/fetch/$s_!z4Fy!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f603da0-6f33-4530-b245-903d8703621e_550x550.png</url><title>The Scalable Thread</title><link>https://newsletter.scalablethread.com</link></image><generator>Substack</generator><lastBuildDate>Wed, 22 Apr 2026 09:14:56 GMT</lastBuildDate><atom:link href="https://newsletter.scalablethread.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Sid]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[scalablethread@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[scalablethread@substack.com]]></itunes:email><itunes:name><![CDATA[Sid]]></itunes:name></itunes:owner><itunes:author><![CDATA[Sid]]></itunes:author><googleplay:owner><![CDATA[scalablethread@substack.com]]></googleplay:owner><googleplay:email><![CDATA[scalablethread@substack.com]]></googleplay:email><googleplay:author><![CDATA[Sid]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Why are Event-Driven Systems Hard?]]></title><description><![CDATA[Understanding the Core Challenges of Asynchronous Architectures]]></description><link>https://newsletter.scalablethread.com/p/why-event-driven-systems-are-hard</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/why-event-driven-systems-are-hard</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Sun, 14 Sep 2025 16:56:31 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/8f23e44c-2a3c-416c-82ed-55a143001014_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>An <em>event</em> is just a small message that says, "Hey, something happened!" For example, <code>UserClickedButton</code>, <code>PaymentProcessed</code>, or <code>NewOrderPlaced</code>. Services subscribe to the events they care about and react accordingly. This event-driven approach makes systems resilient and flexible. However, building and managing these systems at a large scale is surprisingly hard.</p><h2>Managing the Message Format Versions</h2><p>Imagine you and your friend have a secret code to pass notes. One day, you decide to add a new symbol to the code to mean something new. If you start using it without telling your friend, your new notes will confuse them. This is exactly what happens in event-driven systems.</p><p>For example, an <code>OrderPlaced</code> event might look like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H04Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H04Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 424w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 848w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 1272w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H04Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png" width="728" height="283.38255033557044" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:174,&quot;width&quot;:447,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:15682,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/173543404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05af6a13-95b9-49c4-8bcc-e33a4dcb2e68_447x191.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H04Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 424w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 848w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 1272w, https://substackcdn.com/image/fetch/$s_!H04Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4178cf-ceb4-41b5-984a-39babd83c5d5_447x174.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>Now imagine another service reads this event to send a confirmation email. Then, six months later, you add a new field: <code>shippingAddress</code>. You update the producer. The event becomes:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rBfd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rBfd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 424w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 848w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 1272w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rBfd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png" width="716" height="291.8151260504202" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:194,&quot;width&quot;:476,&quot;resizeWidth&quot;:716,&quot;bytes&quot;:15707,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/173543404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rBfd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 424w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 848w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 1272w, https://substackcdn.com/image/fetch/$s_!rBfd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee987fa9-a617-4f5a-83dc-285b7cc3abbe_476x194.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>The problem is that other services, like the <code>OrderConfirmationEmailService</code>, might still be expecting the old version 1 format. When they receive this new message, they won't know what to do with the <code>shippingAddress </code>field. Worse, if a field they relied on was removed, they would simply crash.</p><p>This forces teams to carefully manage how schemas evolve. Common strategies include:</p><ul><li><p><strong>Backward Compatibility:</strong> New schemas can be read by services expecting the old schema. This usually means you can only add new, optional fields. You can't rename or remove existing ones.</p></li><li><p><strong>Forward Compatibility:</strong> Services expecting a new schema can still read messages written in an old one. This is harder to achieve and often requires setting default values for missing fields.</p></li><li><p><strong>Schema Registry:</strong> This is like a central dictionary for all your event "secret codes." Before a service sends a message, it checks with the registry to make sure the format is valid and compatible. It prevents services from sending out "confusing notes."</p></li></ul><p>Without strict rules for changing message formats, a simple update can cause a cascade of failures throughout a large system.</p><h2>Observability and Debugging</h2><p>In a traditional, non-event-driven system, when a user clicks a button, one piece of code calls another, which calls another, in a straight line. If something breaks, you can look at the error log and see the entire sequence of calls, like following a single piece of string from start to finish.</p><p>In an event-driven system, that single string is cut into dozens of tiny pieces. The <code>OrderService</code> publishes an <code>OrderPlaced</code> event. The <code>PaymentService</code>, <code>ShippingService</code>, and <code>NotificationService</code> all pick it up and do their own work independently. They might, in turn, publish their own events.</p><p>Now, imagine a customer calls saying they placed an order but never got a confirmation email. Where did it go wrong?</p><ul><li><p>Did the <code>OrderService</code> fail to publish the event?</p></li><li><p>Did the <code>NotificationService</code> not receive it?</p></li><li><p>Did it receive the event but fail to connect to the email server?</p></li></ul><p>Debugging this can be difficult as you can't see the whole picture at once.</p><p>To solve this, we use <strong>distributed tracing</strong>. When the very first event is created, we attach a unique ID to it, called a <strong>Correlation ID</strong>. Every service that processes this event or creates a new event as a result must copy that same ID onto its own work.</p><p>When you need to investigate a problem, you can search for this one correlation ID across all the logs of all your services. This allows you to stitch the story back together and see the journey of that single request across the entire distributed system.</p><h2>Handling Failures and Message Loss</h2><p>Events can disappear. Not because of bugs &#8212; because of infrastructure issues like network failure, a service crashing, or the message broker itself having a problem.</p><p>The core promise of many event systems is <a href="https://newsletter.scalablethread.com/i/146810780/at-least-once-guarantee">at-least-once delivery</a>. This means the system will do everything it can to make sure your event gets delivered. If a service that is supposed to receive an event is temporarily down, the message broker will hold onto the message and try again later.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sGmD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sGmD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 424w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 848w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 1272w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sGmD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png" width="1456" height="1338" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1338,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:552118,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/173543404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sGmD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 424w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 848w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.png 1272w, https://substackcdn.com/image/fetch/$s_!sGmD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc173afd2-6a2e-414d-a7ed-8af50160ab18_3183x2926.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>But what if a service has a persistent bug and crashes every time it tries to process a specific message? The broker will keep trying to redeliver it, and the service will keep crashing, until the broker retry limit is reached. To handle this, we use a <a href="https://newsletter.scalablethread.com/i/152780978/using-dead-letter-queues">Dead-Letter Queue (DLQ)</a>. After a few failed delivery attempts, the message broker moves the crash causing message to the DLQ. This stops the cycle of crashing and allows the service to continue processing other, valid messages. Engineers can then inspect the DLQ later to debug the problematic message.</p><h2>Idempotency</h2><p>The guarantee of "at-least-once delivery" creates a new, tricky problem: what if a message is delivered more than once? This can happen if a service processes an event but crashes before it can tell the message broker, "I'm done!" The broker, thinking the message was never handled, will deliver it again when the service restarts.</p><p>If the event was <code>IncreaseItemCountInCart</code>, receiving it twice is a big problem. The customer who wanted one item now has two in their cart. If it was <code>ChargeCreditCard</code>, they get charged twice.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BlHa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BlHa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 424w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 848w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BlHa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png" width="1456" height="520" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:520,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:250097,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/173543404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BlHa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 424w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 848w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!BlHa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7318f031-5ce1-43d4-870a-dcd0a94748ec_3038x1084.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>To prevent this, services must be <a href="https://newsletter.scalablethread.com/p/how-to-build-idempotent-apis">idempotent</a>. We can achieve idempotency by having the service keep a record of the event IDs it has already processed. When a new event comes in, the service first checks its records.</p><ol><li><p>Has it seen this event ID before?</p></li><li><p>If yes, it simply ignores the duplicate and tells the broker, "Yep, I'm done."</p></li><li><p>If no, it processes the event and then saves the event ID to its records before telling the broker it's done.</p></li></ol><p>This ensures that even if a message is delivered 100 times, the action is only performed once.</p><h2>Eventual Consistency</h2><p>In a simple application with one database, when you write data, it's there instantly. If you change your shipping address, the very next screen you load will show the new address. This is called <a href="https://newsletter.scalablethread.com/i/146489166/strict-consistency-model">strong consistency</a>.</p><p>Event-driven systems give up this guarantee for the sake of scalability and resilience. They operate on a model of <a href="https://newsletter.scalablethread.com/i/146489166/eventual-consistency-model">eventual consistency</a>. For example, when a user updates their address, the <code>CustomerService</code> updates its own database and publishes an <code>AddressUpdated</code> event. The <code>ShippingService</code> and <code>BillingService</code> subscribe to this event. But it might take a few hundred milliseconds for them to receive the event and update their own data (This example is to provide some context, but ideally, the address should be stored at one place and the id of that record should be passed around in the events).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gE2i!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gE2i!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 424w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 848w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 1272w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gE2i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png" width="1456" height="1087" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1087,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:211419,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/173543404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gE2i!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 424w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 848w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.png 1272w, https://substackcdn.com/image/fetch/$s_!gE2i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67ec02ee-bcdf-4832-8ccc-0adfa1aec02b_2812x2100.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>Designing for eventual consistency means the system must be built to handle this temporary state of disagreement. This might involve:</p><ul><li><p>Designing user interfaces that account for the delay.</p></li><li><p>Adding logic to services to double-check critical data if needed.</p></li><li><p>Accepting that for some non-critical data, a small delay is acceptable.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[Why "What Happened First?" Is One of the Hardest Questions in Large-Scale Systems ]]></title><description><![CDATA[Understanding Why Exact Ordering of Events is Hard in Large Scale Systems]]></description><link>https://newsletter.scalablethread.com/p/why-what-happened-first-is-one-of</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/why-what-happened-first-is-one-of</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Sat, 30 Aug 2025 21:46:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b80b6ead-2385-4225-ba76-f76370efac06_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>The Problem with Time</h2><p>Imagine you and your friend are in different cities. You both look at your watches and agree to clap at exactly 1:00 PM. You clap, but did you both clap at the <em>exact same instant</em>? Probably not. Your watch might be a few seconds faster than your friend's, or maybe it runs a tiny bit slower.</p><p>Computers in a large system face the same problem, but on a massive scale. A system like FAANG&#8217;s runs on thousands of computers spread across the globe. Each computer has its own internal clock, just like you have your own watch. These clocks are never perfectly in sync.</p><p>This creates a big problem: if two events happen on two different computers, it's hard to say with 100% certainty which one happened first just by looking at their timestamps.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BGXU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BGXU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 424w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 848w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 1272w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BGXU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png" width="1456" height="999" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:999,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:251901,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163728719?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BGXU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 424w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 848w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.png 1272w, https://substackcdn.com/image/fetch/$s_!BGXU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55983bfe-6e59-45c2-bf38-6d3e0979143d_2073x1423.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><h2>Why Clock Synchronization is Hard</h2><p>The obvious solution seems to be to just synchronize all the computer clocks. We have protocols like the <strong>Network Time Protocol (NTP)</strong> that try to do this by connecting to ultra-precise atomic clocks. However, even with NTP, achieving perfect synchronization is impossible for two main reasons:</p><ol><li><p><strong>Network Latency:</strong> Information doesn't travel instantly. When a computer asks a central time server, "What time is it?", the question takes time to travel to the server, and the answer takes time to travel back. This delay, called <strong>latency</strong>, is unpredictable. It can change based on network traffic, distance, and other factors. It's like asking your friend for the time over a walkie-talkie with a random delay. You can't be sure if the time you hear is still accurate.</p></li><li><p><strong>Clock Drift:</strong> No two clocks are built exactly the same. The quartz crystals that regulate time in computers vibrate at slightly different frequencies. This causes some clocks to run a little faster and others a little slower. Over time, these small differences add up, and the clocks "drift" apart. Even if you sync them perfectly, they will start to disagree again almost immediately.</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_!GehS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GehS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 424w, https://substackcdn.com/image/fetch/$s_!GehS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 848w, https://substackcdn.com/image/fetch/$s_!GehS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 1272w, https://substackcdn.com/image/fetch/$s_!GehS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GehS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png" width="1456" height="559" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:559,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:70230,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163728719?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GehS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 424w, https://substackcdn.com/image/fetch/$s_!GehS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 848w, https://substackcdn.com/image/fetch/$s_!GehS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 1272w, https://substackcdn.com/image/fetch/$s_!GehS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae4bb5e7-805d-482a-aa73-08ed4f43410e_1582x607.png 1456w" sizes="100vw"></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>Because of these issues, we have to accept that we can never know the <em>exact</em> physical time across a distributed system. The time on one machine might be a few milliseconds ahead of another, and in high-speed large-scale systems, a millisecond is a very long time.</p><h2>A Workaround: Logical Clocks</h2><p>If we can't rely on wall-clock time, how do we solve the ordering problem? Computer scientists realized that we don't always need to know the <em>exact time</em> an event happened. Often, it's enough to know the <strong>causal order</strong>&#8212;that is, if one event caused another.</p><p>This is called the <strong>happened-before</strong> relationship. It's simple:</p><ul><li><p>If event A happens on a computer before event B on the same computer, then A <em>happened before</em> B.</p></li><li><p>If event A is the sending of a message and event D is the receiving of that same message, then A <em>happened before</em> D.</p></li><li><p>If A happened before B, and B happened before C, then A happened before C.</p></li></ul><p>Events that don't have a happened-before relationship are called <strong>concurrent</strong>. We can't say which one happened first, and it might not matter. For example, if you send an email to Alice and your friend sends an email to Bob, these events are concurrent.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y1uq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y1uq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 424w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 848w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 1272w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y1uq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png" width="1456" height="938" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:938,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:151823,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163728719?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y1uq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 424w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 848w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.png 1272w, https://substackcdn.com/image/fetch/$s_!Y1uq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9780b7ea-54e1-4b4f-b444-8010ccb80e79_2137x1376.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><h4>Logical Clocks</h4><p>To track this "happened-before" relationship, we can use <strong>logical clocks. </strong>Imagine each computer in the system has a counter, like a ticket dispenser at a bakery.</p><ol><li><p>Every time an event happens on a computer, it increments its own counter and assigns that number to the event. (e.g., <code>counter = counter + 1</code>).</p></li><li><p>When a computer sends a message, it includes its current counter value in the message.</p></li><li><p>When a computer receives a message, it compares its own counter to the counter in the message. It sets its own counter to the <em>maximum</em> of the two values, and then increments it by one.</p></li></ol><p>An example with two computers, P1 and P2:</p><ul><li><p><strong>P1</strong> has an internal event. Its counter goes from 0 to <strong>1</strong>.</p></li><li><p><strong>P1</strong> sends a message to P2 with the value <strong>1</strong>.</p></li><li><p><strong>P2</strong> receives the message. Its counter is 0. It sees the message's value is 1. It updates its counter to <code>max(0, 1) + 1 = 2</code>.</p></li><li><p><strong>P2</strong> has an internal event. Its counter goes from 2 to <strong>3</strong>.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NrMD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NrMD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 424w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 848w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 1272w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NrMD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png" width="1456" height="552" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:552,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:126522,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163728719?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NrMD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 424w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 848w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.png 1272w, https://substackcdn.com/image/fetch/$s_!NrMD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F351cad14-b0c0-4480-98fb-2c372d52b67e_2449x929.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>By looking at these logical timestamps, we know that the event at P1 (timestamp 1) <em>happened before</em> the event at P2 (timestamp 3). This method guarantees that if event A caused event B, the logical timestamp of A will be less than the timestamp of B.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How to Keep Services Running During Failures?]]></title><description><![CDATA[Strategies for Graceful Degradation in Large Scale Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/how-to-keep-services-running-during</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-keep-services-running-during</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Sat, 16 Aug 2025 14:53:39 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4b016280-f2b9-4a98-a3b0-698e41aba492_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In software applications, a server can fail, a database can become unresponsive, or a sudden traffic surge can overload the system. Most of the time, this is bad news. But what if there was a way to survive the chaos? What if your app, instead of crashing completely, could keep its cool and deliver a working, if slightly reduced, experience? This is where Graceful Degradation comes into the picture.</p><p>Graceful degradation is a design principle where a system maintains essential functionality even when parts of it fail. Instead of crashing completely, the system operates at a reduced capacity. For example, on a video streaming platform, if the recommendation service fails, the platform shouldn't crash. Instead, it might gracefully degrade by showing a generic list of popular videos rather than personalized suggestions. The core function&#8212;video playback&#8212;remains available.</p><h2>Strategies for Graceful Degradation</h2><p>Implementing graceful degradation requires a combination of strategies that control traffic, manage failures, and provide visibility into the system's health.</p><h3>Rate Limiting or Request Throttling</h3><p>Think of rate limiting or request throttling as a bouncer controlling the traffic to your servers. During a <a href="https://newsletter.scalablethread.com/p/how-to-handle-sudden-bursts-of-traffic">huge flash sale</a> or a <a href="https://newsletter.scalablethread.com/p/how-amazon-route-53-handles-ddos">denial-of-service attack</a>, your system can be flooded with requests. A rate limiter lets a certain number of requests in and tells the <a href="https://newsletter.scalablethread.com/p/how-to-handle-sudden-bursts-of-traffic?open=false#%C2%A7exponential-jitter-and-retry">rest to come back later</a>. This simple move stops a server from overloading or failing with many incoming requests.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fD2x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fD2x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 424w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 848w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 1272w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fD2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png" width="700" height="776" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:776,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&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;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fD2x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 424w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 848w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.png 1272w, https://substackcdn.com/image/fetch/$s_!fD2x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf3e383a-c026-4ecf-9084-c7e3354cc84a_700x776.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><figcaption class="image-caption">Fig. Rate Limiting</figcaption></figure></div><h3>Request Coalescing</h3><p>If thousands of people hit submit on an application to get the same data, instead of sending thousands of identical queries to your database or datastore, you send just one. You get the answer and then distribute it to everyone who was waiting. It's efficient, saves resources, and keeps your system from getting bogged down with repetitive tasks. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_M05!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_M05!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 424w, https://substackcdn.com/image/fetch/$s_!_M05!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 848w, https://substackcdn.com/image/fetch/$s_!_M05!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 1272w, https://substackcdn.com/image/fetch/$s_!_M05!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_M05!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png" width="1456" height="969" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:969,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:188395,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/170039880?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_M05!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 424w, https://substackcdn.com/image/fetch/$s_!_M05!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 848w, https://substackcdn.com/image/fetch/$s_!_M05!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.png 1272w, https://substackcdn.com/image/fetch/$s_!_M05!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F484f9b9d-ea65-4bf8-8782-844a1e036c26_2286x1522.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><figcaption class="image-caption">Fig. Request Coalescing</figcaption></figure></div><h3>Request Drops or Load Shedding</h3><p>Sometimes, you just can't serve everyone. In those moments, load shedding becomes a necessity. This is the act of dropping non-critical requests to save the most important ones. For an e-commerce site, this means prioritizing a user's final purchase over an analytical request to log their click.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UXu-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UXu-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 424w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 848w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 1272w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UXu-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png" width="793" height="705" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:705,&quot;width&quot;:793,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&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;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UXu-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 424w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 848w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.png 1272w, https://substackcdn.com/image/fetch/$s_!UXu-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52d97d7c-34cd-402a-9959-b4c6e7684e1b_793x705.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><figcaption class="image-caption">Fig. Load Shedding</figcaption></figure></div><h3>Jitter and Retry</h3><p>When a service fails and then recovers, a huge list of waiting clients will try to connect all at once. This is a classic <a href="https://newsletter.scalablethread.com/p/how-to-handle-sudden-bursts-of-traffic">thundering herd problem</a>, and it can immediately crash the service all over again. The solution is retry with jitter. Instead of all clients retrying at the same time, you add a small, random delay. This distributes the requests over a period of time instead of sending all requests at the same time, giving the service time to recover completely.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5H7y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5H7y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 424w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 848w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 1272w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5H7y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png" width="927" height="792" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f928067b-5ad2-454a-ba59-c50827a126cd_927x792.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:792,&quot;width&quot;:927,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&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;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5H7y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 424w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 848w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.png 1272w, https://substackcdn.com/image/fetch/$s_!5H7y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff928067b-5ad2-454a-ba59-c50827a126cd_927x792.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><figcaption class="image-caption">Fig. Jitter and Retry</figcaption></figure></div><h3>Circuit Breakers</h3><p>This pattern is just like the electric circuit breaker in your home. If a service keeps failing, you open the circuit. All subsequent requests to that service are immediately blocked for a set period. This saves resources, provides an instant failure response to the user, and gives the broken service time to heal. After a timeout, it slowly lets a few requests through to see if the service is healthy again. If they succeed, the circuit closes, and normal operation resumes. If they fail, it opens again.</p><p>Consider an architecture where an Order Service needs to call a Payment Service. If the Payment Service becomes unresponsive, the circuit breaker in the Order Service will open. For the next 60 seconds, any attempt to process a new order will instantly fail without trying to contact the Payment Service, saving resources and providing a fast failure response to the user. After 60 seconds, it will let one request through to test the connection.</p><h3>Request Timeouts</h3><p>A client application should not wait indefinitely for a response from a service. If a response isn't received within a configured time frame, the request should be abandoned, and resources should be freed up. This prevents a slow or unresponsive downstream service from causing resource exhaustion (like holding onto threads or connections) in the upstream service.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Qrti!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Qrti!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 424w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 848w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 1272w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Qrti!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png" width="1456" height="907" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:907,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:149404,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/170039880?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Qrti!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 424w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 848w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.png 1272w, https://substackcdn.com/image/fetch/$s_!Qrti!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2ef0faea-0f12-43c4-868b-f170d9873d4e_2269x1413.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><figcaption class="image-caption">Fig. Request Timeout</figcaption></figure></div><h3>Monitoring and Alerts</h3><p>Monitoring and alerting<strong> </strong>are important for detecting failures before they escalate. This involves collecting metrics (e.g., error rates, latency, CPU utilization), logs, and traces from all system components. Alerting systems are configured on top of this data to notify engineers when key metrics cross dangerous thresholds. This allows for proactive intervention and quick diagnosis of problems.</p><p>A team running a large data processing pipeline would monitor the queue length of their <a href="https://newsletter.scalablethread.com/p/how-message-queues-work">message broker</a>. Suppose the number of messages in the queue suddenly starts growing rapidly. In that case, an alert is triggered and sent to the on-call engineer. This indicates that a consumer service is failing or is too slow, allowing the engineer to investigate and resolve the issue before the system runs out of memory or data processing falls too far behind.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eVSj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eVSj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 424w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 848w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 1272w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eVSj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png" width="1456" height="997" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:997,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:109070,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/170039880?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eVSj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 424w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 848w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.png 1272w, https://substackcdn.com/image/fetch/$s_!eVSj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7bc386e-8cb3-4284-a24f-0e2f4d51cc29_1929x1321.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><figcaption class="image-caption">Fig. Alerting &amp; Monitoring</figcaption></figure></div><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How to Optimize Performance with Cache Warming?]]></title><description><![CDATA[Optimizing Performance and User Experience in Large-Scale Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/how-to-optimize-performance-with</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-optimize-performance-with</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 01 Aug 2025 21:31:17 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/dfed5368-e8a6-4493-b3de-207e928862cc_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is Cache Warming?</h2><p>When a request arrives for data that isn't in the cache (<em><a href="https://newsletter.scalablethread.com/i/147731225/what-is-cache-invalidation">cache miss or cold cache</a></em>), the system must retrieve it from the primary database or service. This process introduces latency, as fetching data from disk-based databases is significantly slower than from an in-memory cache. </p><p><em>Cache warming</em> avoids this <a href="https://newsletter.scalablethread.com/p/how-to-avoid-cache-stampede-or-dogpile">performance penalty</a> by ensuring that high-demand data is already in the cache when the first client request arrives. It preloads frequently accessed data into a cache. This is especially important during system restarts, deployments, or traffic spikes.</p><h2>Cache Warming Strategies</h2><p>Several strategies can be employed to warm a cache.</p><h4>Pre-warming on Deployment</h4><p>Immediately after a new service version is deployed, its in-memory cache is typically empty. A script or process is initiated to populate the cache. The script identifies the most critical or frequently accessed data and loads it. Preloading the cache with frequently accessed data prevents an initial flood of cache misses and a subsequent <a href="https://newsletter.scalablethread.com/p/how-to-handle-sudden-bursts-of-traffic">thundering herd problem</a>.</p><p>For example, in a global e-commerce platform, product catalog data is accessed millions of times per hour. Suppose a new application instance starts without a warm cache. In that case, the first thousand requests might hit the database directly, increasing response time and database load. The system avoids this bottleneck by warming the cache with top-selling product IDs before the instance accepts traffic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wikh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wikh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 424w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 848w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 1272w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wikh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png" width="1456" height="863" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:863,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:266451,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/169311957?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Wikh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 424w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 848w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.png 1272w, https://substackcdn.com/image/fetch/$s_!Wikh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87ac3473-66fa-4894-9c03-48333d78b66d_3347x1984.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><figcaption class="image-caption">Fig. Pre-warming on Deployment</figcaption></figure></div><h4>Scheduled Warming</h4><p>Scheduled warming involves running a batch job regularly (e.g., hourly or daily) to refresh and populate the cache. This is useful for data that doesn't need to be real-time but is computationally expensive. </p><p>A data analytics platform might run a nightly job to pre-compute and cache complex analytical queries for its customers. When users request their daily report, the pre-computed result is served directly from the cache, avoiding a resource-intensive database query.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!65V8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!65V8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 424w, https://substackcdn.com/image/fetch/$s_!65V8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 848w, https://substackcdn.com/image/fetch/$s_!65V8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 1272w, https://substackcdn.com/image/fetch/$s_!65V8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!65V8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png" width="1456" height="1126" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1126,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:253829,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/169311957?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!65V8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 424w, https://substackcdn.com/image/fetch/$s_!65V8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 848w, https://substackcdn.com/image/fetch/$s_!65V8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.png 1272w, https://substackcdn.com/image/fetch/$s_!65V8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d1b0c5e-8bff-4a66-ae4e-e1c4311075b5_2746x2123.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><figcaption class="image-caption">Fig. Scheduled Warming</figcaption></figure></div><h4>Event-Driven Warming</h4><p>This dynamic strategy triggers the cache warming process in response to specific events within the system. </p><p>For instance, an e-commerce site running a flash sale, warming the cache with relevant content or product data just before the event can prevent the system from crashing under the initial load or if an item in an e-commerce inventory drops below a certain threshold, it might trigger a process to cache related or alternative products, anticipating that users will be looking for substitutes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uTlg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uTlg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 424w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 848w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uTlg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png" width="1456" height="1213" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1213,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:235887,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/169311957?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!uTlg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 424w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 848w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.png 1272w, https://substackcdn.com/image/fetch/$s_!uTlg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb7bc945-ae27-4c70-bbb5-7c08e346502a_2400x2000.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><figcaption class="image-caption">Fig. Event-Driven Warming</figcaption></figure></div><h4>Just-in-Time (JIT) Warming</h4><p>When a cache miss occurs for an item, the system fetches it from the database. However, instead of just caching that single item, it also fetches and caches related items that will likely be requested next. </p><p>For example, if a user requests page 1 of a product listing, the system could proactively fetch and cache pages 2 and 3, assuming the user will likely navigate to them.</p><h2>Cons of Cache Warming</h2><p>Despite its benefits, cache warming has drawbacks and requires careful implementation.</p><ul><li><p><strong>Increased System Complexity:</strong> Implementing cache warming adds another moving part to the system architecture. It requires writing, managing, and monitoring warming scripts or services. This introduces new potential points of failure that must be handled.</p></li><li><p><strong>Resource Consumption Spikes:</strong> Running a cache warming process can be resource-intensive. It can cause a sudden spike in CPU, memory, and network I/O on both the application servers and the backend databases. If not managed carefully, a warming process could negatively impact the performance of the live system. This is often mitigated by running warmers on dedicated instances or throttling the warming process.</p></li><li><p><strong>Stale Data:</strong> A challenge with any caching strategy is <a href="https://newsletter.scalablethread.com/i/159954063/balancing-freshness-and-performance">data staleness</a>. A cache warmed with a data snapshot can quickly become outdated if the source data changes.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How Tool Calling Works in LLMs]]></title><description><![CDATA[Understanding the Internals of Tool Calling in Large Language Models]]></description><link>https://newsletter.scalablethread.com/p/how-tool-calling-works-in-llms</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-tool-calling-works-in-llms</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 20 Jun 2025 21:30:30 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/cf639de5-9d6f-482b-ae74-5c3ec6f7716e_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A Large Language Model&#8217;s (LLM&#8217;s) internal knowledge is limited to the data it was trained on, which can be outdated or lack direct access to real-time data, proprietary systems, or complex computational functions. Tool calling extends the capabilities of LLMs by allowing them to interact with external systems and access real-time or proprietary information.</p><h1>What are the Tools in LLMs?</h1><p>Tools are external functions or APIs a model can invoke to perform tasks beyond its inherent textual knowledge. These tasks include searching the web, performing calculations, accessing databases, sending emails, or interacting with other software services.</p><h4>Example</h4><p>Consider a large e-commerce platform's customer service chatbot powered by an LLM. When a user asks, "What is the shipping status of my order number 12345?" the LLM does not have direct access to the shipping information. A tool would be an API call to the platform's order-tracking system. The LLM, recognizing the user's intent, would call this tool, pass the order number (12345) as a parameter, and then receive the shipping status, which it can then communicate back to the user.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OkV5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OkV5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 424w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 848w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 1272w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OkV5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png" width="1456" height="924" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:924,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:370583,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/166417195?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OkV5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 424w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 848w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.png 1272w, https://substackcdn.com/image/fetch/$s_!OkV5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa057e7c-4f30-4b1f-8429-2ecfd38ea07e_3438x2182.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><figcaption class="image-caption">Fig. LLM decides to call Weather toll when user queries about the weather</figcaption></figure></div><h1>How Tool Calling Works</h1><p>The process of tool calling involves a series of steps that begin with the user's prompt and end with the LLM generating a response that incorporates the tool's output.</p><h3>How LLM Knows It Can Call a Tool</h3><p>The LLM's ability to call a tool results from its training and the information provided during inference.</p><ul><li><p><strong>During Training: </strong>When an LLM is trained for tool calling, it's exposed to vast amounts of data where natural language queries are paired with corresponding tool calls and their expected outputs. The LLM develops a deep understanding of language patterns, semantic relationships, and intent through its pre-training on massive text data. <br>When it encounters tool definitions, it learns to associate specific patterns in user queries with the functionalities described by the tool.  For example, if the tool <code>getCurrentWeather </code>has the description "Fetches the current weather conditions," the LLM forms an internal association between phrases like "What's the weather," "temperature in," "Is it raining," and this tool. This isn't explicit programming but rather an association learned from the training data.</p></li><li><p><strong>During Inference: </strong>These tool definitions are explicitly provided to the LLM as part of its input prompt or context window. These definitions specify the tool's name, a description of what it does, and the parameters it expects. The LLM then processes these definitions alongside the user's query. This is similar to giving a human a list of available functions and their documentation before asking them to solve a problem. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VITo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VITo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 424w, https://substackcdn.com/image/fetch/$s_!VITo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 848w, https://substackcdn.com/image/fetch/$s_!VITo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 1272w, https://substackcdn.com/image/fetch/$s_!VITo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VITo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png" width="662" height="372" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:372,&quot;width&quot;:662,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24876,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/166417195?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VITo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 424w, https://substackcdn.com/image/fetch/$s_!VITo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 848w, https://substackcdn.com/image/fetch/$s_!VITo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.png 1272w, https://substackcdn.com/image/fetch/$s_!VITo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4305eb70-e73c-4f95-9964-73cda69b9d43_662x372.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><figcaption class="image-caption">Fig. A tool definition for an order tracking system</figcaption></figure></div></li></ul><h3>How LLM Decides When to Call a Tool</h3><p>The decision to call a tool is based on the LLM's understanding of the user's intent and the information required to fulfill that intent. The LLM first processes the user's input and attempts to answer it using its internal knowledge base. Suppose the query can be answered directly from its training data (e.g., "What is the capital of France?"). In that case, it will proceed to generate a direct textual response. </p><p>However, if the query requires external, real-time, or proprietary information not present in its pre-trained weights (e.g., "What's the current stock price of Google?"), the LLM identifies this knowledge gap. Then, LLM analyzes the user's query to understand the intent (underlying goal or action). It then attempts to map this recognized intent to the available functionalities described by the tools. For instance:</p><ul><li><p><strong>User</strong>: "What's the weather in Tokyo?" <br><strong>Intent</strong>: Get current weather information.</p></li><li><p><strong>User</strong>: "Book me a flight to New York for next Tuesday."<br><strong>Intent</strong>: Initiate a flight booking process.</p></li></ul><h3>How LLM Decides Which Tool to Call</h3><p>When multiple tools are available, the LLM selects the most appropriate one based on the user's query and the descriptions of the available tools. This decision is driven by the LLM's ability to match the semantic meaning of the user's request with the functionalities described in the tool definitions. The LLM might rank potential tools based on relevance scores or confidence levels. For example, if both a "weather forecasting tool" and a "stock price lookup tool" are available, and the user asks, "What's the current temperature in New York?" the LLM will favor the weather forecasting tool due to semantic similarity.</p><h3>How Results from a Tool are Used</h3><p>Once a tool call is executed, the external system returns a result. This result, often in a structured format like JSON, is fed back to the LLM. The LLM then integrates this new information into its context. It uses it to formulate an informative response to the user. In the e-commerce example, if the order tracking tool returns <code>{"status": "Shipped," "estimated_delivery": "2025-06-25"}</code>, the LLM would then generate a response like: "Your order number 12345 has been shipped and is estimated to be delivered by June 25, 2025."</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How Feature Flags Enable Safer, Faster, and Controlled Rollouts]]></title><description><![CDATA[Understanding Effective Rollouts Using Feature Flags in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/how-feature-flags-enable-safer-faster</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-feature-flags-enable-safer-faster</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Sat, 07 Jun 2025 16:12:56 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/da2d9ae9-d217-44ac-a44a-cb33e202002b_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What are Feature Flags?</h2><p>A feature flag is a mechanism that allows developers to turn certain functionalities on or off during runtime without deploying new code. Imagine a large e-commerce platform processing millions of <a href="https://newsletter.scalablethread.com/p/why-distributed-transactions-can">transactions</a>. If the platform wants to introduce a new, experimental recommendation algorithm, it's risky to release it to all users simultaneously. A feature flag acts like a light switch for this new algorithm. The code for the new algorithm is deployed as part of the application, but it's wrapped in a conditional block controlled by the feature flag. If the flag is "on," the new algorithm runs; if it's "off," the old, stable algorithm continues to operate. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V66z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V66z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 424w, https://substackcdn.com/image/fetch/$s_!V66z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 848w, https://substackcdn.com/image/fetch/$s_!V66z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 1272w, https://substackcdn.com/image/fetch/$s_!V66z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V66z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png" width="1456" height="777" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:777,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:196742,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/165193944?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V66z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 424w, https://substackcdn.com/image/fetch/$s_!V66z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 848w, https://substackcdn.com/image/fetch/$s_!V66z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.png 1272w, https://substackcdn.com/image/fetch/$s_!V66z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fe4a00e-06c1-438b-9a35-f1bd2f5234b2_2754x1469.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><figcaption class="image-caption">Fig. Partial and Controlled Rollouts with Feature Flags</figcaption></figure></div><h2>How Feature Flags Help with Rollouts</h2><p>Feature flags provide several advantages during the rollout of new features or changes in complex systems.</p><h4>Partial and Controlled Rollouts</h4><p>Instead of a "big bang" release where a new feature is pushed to 100% of users simultaneously, feature flags allow for a more controlled exposure. For instance, a video-sharing platform introducing a new video processing pipeline can initially enable it for just 1% of its user base or users in a specific geographic region. This limited exposure allows the team to monitor the new pipeline's performance, resource consumption (e.g., CPU, memory on data processing nodes), and error rates in a real-world environment but with a contained blast radius. If issues arise, only a small subset of users is affected. As confidence in the new feature grows, the percentage of users or servers exposed can gradually increase to 5%, 20%, 50%, and finally 100%.</p><h4>Faster Development Iteration &amp; Testing/Experimentation</h4><p>Feature flags allow faster development iterations because developers can merge incomplete or experimental features into the main codebase, hiding them behind a flag. This reduces merge conflicts and allows for testing in the production environment. Teams can run A/B tests by enabling a feature for one group of users (Group A) and keeping it disabled or providing an alternative for another group (Group B).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aZ-q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aZ-q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 424w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 848w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 1272w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aZ-q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png" width="1456" height="747" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:747,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:172445,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/165193944?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aZ-q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 424w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 848w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.png 1272w, https://substackcdn.com/image/fetch/$s_!aZ-q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7395a62-dbd3-4881-a08e-93e0cc7a181f_2624x1346.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><figcaption class="image-caption">Fig. Experimenting with Feature Flags</figcaption></figure></div><h4>Dynamic Configuration without Redeployment</h4><p>Feature flags allow functionality to be turned on or off without redeploying the service. In a distributed system with hundreds or thousands of service instances, redeploying all of them to turn a feature on or off is a significant operational overhead. The running application instances pick up feature flag changes within seconds or minutes, allowing for near real-time control over system behavior.</p><h4>Rapid Rollbacks</h4><p>When a new feature starts causing unexpected problems, feature flags allow instant rollbacks. Instead of redeployment of the previous application version, which can be time-consuming and stressful, the feature flag can be turned"off." The problematic feature is immediately disabled, and the system reverts to its prior stable state. This is much faster and safer than a traditional rollback process involving code changes and deployments.</p><h2>How Feature Flags Work</h2><p>A feature flag system involves two main components:</p><ol><li><p><strong>Conditional Logic in Code:</strong> The application code contains <code>if-else</code> statements (or similar conditional logic) that check the state of a feature flag. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!A9zq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!A9zq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 424w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 848w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 1272w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!A9zq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png" width="1456" height="535" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:535,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:268860,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/165193944?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!A9zq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 424w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 848w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.png 1272w, https://substackcdn.com/image/fetch/$s_!A9zq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F611081cc-73c4-489b-9d52-29f4993e2235_2284x840.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><figcaption class="image-caption">Fig. Conditional Logic for Feature Flags</figcaption></figure></div></li><li><p><strong>Flag Configuration Management:</strong> This is a system (often a dedicated service or a distributed configuration store like etcd or Consul) that stores the current state (on/off) and targeting rules (e.g., enabled for 10% of users, or for users in Canada) for all feature flags. Applications typically fetch the flag configurations from this system at startup and then periodically refresh them or subscribe to updates.</p></li></ol><h2>Downsides of Feature Flags</h2><ol><li><p><strong>Technical Debt: </strong>Over time, the number of feature flags in a codebase can grow substantially. Each flag adds complexity to the code (e.g., more conditional branches). Old flags, especially those for features that are now fully rolled out or abandoned, become technical debt.</p></li><li><p><strong>Testing Complexity: </strong>With each flag, the number of possible execution paths in an application increases, potentially making testing more complex.</p></li><li><p><strong>Management Overhead: </strong>A feature flagging system requires setup and ongoing management. Rules need to be defined, statuses tracked, and the system must be reliable.</p></li></ol><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[How to Handle Concurrency with Optimistic Locking?]]></title><description><![CDATA[Understanding How Distributed Systems Avoid Race Conditions]]></description><link>https://newsletter.scalablethread.com/p/how-to-handle-concurrency-with-optimistic</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-handle-concurrency-with-optimistic</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Sat, 17 May 2025 16:22:00 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/ea3a4fc4-1689-4f84-85df-3b06c1639599_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is Optimistic Locking?</h2><p>Managing concurrent access to shared resources is a critical challenge in distributed systems. Conflicting updates can lead to data corruption and inconsistency. Optimistic locking operates on the principle of "assume no conflict, but verify." Instead of <a href="https://newsletter.scalablethread.com/p/how-distributed-systems-avoid-race">preventing conflicts by acquiring locks upfront</a>, transactions can proceed. It only checks for conflicts at the point of committing changes. This process typically involves three distinct phases:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LX3P!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LX3P!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 424w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 848w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 1272w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LX3P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png" width="1456" height="1449" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1449,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:254472,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163729709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LX3P!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 424w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 848w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.png 1272w, https://substackcdn.com/image/fetch/$s_!LX3P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F654ab604-a063-4ec6-9e5b-11101175cf43_2210x2199.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><figcaption class="image-caption">Fig. Data Inconsistency with Concurrent Updates</figcaption></figure></div><h4>Read Phase</h4><p>A transaction reads data from the data store without acquiring any locks on the resources. The system notes the data's version at the time of the read, which can be a timestamp, a version number, or a checksum. The transaction then computes and prepares any intended modifications based on this read data.</p><p>For example, consider a distributed product inventory system. When a user attempts to update a product's stock level, the system first reads the current stock count and its associated version number. Let's say Product A has a stock of 100 and a version number of 5.</p><h4>Validation Phase</h4><p>Before committing any changes, the transaction enters the validation phase. Here, the system checks if any other concurrent transaction has modified the data read in the first phase. This is done by comparing the version of the data noted during the read phase with the current version of the data in the data store. If the versions match, it implies that no other transaction has altered the data, and the current transaction can proceed. If the versions differ, another transaction has modified the data in the interim, leading to a conflict.</p><p>For example, before updating the stock of Product A to 90 (due to a sale), the system checks if the version number for Product A in the database is still 5.</p><h4>Write Phase (or Rollback)</h4><p>If the validation phase is successful (no conflict detected), the transaction proceeds to the write phase. The prepared changes are applied to the data store, and the version of the modified data is updated (e.g., the version number is incremented). This makes the changes visible to other transactions. If the validation phase detects a conflict, the transaction is aborted or rolled back. The application then needs to decide how to handle this conflict &#8211; it might retry the entire transaction, notify the user, or apply a specific conflict resolution strategy.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!l8AX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!l8AX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 424w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 848w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 1272w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!l8AX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png" width="1456" height="1403" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1403,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:278060,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163729709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!l8AX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 424w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 848w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.png 1272w, https://substackcdn.com/image/fetch/$s_!l8AX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6643aa9-d7ba-44d7-b56f-c2f778074c17_2210x2129.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><figcaption class="image-caption">Fig. Optimistic Locking</figcaption></figure></div><h2>When to Use Optimistic Locking</h2><h4>Low Contention Environments</h4><p>The core assumption of optimistic locking is that conflicts are rare. Therefore, it shines in systems where the probability of multiple transactions attempting to modify the same piece of data simultaneously is low.</p><h4>Read-Heavy Workloads</h4><p>Systems where data is read frequently but modified infrequently benefit greatly from optimistic locking. Read operations do not require locks, allowing for high concurrency and responsiveness.</p><h4>High Concurrency Requirements</h4><p>By not holding locks for extended periods (or at all during the read and computation phases), optimistic locking allows a greater number of transactions to proceed concurrently. This can significantly improve system throughput.</p><h4>Systems Where Retries are Acceptable</h4><p>Since conflicts lead to transaction rollbacks, the application must be designed to gracefully handle these retries. Suppose the cost of retrying a transaction is low and the conflict rate is minimal. In that case, optimistic locking is an efficient choice.</p><h2>Problems with Optimistic Locking</h2><h4>Potential for Starvation</h4><p>Suppose a particular piece of data is highly contended. In that case, some transactions attempting to update it might repeatedly fail validation and get rolled back, leading to starvation for those transactions. Frequent conflicts can also lead to significant performance degradation due to repeated rollbacks and retries.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-KZP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-KZP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 424w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 848w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 1272w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-KZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png" width="1456" height="1459" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1459,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:233163,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163729709?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-KZP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 424w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 848w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.png 1272w, https://substackcdn.com/image/fetch/$s_!-KZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe840b7d0-922f-41eb-ae24-9ad296687deb_2163x2168.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><figcaption class="image-caption">Fig. Client A stuck in loop with transaction failures</figcaption></figure></div><h4>Complexity in Handling Retries</h4><p>The application logic must be robust enough to handle transaction failures and implement retry mechanisms. This can add complexity to the application development. Deciding on the appropriate retry strategy (e.g., number of retries, backoff period) is crucial.</p><h4>Data Validation Complexity</h4><p>Ensuring that the validation check is comprehensive and correct is critical. The validation logic can become intricate for complex transactions involving multiple data items.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How to Improve Performance of Your Database?]]></title><description><![CDATA[Strategies for Scaling Databases in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/how-to-improve-performance-of-your</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-improve-performance-of-your</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 09 May 2025 23:41:29 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/f3e78132-19ff-45cc-94cd-8d371aa5d36d_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Scaling a database is critical for systems handling massive data volumes and high traffic. Scaling becomes necessary for several primary reasons. First, <em><strong>performance</strong></em> degrades as data volume and <a href="https://newsletter.scalablethread.com/p/how-to-handle-sudden-bursts-of-traffic">concurrent user requests increase</a>. Queries that were once fast can become slow, leading to a poor user experience. Second, applications require <em><strong>high</strong> <strong>availability</strong></em>.</p><h1>Strategies</h1><p>Several techniques can be employed, often in combination, to scale a database.</p><h2>Indexing</h2><p><a href="https://newsletter.scalablethread.com/p/how-indexes-work-in-partitioned-databases">Indexes speed up data retrieval</a> by creating pointers to rows, similar to a book's index. Instead of scanning an entire table (full table scan) to find specific rows, the database can use an index to locate the data quickly. For example, consider a <code>orders </code>table with millions of rows. Searching for an order by the <code>order_id </code>without an index would require scanning every row. Creating an index on the <code>order_id </code>column allows the database to find the user's record almost instantly.</p><h4>Trade-off</h4><p>Indexes speed up read queries (<code>SELECT</code>) but slow down write operations (<code>INSERT, UPDATE, DELETE</code>) because the index must also be updated during writes. They also consume disk space. Choose indexes wisely based on common query patterns.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lHwA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lHwA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lHwA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png" width="1456" height="540" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:540,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:333894,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163016908?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lHwA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!lHwA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1dc4dfbf-2126-41ec-9754-d53522f65696_4432x1644.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><figcaption class="image-caption">Fig. The orders table is partitioned by customer_id with a global secondary index is created on product_id. Query SELECT * FROM orders WHERE product_id = 'product_id_xyz' returns [(order_id_44, customer_id_20), (order_id_64, customer_id_32)] from the global secondary index. It  uses the retrieved customer_id values (the partition key) to determine the specific data partitions holding the relevant rows.</figcaption></figure></div><h2>Materialized Views</h2><p>Materialized views store the pre-computed result of a query. For complex queries involving joins or aggregations that run frequently, computing the result once and storing it can reduce query time. For example, an analytics dashboard displays daily total sales aggregated by product category. This query involves joining <code>orders</code>, <code>order_items</code>, and <code>products </code>tables and performing aggregations. Creating a materialized view, <code>daily_sales_by_category</code>, that stores these daily totals allows the dashboard to load much faster by querying this simple view.</p><h4>Trade-off</h4><p>The data in a materialized view can become stale. It needs a refresh strategy that consumes resources (e.g., nightly or triggered by data changes). Materialized views also consume storage space.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qMx7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qMx7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 424w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 848w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qMx7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png" width="1456" height="743" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:743,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97069,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163016908?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qMx7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 424w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 848w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!qMx7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7bda921-59c1-4d8e-a0d5-b4e9f0b5600b_2002x1022.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><figcaption class="image-caption">Fig. Materialized Views</figcaption></figure></div><h2>Denormalization</h2><p>Normalization is organizing database tables to reduce data redundancy and improve data integrity, often involving splitting data into multiple related tables. Denormalization is the opposite process: intentionally adding redundant data to one or more tables to avoid costly joins when querying. For example, an e-commerce application must frequently display the product name and order details. A normalized design might require joining the orders table with the products table. To speed this up, you could denormalize by adding a product_name column directly to the orders table. This avoids the join during reads.</p><h4>Trade-off</h4><p>Denormalization increases storage requirements. More importantly, it complicates data updates and increases the risk of data inconsistency. If a product name changes, it must be updated in every order record referencing it, not just in the central products table.</p><h2>Vertical Scaling</h2><p>This involves increasing the resources of the existing database server, such as adding more CPU power, RAM, or faster disk storage (like SSDs).</p><h4>Trade-off</h4><p>Physical limits exist to how much you can scale up a single machine. High-end hardware is expensive. It does not significantly improve fault tolerance, as you still have a single point of failure. Upgrades often require downtime.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Nk-3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Nk-3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 424w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 848w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 1272w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Nk-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png" width="1456" height="763" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:763,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94393,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163016908?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Nk-3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 424w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 848w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.png 1272w, https://substackcdn.com/image/fetch/$s_!Nk-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b8a6511-7c0b-49bd-b186-3c819c3a477e_1672x876.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><figcaption class="image-caption">Fig. Vertical Scaling</figcaption></figure></div><h2>Database Caching</h2><p><a href="https://newsletter.scalablethread.com/p/how-consistent-hashing-minimizes">Caching </a>involves storing frequently accessed data in a temporary, fast-access memory layer (like Redis or Memcached) between the application and the database. Subsequent requests for the same data can be served directly from the cache, bypassing the database entirely. For example, a news website caches popular articles. When a user requests an article, the application first checks the cache. If the article is cached, it's returned immediately. If not, the application queries the database, returns the article to the user, and stores a copy in the cache for future requests.</p><h4>Trade-off</h4><p><a href="https://newsletter.scalablethread.com/p/why-cache-invalidation-is-hard">Cache invalidation is a complex problem</a>. Caching adds another component to manage and introduces potential consistency issues (stale data). It primarily helps with read-heavy workloads.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fd4C!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fd4C!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 424w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 848w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 1272w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fd4C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png" width="1456" height="1012" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1012,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:111498,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163016908?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fd4C!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 424w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 848w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.png 1272w, https://substackcdn.com/image/fetch/$s_!fd4C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F163b12a0-4a2d-4c51-925d-645ef6caba2a_1485x1032.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><figcaption class="image-caption">Fig. Database Caching</figcaption></figure></div><h2>Replication</h2><p><a href="https://newsletter.scalablethread.com/p/single-leader-multi-leader-and-leaderless">Replication </a>involves creating one or more copies (replicas) of the primary database server. Write operations (<code>INSERT, UPDATE, DELETE</code>) to the primary server. Read operations (<code>SELECT</code>) can be distributed across the replicas. This distributes the read load and improves read throughput. Replication also provides high availability; if the primary fails, <a href="https://newsletter.scalablethread.com/i/159551346/replication">a replica can be promoted to the new primary.</a></p><h4>Trade-off</h4><p>There is often a replication lag before changes on the primary appear on the replicas. This can lead to reading slightly stale data. It increases infrastructure costs and complexity. It does not solve bottlenecks related to high write volumes on the primary server.</p><h2>Sharding</h2><p><a href="https://newsletter.scalablethread.com/p/what-is-function-sharding-in-serverless">Sharding </a>involves partitioning the database data horizontally across multiple independent database servers, called shards. Each shard contains a subset of the total data. The application logic or a proxy layer routes queries to the appropriate <a href="https://newsletter.scalablethread.com/p/how-indexes-work-in-partitioned-databases">shard based on a partition key.</a> This distributes both read and write loads across many machines.</p><h4>Trade-off</h4><p>Sharding increases complexity in both application development and database operations. Queries that need data from multiple shards <a href="https://newsletter.scalablethread.com/i/161065644/local-secondary-indexes">become complex and slow</a>. Rebalancing data is <a href="https://newsletter.scalablethread.com/i/148600431/what-if-a-server-goes-down-or-a-new-one-is-added">challenging</a>. Choosing an appropriate shard key is critical to avoid uneven data distribution.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!J4Ks!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!J4Ks!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 424w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 848w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 1272w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!J4Ks!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png" width="1456" height="1129" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93bac987-3e53-4289-b854-95afba79b900_4169x3234.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1129,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:700879,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/163016908?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!J4Ks!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 424w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 848w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.png 1272w, https://substackcdn.com/image/fetch/$s_!J4Ks!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93bac987-3e53-4289-b854-95afba79b900_4169x3234.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><figcaption class="image-caption">Fig. Sharding v/s Replication</figcaption></figure></div><h1>When to Consider?</h1><p>Scaling introduces complexity and cost, so it should not be implemented prematurely. Start monitoring key database metrics early. Before implementing complex scaling solutions, always investigate if <em><strong>query optimization</strong></em> or <em><strong>schema improvements</strong></em> can resolve the bottlenecks. Rewriting inefficient queries or adding appropriate indexes sometimes provides relief without infrastructural changes.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[How Failover Works in Single Leader Databases]]></title><description><![CDATA[Strategies for Handling Failover in Single Leader Architectures]]></description><link>https://newsletter.scalablethread.com/p/how-failover-works-in-single-leader</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-failover-works-in-single-leader</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 02 May 2025 21:30:16 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/2501a821-fbd3-4b25-8507-9d2b5018029d_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is Single Leader System?</h2><p>In a <a href="https://newsletter.scalablethread.com/i/146489184/single-leader-replication">single-leader system</a>, one node (the leader) handles all write operations. Followers replicate the leader&#8217;s data and serve read requests to distribute the load. The request is sent only to the leader when a client wants to write data. The leader processes the write, records it in its <a href="https://newsletter.scalablethread.com/i/147344365/what-are-write-ahead-logs">local log</a> (a sequence of operations), and then sends this update to all its followers. Followers apply the updates in the same order as the leader, ensuring they eventually have the same data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iiVH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iiVH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 424w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 848w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 1272w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iiVH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png" width="613" height="558" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/01166538-e644-4743-9ce1-412363dd0cfc_613x558.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:558,&quot;width&quot;:613,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29265,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162593458?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iiVH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 424w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 848w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.png 1272w, https://substackcdn.com/image/fetch/$s_!iiVH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01166538-e644-4743-9ce1-412363dd0cfc_613x558.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><h2>What is Failover?</h2><p>Failover is the process of automatically switching to a backup system or node when the primary system or node fails or needs to be taken offline for maintenance. In single-leader systems, failover deals with two main scenarios: a follower node failing or the leader node failing. The goal is to minimize downtime and data loss.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!G8mI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!G8mI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 424w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 848w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 1272w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!G8mI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png" width="1456" height="562" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:562,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:442978,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162593458?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!G8mI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 424w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 848w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 1272w, https://substackcdn.com/image/fetch/$s_!G8mI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68d50a5d-ecc7-4baf-97d2-833a8dec688d_5678x2190.png 1456w" sizes="100vw"></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><h2>Follower Failover</h2><p>The followers are read-only replicas (or read-mostly) that do not accept direct writes from clients. However, their failure does not stop the system from accepting new data.</p><ol><li><p><strong>Detection:</strong> The leader or a monitoring system detects unresponsive followers (e.g., through missed heartbeats or failed connection attempts).</p></li><li><p><strong>Removal:</strong> The failed follower might be temporarily or permanently removed from the list of active followers. Read requests previously directed to it <a href="https://newsletter.scalablethread.com/p/what-is-service-discovery">need to be rerouted</a> to other followers or the leader.</p></li><li><p><strong>Replacement:</strong> A new follower node can be started. This new node needs to catch up with the leader's current state. It typically does this by copying a recent snapshot of the leader's data and then <a href="https://newsletter.scalablethread.com/i/147344365/how-does-wal-help-prevent-data-loss">applying all the changes from the leader's log</a> that occurred after the snapshot was taken. Once caught up, it starts receiving live updates like any other follower.</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_!cJjG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cJjG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 424w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 848w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 1272w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cJjG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png" width="1456" height="1034" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1034,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:219497,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162593458?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cJjG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 424w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 848w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.png 1272w, https://substackcdn.com/image/fetch/$s_!cJjG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0659ea58-a5d6-4e7e-8222-00c0cbef251f_2514x1786.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, if a follower crashes, a new virtual machine can be provisioned, installed with the necessary software, and configured to connect to the leader to start replication from the last known point or a full snapshot. Follower failover is relatively straightforward because the leader remains operational throughout the process. However, delays in detecting failures or slow synchronization can temporarily reduce redundancy, making the system more vulnerable.</p><h2>Leader Failover</h2><p>Leader failure is more critical because the leader is the only node that can accept writes. Without a leader, the system cannot process new data updates.</p><ol><li><p><strong>Detection:</strong> The first step is detecting that the leader has failed. This is done through health checks or heartbeats monitored by followers or an external coordination service. If the leader doesn't respond for a specific period, it's considered failed.</p></li><li><p><strong>Leader Election:</strong> Once the failure is confirmed, the system must choose a new leader from the available followers. The most common requirement is to select the follower with the most <a href="https://newsletter.scalablethread.com/i/147344365/what-are-write-ahead-logs">up-to-date data log</a>. This minimizes potential data loss. Various algorithms exist for leader election, often managed by consensus protocols (like Raft or Paxos) implemented directly by the nodes or external services.</p></li><li><p><strong>Reconfiguration:</strong> Clients and remaining followers must be informed about the new leader. Clients need to redirect their write requests to the newly elected leader. Followers need to disconnect from the old leader and follow the new one.</p></li></ol><p>There are two main types of leader failover:</p><h4>Graceful Failover (Planned)</h4><p>This happens during scheduled maintenance or the leader's planned shutdown. The current leader finishes processing pending writes, ensures one specific follower is fully up-to-date, and then transfers leadership to that follower before shutting down. This process typically involves no data loss because the handover is controlled. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H0wK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H0wK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 424w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 848w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 1272w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H0wK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png" width="1456" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:207412,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162593458?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H0wK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 424w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 848w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.png 1272w, https://substackcdn.com/image/fetch/$s_!H0wK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ef0d7a8-b33e-433e-a489-8b45f3e3d10c_3220x1525.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><h4>Emergency Failover (Unplanned)</h4><p>This occurs when the leader fails unexpectedly (e.g., crashes, network partition). The system must react automatically. A follower node (usually the one with the most recent data log) is promoted to be the new leader through the election process. If it recovers later, the old leader might rejoin the cluster as a follower to avoid having two leaders.</p><p><a href="https://newsletter.scalablethread.com/p/understanding-latency-in-distributed">Network latency</a> matters when choosing a new leader, especially in geographically distributed systems. Ideally, the new leader should be located "close" (in terms of network latency) to most clients or the previous leader's location to minimize the time it takes for requests to travel. However, the primary concern is <a href="https://newsletter.scalablethread.com/i/146489166/consistency-and-consistency-model">data consistency</a> (choosing the most up-to-date follower), so proximity is a secondary factor.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How to Build Idempotent APIs?]]></title><description><![CDATA[Understanding Idempotency in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/how-to-build-idempotent-apis</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-build-idempotent-apis</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 25 Apr 2025 22:48:49 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/2b49eee1-fcda-498a-8ac7-e81126ffcd7e_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Why Idempotency is Important? </h2><p>Idempotency refers to the property of an operation where executing it multiple times has the same effect as executing it once. Distributed systems inherently deal with unreliability. <a href="https://newsletter.scalablethread.com/i/146142578/partition-tolerance">Network partitions</a>, timeouts, server unavailability, or client crashes can interrupt communication flows. When a client sends a request but doesn't receive a timely response, it <a href="https://newsletter.scalablethread.com/p/what-is-the-two-generals-problem">cannot be certain whether the request was processed</a>, lost before reaching the server, or processed successfully, but the response was lost. The natural reaction is to retry the request. Without idempotency, <a href="https://newsletter.scalablethread.com/i/147056244/exponential-jitter-and-retry">retrying operations</a> can cause unintended side effects like duplicate payments or incorrect data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hi1D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hi1D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 424w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 848w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 1272w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hi1D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png" width="1456" height="977" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:977,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:246233,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162096233?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hi1D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 424w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 848w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.png 1272w, https://substackcdn.com/image/fetch/$s_!hi1D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F06204249-03e9-4d92-a769-9293e90f7ebf_2797x1876.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><figcaption class="image-caption">Fig. Idempotent Request</figcaption></figure></div><p>For example, a customer submits an order on an e-commerce platform, but a network glitch causes the client to retry the request. Without idempotency, the system might create two identical orders, charging the customer twice. An idempotent API ensures only one order is created, regardless of retries. Similarly, in financial systems, a funds transfer API must guarantee a single transfer even if the request is sent multiple times due to timeouts.</p><h2>Achieving Idempotency</h2><p>Implementing idempotency requires the server to detect duplicate requests. This involves tracking the state of requests using a unique identifier provided by the client, known as an <strong>Idempotency Key</strong>. The client generates a unique key (e.g., a UUID) for each distinct operation and it must send the same idempotency key for retries of the <em>same</em> operation (in case of failures).</p><ul><li><p>When a request arrives with an idempotency key, the server checks to see if it has processed one with this key before.</p></li><li><p>Suppose the key is recognized and associated with a completed operation. In that case, the server skips executing the operation again and returns the previously generated response.</p></li><li><p>Suppose the key is recognized, but the original operation is still in progress (e.g., held by a lock). The server might wait or return a conflict/busy status in that case.</p></li><li><p>If the key is new, the server processes the request, stores the result associated with the idempotency key, and then returns the response.</p></li></ul><h3>Idempotency in Stateful Services</h3><p>Stateful services manage idempotency by storing information about processed requests.</p><h4>Database Unique Constraints</h4><p>Databases provide built-in mechanisms to enforce idempotency. For example, assigning unique primary keys to entities ensures duplicate entries cannot exist. Continuing with the e-commerce example, if each order has a globally unique identifier (GUID), attempting to insert an order with the same GUID will fail due to a primary key violation. This technique works well for write-heavy systems but requires proper error handling.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jqwS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jqwS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 424w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 848w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 1272w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jqwS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png" width="1456" height="1035" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1035,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:252276,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162096233?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jqwS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 424w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 848w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.png 1272w, https://substackcdn.com/image/fetch/$s_!jqwS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9604915-8e8a-45db-8060-26148a87e1db_2797x1989.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><figcaption class="image-caption">Fig. Database Unique Constraints</figcaption></figure></div><h4>Deduplicating Duplicate Requests</h4><p>In distributed systems, messages may be delivered multiple times due to network issues or retries. Before processing each request, the system checks if the request's idempotency key has been seen before. For example, a message queue processing user signups might store processed message IDs in a distributed cache. If a duplicate message arrives, the system immediately skips reprocessing and acknowledges receipt.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IJjJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IJjJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 424w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 848w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 1272w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IJjJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png" width="1456" height="1152" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1152,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:390829,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162096233?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IJjJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 424w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 848w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.png 1272w, https://substackcdn.com/image/fetch/$s_!IJjJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F886016f2-7467-465c-bfef-efc1fb94ed8f_3350x2651.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><figcaption class="image-caption">Fig. Deduplication</figcaption></figure></div><h3>Idempotency in Stateless Services</h3><p>Stateless services cannot easily implement idempotency, as they retain no memory of past requests. Idempotency for stateless services is often handled by stateful components upstream (like an API gateway) or downstream (like ensuring the ultimate data storage operation is idempotent).</p><h3>Race Conditions in Idempotent Systems</h3><p>Race conditions occur when multiple processes access shared resources concurrently, potentially causing inconsistent outcomes. In distributed systems, race conditions are common due to high concurrency and network latencies. Consider a scenario where two identical requests with the same idempotency key arrive simultaneously at different server nodes. Without synchronization, both nodes might check the idempotency store, find no key, and process the payment, resulting in duplicate transactions. Mitigation strategies include:</p><ul><li><p><strong>Distributed Locking: </strong>Before processing, the <a href="https://newsletter.scalablethread.com/p/how-distributed-systems-avoid-race">service attempts to acquire a lock</a> named after the idempotency key. Only the lock holder proceeds with processing.</p></li><li><p><strong>Request Sharding: </strong>Using <a href="https://newsletter.scalablethread.com/i/148600431/what-is-consistent-hashing">consistent hashing on the idempotency key</a>, requests concerning the same key can be routed to the same server, reducing the need for global locks.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!L3dO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!L3dO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 424w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 848w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 1272w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!L3dO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png" width="1456" height="771" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:771,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:186549,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/162096233?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!L3dO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 424w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 848w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.png 1272w, https://substackcdn.com/image/fetch/$s_!L3dO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299f9c9a-8679-4db9-bef0-a72ff63bfb39_2654x1406.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><figcaption class="image-caption">Fig. Request Sharding</figcaption></figure></div></li><li><p><strong>Database Transaction Isolation Levels: </strong><a href="https://newsletter.scalablethread.com/p/how-transaction-isolation-provides">Transaction isolation levels </a>(like Serializable) can lock the relevant resource (the potential row for the idempotency key) during the check-and-process phase. This forces concurrent requests for the same key to serialize.</p></li><li><p><strong>Atomic Operations: </strong>Leverage atomic operations/commands provided by the underlying storage. The first successful insert wins; subsequent attempts fail gracefully or update a status, allowing the application to know the request was already handled.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[What is Key-Based vs Range-Based Partitioning in Databases?]]></title><description><![CDATA[Understanding Data Partitioning Strategies in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/what-is-key-based-vs-range-based</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/what-is-key-based-vs-range-based</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 18 Apr 2025 21:30:48 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4f3d7aa8-ff6e-4c27-a372-7fb4a9bd1941_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Last week we covered <a href="https://newsletter.scalablethread.com/i/161065644/what-is-partitioning">What is Partitioning</a> and <a href="https://newsletter.scalablethread.com/p/how-indexes-work-in-partitioned-databases">How Indexes Work in Partitioning</a>.</p><h2>Why Partition Data?</h2><p>Data is partitioned primarily for scalability. A single server has limits on CPU power, memory, disk space, and network bandwidth. As a dataset or the number of requests grows, a single server eventually becomes a bottleneck. By partitioning the data across multiple servers, the storage requirement is distributed. </p><h4>Scaling Reads and Writes</h4><p>The read/write query load can also be spread out. If data is partitioned effectively, a query targeting a specific piece of data must only be processed by the node(s) holding that partition rather than overwhelming a single central server. Similarly, write operations can be directed to the appropriate partition, allowing multiple writes to occur concurrently across the cluster. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6LCm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6LCm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 424w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 848w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 1272w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6LCm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png" width="1456" height="1117" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1117,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:210591,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161586465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6LCm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 424w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 848w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.png 1272w, https://substackcdn.com/image/fetch/$s_!6LCm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F660fa848-d646-4bcf-82df-10e67ec815ff_2490x1911.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><figcaption class="image-caption">Fig. Scaling Reads and Writes</figcaption></figure></div><h4>Fault Tolerance</h4><p>Fault tolerance is another critical benefit. If data is stored on a single machine and that machine fails, the entire dataset becomes unavailable. With partitioning, if one node fails, only the partitions it hosts become unavailable (<a href="https://newsletter.scalablethread.com/p/understanding-faults-and-fault-tolerance">replication strategies mitigate this</a>). The rest of the dataset remains accessible, improving the system's overall availability and resilience.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gBZr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gBZr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 424w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 848w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 1272w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gBZr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png" width="1456" height="1003" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1003,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:326966,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161586465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gBZr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 424w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 848w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 1272w, https://substackcdn.com/image/fetch/$s_!gBZr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93c84299-f8d9-4c95-a9cc-9a35bb5a11fa_3433x2366.png 1456w" sizes="100vw"></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><figcaption class="image-caption">Fig. Fault Tolerance</figcaption></figure></div><h4>Hotspots</h4><p>Achieving scalability through partitioning depends heavily on how the data is partitioned. A poorly designed partitioning strategy can lead to <strong>data skew</strong>, where some partitions store significantly more data than others. This imbalance causes <strong><a href="https://newsletter.scalablethread.com/p/how-to-handle-hot-shard-problem">hotspots</a></strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Omaf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Omaf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 424w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 848w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 1272w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Omaf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png" width="1456" height="692" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:692,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:157350,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161586465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Omaf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 424w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 848w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.png 1272w, https://substackcdn.com/image/fetch/$s_!Omaf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a89c9fd-56b1-49f5-8875-7c9a0aaff5bf_2514x1195.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><figcaption class="image-caption">Fig. Hot Shards</figcaption></figure></div><h2>Range-Based Partitioning</h2><p>Range-based partitioning involves dividing the data based on contiguous ranges of a specific attribute, known as the partition key. The system defines key ranges, each assigned to a specific partition. The partition key should be an attribute whose values have a natural order, such as timestamps, numerical IDs, or alphabetical strings.</p><p>For example, consider a database storing user information partitioned by <code>user_id</code>.</p><ul><li><p>Partition 1: <code>user_id </code>from 1 to 1,000,000</p></li><li><p>Partition 2: <code>user_id </code>from 1,000,001 to 2,000,000</p></li><li><p>Partition 3: <code>user_id </code>from 2,000,001 to 3,000,000</p></li></ul><p>Another example could use order dates:</p><ul><li><p>Partition A: Orders placed in January 2025</p></li><li><p>Partition B: Orders placed in February 2025</p></li><li><p>Partition C: Orders placed in March 2025</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!exu7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!exu7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 424w, https://substackcdn.com/image/fetch/$s_!exu7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 848w, https://substackcdn.com/image/fetch/$s_!exu7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!exu7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!exu7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png" width="1456" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:355233,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161586465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!exu7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 424w, https://substackcdn.com/image/fetch/$s_!exu7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 848w, https://substackcdn.com/image/fetch/$s_!exu7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!exu7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8185581b-0afd-400f-83a0-d248e5f07457_4281x2042.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><figcaption class="image-caption">Fig. Range-Based Partitioning</figcaption></figure></div><p>The primary advantage of range partitioning is its efficiency for range queries. Suppose an application needs to retrieve all users with IDs between 500,000 and 600,000. In that case, the system knows immediately that this data resides entirely within Partition 1. The query only needs to be routed to the node(s) holding Partition 1, significantly reducing the search scope compared to random assignment.</p><h4>Susceptible to Hotspots</h4><p>However, range partitioning is susceptible to creating hot spots. Specific partitions can become overloaded if access patterns are not uniformly distributed across the key ranges. In the <code>user_id </code>example, if newly registered users (with higher IDs) are accessed more frequently, the partitions holding the highest ID ranges will experience more load. In the order date example, if most queries target recent orders, the current month's data partition will become a hot spot for both reads and writes. </p><h2>Key-Based Partitioning</h2><p>Key-based partitioning aims to distribute data more evenly than range partitioning, mitigating the hot spot issues associated with predictable ranges. In this strategy, a hash function is applied to the partition key of each data record. The output of the hash function determines which partition the record belongs to. A common method is to use the modulo operator. If there are N partitions, the target partition for a record with<code> key k</code> might be calculated as <code>partition = hash(k) % N.</code></p><p>For example, consider partitioning user data by <code>user_id </code>across three partitions using a hash function:</p><ul><li><p>User with <code>user_id </code>123 &#8212;&gt; <code>hash(123)</code> = 84362 &#8212;&gt; <code>84362 % 3 = 2</code>. The record goes to <strong>Partition 2</strong>.</p></li><li><p>User with <code>user_id </code>456 &#8212;&gt;<code> hash(456) </code>= 19373 &#8212;&gt; <code>19372 % 3 = 1</code>. The record goes to <strong>Partition 1</strong>.</p></li><li><p>User with <code>user_id </code>789 &#8212;&gt; <code>hash(789)</code> = 55219 &#8212;&gt; 55221 % 3<code> = 0</code>. The record goes to <strong>Partition 0</strong>.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!i6jW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!i6jW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 424w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 848w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!i6jW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png" width="1456" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:344757,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161586465?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!i6jW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 424w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 848w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.png 1272w, https://substackcdn.com/image/fetch/$s_!i6jW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99545ca4-b926-4762-a315-b9401a3b192f_4281x2042.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><figcaption class="image-caption">Fig. Key-Based Patitioning</figcaption></figure></div><p>A <a href="https://newsletter.scalablethread.com/i/148600431/what-is-consistent-hashing">good hash function</a> distributes keys pseudo-randomly and uniformly across its possible output values. This ensures that each partition receives a roughly equal amount of data and request load. This approach effectively spreads write load and avoids hot spots caused by access patterns concentrating on specific key ranges.</p><p>The main disadvantage of hash partitioning is that it makes range queries inefficient. Since keys that are close together in their natural order (e.g., user_id 1000 and 1001) are likely mapped to different partitions by the hash function, retrieving a range of keys (e.g., all users with IDs between 1000 and 2000) requires querying all partitions. The relationship between adjacent keys is lost in the hashing process.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[How Indexes Work in Partitioned Databases]]></title><description><![CDATA[Understanding Local and Global Secondary Indexing for Large-Scale Systems]]></description><link>https://newsletter.scalablethread.com/p/how-indexes-work-in-partitioned-databases</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-indexes-work-in-partitioned-databases</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 11 Apr 2025 21:30:52 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/a52aff33-c0d1-414b-922b-28fdf2f0ea30_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is Partitioning?</h2><p>Managing and efficiently querying vast amounts of data in distributed systems is a big challenge. Partitioning (or sharding) is a common technique to handle this scale. It involves splitting a large database into smaller, more manageable pieces called partitions. Each partition holds a portion of the data and can live on a separate machine in a cluster. For example, consider a customer database with millions of records. The system can distribute the load across multiple machines by partitioning the data based on a key such as CustomerID.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Vb7u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Vb7u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 424w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 848w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 1272w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Vb7u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png" width="1456" height="1346" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1346,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:296757,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161065644?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Vb7u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 424w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 848w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.png 1272w, https://substackcdn.com/image/fetch/$s_!Vb7u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b00bb70-9ba6-4524-8f8b-07208adec342_2489x2301.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><figcaption class="image-caption">Fig.Partitioning</figcaption></figure></div><p>Each partition acts like a smaller, independent database. A <strong>primary index</strong> is maintained within each partition, typically based on the table's primary key. If the table's primary key is also the partition key, query filtering can be done as follows:</p><ol><li><p>The query router (or client) determines the target partition using the partition key value provided in the query (e.g., by hashing it).</p></li><li><p>The query is sent <em>only</em> to that specific partition.</p></li><li><p>The target partition uses its local primary index to locate the data quickly.</p></li></ol><p>For example, SELECT * FROM orders WHERE customer_id = 54321 AND order_id = 9876; If partitioning is by customer_id, the system hashes 54321 to find the correct partition. That partition then uses its local index (likely on (customer_id, order_id) or order_id to find the specific order.</p><h2>Secondary Indexes</h2><p>Often, applications need to query data based on attributes <em>other than</em> the partition key, such as searching orders by product_id. Performing such queries without an appropriate index would require scanning <em>every row in every partition</em> (a full table scan), which is infeasible in large systems. To support such queries, secondary indexes are created on these non-partition key columns. In partitioned databases, secondary indexes can be <strong>local</strong> (tied to one partition) or <strong>global</strong> (covering all partitions).</p><h4>Local Secondary Indexes</h4><p>A <strong>local secondary index</strong> exists within each data partition and only references data stored on that specific partition. For example, the orders table is partitioned by <code>customer_id</code>. A local secondary index is created on <code>product_id</code>. Partition 1's <code>product_id </code>index only contains entries for orders belonging to customers whose <code>customer_id </code>hashes to Partition 1.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k6DE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k6DE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k6DE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png" width="1456" height="540" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:540,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:354314,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161065644?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!k6DE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!k6DE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c442ce0-13c9-4360-bee4-e3bfba8798f6_4432x1644.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><figcaption class="image-caption">Fig. Local Secondary Index</figcaption></figure></div><p>Despite their benefits, local secondary indexes have limitations. Since they are confined to individual partitions, querying them requires the system to perform a "<strong>scatter and gather</strong>" operation. This means the query is broadcast to all partitions. Each partition independently searches its local secondary index for the matching data, and the query coordinator aggregates the results. While effective for small datasets, this approach becomes inefficient at scale because it increases network overhead and query latency.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7Eq2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7Eq2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 424w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 848w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 1272w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7Eq2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png" width="1456" height="785" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:785,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:457852,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161065644?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7Eq2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 424w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 848w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.png 1272w, https://substackcdn.com/image/fetch/$s_!7Eq2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26acaabc-d9bd-41ef-b628-0119c4273631_4432x2389.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><figcaption class="image-caption">Fig. Scatter and Gather </figcaption></figure></div><h4>Global Secondary Indexes</h4><p>Distributed systems use <strong>global secondary indexes</strong> to overcome the limitations of local secondary indexes<strong>. </strong>A global secondary index spans all or multiple partitions and is not tied to the table's primary/partition key. It maps the secondary key value(s) directly to the base table's primary/partition key(s). Queries targeting the secondary key first consult the global index. The index returns the primary/partition key(s) of the matching rows. The system then uses these keys to query only the necessary data partitions directly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2Y9N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2Y9N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2Y9N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png" width="1456" height="540" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:540,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:333540,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161065644?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2Y9N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!2Y9N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bf927bc-cedb-45a1-b4ce-1c6984e5ae98_4432x1644.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><figcaption class="image-caption">Fig. Global Secondary Index</figcaption></figure></div><p>For example, the orders table is partitioned by customer_id. A global secondary index is created on product_id. This index stores entries like</p><ul><li><p><code>product_id_abc =&gt; [(order_id_1, customer_id_99), (order_id_39, customer_id_56)] </code>and </p></li><li><p><code>product_id_xyz =&gt; [(order_id_44, customer_id_20), (order_id_64, customer_id_32)]</code></p></li></ul><p>Query<code> SELECT * FROM orders WHERE product_id = 'product_id_xyz' </code>returns  <code>[(order_id_44, customer_id_20), (order_id_64, customer_id_32)] </code>from the global secondary index and then uses the retrieved <code>customer_id</code> values (the partition key) to determine the specific data partitions holding the relevant rows. Queries (eg. <code>SELECT * FROM orders WHERE customer_id = 'customer_id_32' AND order_id = 'order_id_64'</code>) are then sent directly to those partitions using the full primary key.</p><h4>Problems with Keeping Global Index</h4><p>Global secondary indexes solve the problem of scattering and gathering by centralizing the indexed data. However, maintaining a global index on a single machine poses challenges. The node can become the single point of failure or a bottleneck during index reads and updates. As the dataset grows, the index itself can become a bottleneck, leading to slower query performance and potential downtime.</p><h4>Splitting Global Indexes Across Multiple Partitions</h4><p>The index itself is partitioned across multiple nodes, similar to how the base data is partitioned, to address the scalability issues of global secondary indexes. The partition key for the global index is typically the indexed column itself. Each index partition holds a slice of the global index entries based on the index's partition key.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MkJw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MkJw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MkJw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png" width="1456" height="540" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:540,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:364870,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/161065644?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MkJw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 424w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 848w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.png 1272w, https://substackcdn.com/image/fetch/$s_!MkJw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe220389e-9519-481a-96a6-00d8d0d49371_4432x1644.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><figcaption class="image-caption">Fig. Partitioning Global Secondary Index </figcaption></figure></div><p>For example, the global index on <code>product_id</code> (shared previously) is partitioned across 2 nodes based on <code>hash(product_id) % 2. </code>A query for <code>product_id = 'product_id_xyz'</code> calculates <code>hash('product_id_xyz') % 10</code>, say it results in 1. The query is sent to Index Partition 1. Index Partition 1 returns the relevant <code>(customer_id, order_id)</code> pairs. Then, the system fetches the actual order data from the data partitions indicated by the <code>customer_id</code> values.</p><h4>Updating Global Secondary Indexes</h4><p>Updates to a partitioned global secondary index require coordination because multiple, potentially remote partitions must be updated atomically or consistently. Whenever a record is inserted, updated, or deleted, the corresponding entry in the global index must also be modified. This process involves two steps:</p><ol><li><p><strong>Primary Data Update</strong>: The system first updates the primary data in its partition.</p></li><li><p><strong>Index Update</strong>: The system then propagates the change to the appropriate partition of the global secondary index.</p></li></ol><p>To ensure consistency, distributed databases often use techniques such as eventual consistency or distributed transactions.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[Understanding Latency in Distributed Systems]]></title><description><![CDATA[Causes and Mitigations for Latency in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/understanding-latency-in-distributed</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/understanding-latency-in-distributed</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 04 Apr 2025 21:31:22 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3cb1fd6c-451f-4292-8444-e02efb5a0130_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Why Latency Occurs</h2><p>Latency refers to the time it takes for a request initiated by a client to travel across the network to one or more servers, be processed, and for the response to travel back to the client. It is a metric used for evaluating the responsiveness and efficiency of software systems. Several factors contribute to latency in distributed systems.</p><h4>1) Geographic Distance</h4><p>Data travels at a finite speed through network cables or wireless signals. The physical distance between components directly impacts this delay. For instance, a request from the US to a European server will inherently experience higher latency than one within the same city.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WW0v!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WW0v!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 424w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 848w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 1272w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WW0v!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png" width="1456" height="1012" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1012,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:891124,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WW0v!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 424w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 848w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.png 1272w, https://substackcdn.com/image/fetch/$s_!WW0v!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa88b082b-2bb6-4cd7-9229-5940f2a9a883_2704x1879.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><figcaption class="image-caption">Fig. Geographic Distance</figcaption></figure></div><h4>2) Network Congestion</h4><p>When network links become overloaded with traffic, data packets may experience queuing delays at routers and switches. This congestion can significantly increase latency, especially during peak usage times.</p><h4>3) Processing Delay</h4><p>Servers require time to process requests. This includes parsing the request, accessing data, performing computations, and preparing the response. Complex operations or resource-intensive tasks will naturally lead to higher processing delays</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PNSK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PNSK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 424w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 848w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 1272w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PNSK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:115587,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PNSK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 424w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 848w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 1272w, https://substackcdn.com/image/fetch/$s_!PNSK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc420602e-887c-4c16-834a-7b31eb79c65a_2169x1214.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Fig. Processing Delay</figcaption></figure></div><h4>4) I/O Delay</h4><p>Many operations in distributed systems involve reading from or writing to storage systems (like databases or disk drives). Disk I/O operations are generally slower than in-memory operations, contributing to latency.</p><h4>5) Data Handling</h4><p>When data is transmitted across a network, it often needs to be converted into a transferable format (serialization) and then back into its original format upon arrival (deserialization). These processes add to the latency, particularly for large data objects or complex data structures.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sPm3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sPm3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 424w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 848w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 1272w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sPm3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png" width="1456" height="955" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:955,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:455785,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sPm3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 424w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 848w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.png 1272w, https://substackcdn.com/image/fetch/$s_!sPm3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce300423-2a95-4af6-87d2-efba07268c00_3021x1981.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><figcaption class="image-caption">Fig. Data Handling</figcaption></figure></div><h2>How to Reduce Latency?</h2><p>Various techniques can be used to reduce latency in large-scale software systems. The choice of strategy depends on the specific scenario and the primary sources of latency.</p><h3>Network-Level Optimizations</h3><h4>1) Geo-Specific Routing</h4><p>Directing user requests to the geographically closest data center reduces network propagation delay. For example, a user in London accessing a service hosted in the US and Europe should be routed to the European data center. DNS-based routing (like <a href="https://newsletter.scalablethread.com/p/how-amazon-route-53-handles-ddos">AWS Route 53</a> Geolocation Routing) or specialized traffic management tools can be used to achieve this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bmvJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bmvJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 424w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 848w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 1272w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bmvJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png" width="1456" height="1012" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1012,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:865340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bmvJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 424w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 848w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.png 1272w, https://substackcdn.com/image/fetch/$s_!bmvJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86924a75-efbe-4c7d-96ed-d714e8685f38_2704x1879.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><figcaption class="image-caption">Fig. Geo-Specific Routing</figcaption></figure></div><h4>2) Content Delivery Networks (CDNs)</h4><p>CDNs cache static assets (images, videos, CSS) on servers distributed geographically closer to users. This reduces the need to fetch data from origin servers and minimizes network latency.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6KA8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6KA8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 424w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 848w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 1272w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6KA8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png" width="1456" height="1139" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1139,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:936974,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6KA8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 424w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 848w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.png 1272w, https://substackcdn.com/image/fetch/$s_!6KA8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F353453d7-bf32-4a36-a8b3-e6108589c8ef_2704x2115.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><figcaption class="image-caption">Fig. CDNs</figcaption></figure></div><h3>Data-Level Optimizations</h3><h4>1) Caching</h4><p><a href="https://newsletter.scalablethread.com/p/how-to-invalidate-and-evict-data">Caching </a>stores frequently accessed data in memory for faster retrieval, reducing the need for slower operations like database queries. This approach is effective for read-heavy workloads. For example, a frequently accessed user profile can be cached in memory to avoid hitting the database for every request.</p><h4>2) Data Compression and Serialization</h4><p>Reducing the size of data transmitted over the network directly reduces the time it takes to transfer. Besides that, efficient data serialization formats can also improve latency. Formats like Protocol Buffers or Apache Avro are often more compact and faster to serialize/deserialize compared to JSON or XML, especially for large data volumes.</p><h3>System-Level Optimizations</h3><h4>1) Asynchronous Systems</h4><p>In many scenarios, a user doesn't need an immediate response. Using asynchronous communication patterns, such as <a href="https://newsletter.scalablethread.com/p/how-message-queues-work">message queues</a> (like Kafka, RabbitMQ, or Amazon SQS), allows the system to process requests in the background. The user can receive an acknowledgment quickly, and the actual processing can happen later without blocking the user's interaction. This is useful for tasks like image processing or sending emails.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Abp9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Abp9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 424w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 848w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 1272w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Abp9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png" width="1456" height="431" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:431,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:73880,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Abp9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 424w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 848w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.png 1272w, https://substackcdn.com/image/fetch/$s_!Abp9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9971c1c0-23b1-46e8-8306-a95131eae402_2305x683.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><figcaption class="image-caption">Fig. Asynchronous Systems</figcaption></figure></div><h4>2) Sharding, Replication, and Load Balancing</h4><p><a href="https://newsletter.scalablethread.com/p/what-is-function-sharding-in-serverless">Horizontally partitioning the data</a> across multiple nodes can allow queries to be executed in parallel across different shards, reducing the amount of data each server needs to process. Read-heavy workloads can be <a href="https://newsletter.scalablethread.com/p/single-leader-multi-leader-and-leaderless">distributed across multiple replicas</a>, reducing the load on the primary node. Moreover, <a href="https://newsletter.scalablethread.com/p/types-of-load-balancing-algorithms">incoming requests can be distributed evenly</a> (load balancing) across multiple instances of an application server to prevent any single server from becoming overloaded.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TOUz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TOUz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 424w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 848w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 1272w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TOUz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png" width="1456" height="1098" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1098,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:604204,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TOUz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 424w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 848w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.png 1272w, https://substackcdn.com/image/fetch/$s_!TOUz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb68a4ded-297b-43f0-83bc-88968e993fa1_3995x3014.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><figcaption class="image-caption">Fig. Sharding and Replication</figcaption></figure></div><h4>3) Connection Pooling and Database Indexing</h4><p>Establishing and tearing down network connections for every request can be expensive. Connection pooling allows applications to reuse existing connections, reducing the overhead associated with connection establishment. In addition to this, indexing frequently queried database columns and avoiding full-table scans can help with quick data lookups.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ezZz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ezZz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 424w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 848w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 1272w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ezZz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png" width="1456" height="823" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:823,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:130389,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/160502330?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ezZz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 424w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 848w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.png 1272w, https://substackcdn.com/image/fetch/$s_!ezZz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3df22ea7-16fd-439b-bb4f-cd4d36d6276c_2244x1269.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><figcaption class="image-caption">Fig. Connection Pooling</figcaption></figure></div><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[Why is Cache Invalidation Hard?]]></title><description><![CDATA[Understanding the Hidden Complexity of Cache Invalidation in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/why-cache-invalidation-is-hard</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/why-cache-invalidation-is-hard</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 28 Mar 2025 21:30:57 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3b69e12b-ccfd-4be2-89f7-2e204e0a235b_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Caches are critical for optimizing performance in software applications. They reduce latency and improve scalability by storing copies of frequently accessed data closer to users or applications. For example, a global e-commerce platform might cache product catalogs to avoid repeated database queries, cutting response times from hundreds of milliseconds to microseconds. Without caching, high traffic volumes would overwhelm datastores, causing delays or outages. Caches also reduce operational costs by minimizing compute and storage load on primary systems.</p><h2>What is Cache Invalidation?</h2><p>Cache invalidation ensures that the data stored in the cache is up-to-date and consistent with the original data source. When the original data changes, the corresponding entry in the cache must be either removed or updated to reflect the change. This is crucial because if the cache contains stale data (data that is no longer accurate), users might receive incorrect information, leading to inconsistencies and potential errors in the system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PeIQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PeIQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 424w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 848w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 1272w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PeIQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png" width="1456" height="625" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:625,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87924,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159954063?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PeIQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 424w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 848w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.png 1272w, https://substackcdn.com/image/fetch/$s_!PeIQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b11ccf1-d974-428e-8c42-35149706098a_1913x821.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><figcaption class="image-caption">Fig. Write-Through Cache Invalidation</figcaption></figure></div><h2>Why is Cache Invalidation Hard?</h2><p>Cache Invalidation presents a significant challenge in large-scale distributed systems processing high volumes of data. The inherent complexities arise from various factors:</p><h4>Distributed Systems and Network Latency</h4><p>In distributed systems, data can be cached across numerous nodes. When the original data is updated, the invalidation signal must reach all the nodes holding the cached copy. This process is inherently susceptible to <a href="https://newsletter.scalablethread.com/i/146255087/the-network-is-reliable">network latency</a> and <a href="https://newsletter.scalablethread.com/i/146142578/partition-tolerance">potential failures</a>. Sending invalidation messages across a large network takes time, and there's <a href="https://newsletter.scalablethread.com/i/153244430/network-behavior-in-distributed-systems">no guarantee that all messages will be delivered on time</a>. This delay can lead to temporary inconsistencies where some users see the updated data. In contrast, others still see the stale version.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BiYi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BiYi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 424w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 848w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BiYi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png" width="1456" height="757" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/de4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:757,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:131626,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159954063?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BiYi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 424w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 848w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 1272w, https://substackcdn.com/image/fetch/$s_!BiYi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde4f403c-e63a-4eeb-bc1d-04da056fb8c7_2097x1090.png 1456w" sizes="100vw"></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><figcaption class="image-caption">Fig. Update message to node 4 lost</figcaption></figure></div><h4>Concurrency and Ordering of Operations</h4><p>In high-throughput systems, multiple read and write operations can occur concurrently. Ensuring that invalidation messages are processed in the correct order relative to data updates can be challenging. Suppose an invalidation message arrives before the update is fully committed to the primary data store. In that case, the cache might prematurely remove a non-stale entry. On the other hand, the cache will serve stale data if the update is complete before the invalidation.</p><h4>Balancing Freshness and Performance</h4><p>There's an inherent trade-off between data freshness and performance. Aggressive <a href="https://newsletter.scalablethread.com/i/147731225/cache-invalidation-strategies">invalidation strategies</a> immediately removing cached data upon any update ensure high freshness but can lead to increased load on the primary data store as caches are frequently repopulated. On the other hand, less aggressive strategies that rely on time-based expiration might improve performance by reducing the frequency of invalidation but risk serving stale data for longer periods.</p><h4>Cache Size Limitations and Eviction Policies</h4><p>Caches have finite storage capacity. When a cache reaches its limit, it needs to evict some entries to make space for new ones. The <a href="https://newsletter.scalablethread.com/i/147731225/what-is-cache-eviction">eviction policy</a> (e.g., <a href="https://newsletter.scalablethread.com/i/147731225/least-recently-used-lru">Least Recently Used</a>, <a href="https://newsletter.scalablethread.com/i/147731225/least-frequently-used-lfu">Least Frequently Used</a>) can inadvertently remove data that is still valid and might be requested again soon, leading to cache misses and increased load on the primary data store.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6tkD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6tkD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 424w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 848w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 1272w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6tkD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png" width="991" height="324" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:324,&quot;width&quot;:991,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&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;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6tkD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 424w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 848w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.png 1272w, https://substackcdn.com/image/fetch/$s_!6tkD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87af4e9e-91b4-4768-8af5-49129e02988e_991x324.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><figcaption class="image-caption">Fig. Least Frequently Used Cache Eviction</figcaption></figure></div><h4>False Sharing</h4><p>False sharing is a performance issue in multi-core processors where two or more data items reside within the same cache line. When one core modifies a data item, the entire cache line is invalidated for other cores, even if they accessed different data items within the same line, which degrades performance. For example, two variables stored contiguously in memory might share a cache line. Updating one variable invalidates the entire line, even if the other remains unchanged.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ginX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ginX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 424w, https://substackcdn.com/image/fetch/$s_!ginX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 848w, https://substackcdn.com/image/fetch/$s_!ginX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 1272w, https://substackcdn.com/image/fetch/$s_!ginX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ginX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png" width="1456" height="793" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:793,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:189462,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159954063?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ginX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 424w, https://substackcdn.com/image/fetch/$s_!ginX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 848w, https://substackcdn.com/image/fetch/$s_!ginX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.png 1272w, https://substackcdn.com/image/fetch/$s_!ginX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F292744b1-ddf6-447f-81f2-04d5eeddc160_2483x1352.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><figcaption class="image-caption">Fig. False Shring</figcaption></figure></div><h4>Resource Constraints and Cost Considerations</h4><p>Implementing sophisticated cache invalidation mechanisms can be resource-intensive. Maintaining metadata about cached entries, tracking dependencies, and reliably propagating invalidation messages require computational resources, network bandwidth, and storage. The cost of implementing and maintaining these mechanisms must be carefully considered, especially in large-scale systems where even small inefficiencies can have a significant impact.</p><h4>Predicting Future Access Patterns</h4><p>An ideal invalidation strategy would proactively invalidate data that is likely to become stale or is unlikely to be accessed again soon. However, accurately predicting future access patterns is a complex problem. Relying on heuristics or historical data might not always be effective in dynamic environments where access patterns can change rapidly. For example, a video streaming service might prefetch content based on user behavior, but unexpected viral trends could render predictions obsolete,<a href="https://newsletter.scalablethread.com/p/how-to-avoid-cache-stampede-or-dogpile"> flooding caches with irrelevant data</a>.</p><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[Understanding Faults and Fault Tolerance in Distributed Systems]]></title><description><![CDATA[How Things Can Break in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/understanding-faults-and-fault-tolerance</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/understanding-faults-and-fault-tolerance</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 21 Mar 2025 21:30:54 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/840caf39-5397-4f9b-ab91-d242ffbbb1d6_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Software applications rely on distributed systems for data storage, computation, and real-time processing. These systems spread workloads across multiple nodes (servers, databases, or services) to improve scalability and availability. However, distributed systems face a critical challenge: faults. Their inherent complexity makes them susceptible to various types of failures. Fault tolerance is the ability of a system to continue operating correctly despite faults in some of its components.</p><h2><strong>What Are Faults and Why Do They Occur?</strong></h2><p>A <em>fault</em> is a defect in a system component that can lead to an <em>error</em> (incorrect state) and, eventually, a <em>failure</em> (deviation from expected behavior). Faults in distributed systems can be categorized into several types:</p><ul><li><p><strong>Hardware Faults</strong>: Physical failures like disk crashes, RAM corruption, or power outages.</p><ul><li><p>Example: A server in a cloud cluster loses network connectivity due to a faulty router.</p></li></ul></li><li><p><strong>Software Faults</strong>: Bugs in code, race conditions, or memory leaks.</p><ul><li><p>Example: A microservice crashes after encountering an unhandled null pointer exception.</p></li></ul></li><li><p><strong>Human Errors</strong>: Misconfigurations, accidental data deletions, or flawed deployment scripts.</p><ul><li><p>Example: A developer deploys a buggy update that corrupts a distributed cache.</p></li></ul></li><li><p><strong>Network Faults</strong>: Packet loss, latency spikes, or partitions (split-brain scenarios).</p><ul><li><p>Example: A database replica in Europe becomes unreachable due to an undersea cable cut.</p></li></ul></li><li><p><strong>Environmental Faults</strong>: Natural disasters, power grid failures, or overheating data centers.</p></li><li><p><strong>Byzantine Faults:</strong> These are the faults where a component behaves arbitrarily, potentially <a href="https://newsletter.scalablethread.com/p/what-is-the-byzantine-generals-problem">sending conflicting or malicious information</a> to different parts of the system. This can be due to hardware errors, software bugs, or even malicious attacks.</p></li></ul><h2><strong>Impact of Faults</strong></h2><p>When faults occur in a distributed system, they can impact the overall system functionality and user experience.</p><ul><li><p><strong>Data Loss:</strong> A critical impact of hardware failures, especially disk drive failures, can be the permanent loss of valuable data if proper <a href="https://newsletter.scalablethread.com/p/single-leader-multi-leader-and-leaderless">replication </a>mechanisms are not in place. Software bugs leading to incorrect data updates or deletions can also result in data loss.</p></li><li><p><strong>Service Unavailability:</strong> If a critical component of the system fails, its service might become unavailable to users.</p></li><li><p><strong>Performance Degradation:</strong> Faults can lead to a decrease in performance. For example, network delays can slow communication between components, and failed nodes might need to be bypassed or their workload redistributed, adding overhead to the remaining nodes.</p></li><li><p><strong>Inconsistent State:</strong> Faults, particularly <a href="https://newsletter.scalablethread.com/i/146142578/partition-tolerance">network partitions</a>, can lead to inconsistent views of the data or the system's state among different parts of the system.</p></li><li><p><strong>Security Breaches:</strong> In some cases, faults can be exploited by malicious actors to compromise the system's security.</p></li></ul><h2>How to Achieve Fault Tolerance?</h2><h4>Replication</h4><p>This involves creating multiple copies of critical data or components across different nodes. If one replica fails, others can take over, ensuring continued availability and data durability.</p><ul><li><p><strong>Active Replication:</strong> All replicas process incoming requests concurrently. This provides low latency failover but requires careful coordination to ensure consistency.<strong> </strong>For example, a load balancer distributes incoming user requests to multiple identical application servers. If one server fails, the load balancer simply directs traffic to the remaining healthy servers.<br></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-G8f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-G8f!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 424w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 848w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 1272w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-G8f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png" width="528" height="748" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:748,&quot;width&quot;:528,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41946,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!-G8f!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 424w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 848w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.png 1272w, https://substackcdn.com/image/fetch/$s_!-G8f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5f2099f-cefc-40c6-9e7e-a63fdcc2af13_528x748.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><figcaption class="image-caption">Fig. Active Replication - Node Failover</figcaption></figure></div></li><li><p><strong>Passive Replication (Standby):</strong> One replica is designated as the primary, handling all requests. Secondary replicas remain idle and are activated only when the primary fails. This may have a higher failover latency. For example, a primary database server handles all write operations, while a secondary server continuously receives updates. If the primary fails, the secondary is promoted to become the new primary.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!a8c1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!a8c1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 424w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 848w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 1272w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!a8c1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png" width="1284" height="628" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:628,&quot;width&quot;:1284,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:84042,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!a8c1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 424w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 848w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.png 1272w, https://substackcdn.com/image/fetch/$s_!a8c1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbacafdd9-d80b-483a-b6e9-5baf6cc1d6c6_1284x628.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><figcaption class="image-caption">Fig. Passive Replication - Database Failover</figcaption></figure></div></li></ul><h4>Failure Detection and Recovery</h4><p>This includes having mechanisms to detect when a component has failed.</p><ul><li><p><strong>Heartbeats:</strong> Periodic messages sent by a component to indicate its health. The absence of a heartbeat within a certain timeout period indicates a failure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-IeA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-IeA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 424w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 848w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 1272w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-IeA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png" width="567" height="529" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e99e5283-4841-4d70-822f-5d8e902f6add_567x529.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:529,&quot;width&quot;:567,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30265,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-IeA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 424w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 848w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.png 1272w, https://substackcdn.com/image/fetch/$s_!-IeA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99e5283-4841-4d70-822f-5d8e902f6add_567x529.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><figcaption class="image-caption">Fig. Heartbeats</figcaption></figure></div></li><li><p><strong>Checkpointing: </strong>Periodically saving the state of a system or component (checkpointing) allows for recovery from failures by restoring the last saved state.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JzsX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JzsX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 424w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 848w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 1272w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JzsX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png" width="849" height="704" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c8196ff3-4583-4c22-9c3c-04963280b066_849x704.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:704,&quot;width&quot;:849,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:65659,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JzsX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 424w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 848w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.png 1272w, https://substackcdn.com/image/fetch/$s_!JzsX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8196ff3-4583-4c22-9c3c-04963280b066_849x704.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><figcaption class="image-caption">Fig. Checkpointing</figcaption></figure></div></li></ul><h4>Retries and Timeouts</h4><ul><li><p><strong>Exponential Backoff</strong>: Retry failed requests with <a href="https://newsletter.scalablethread.com/i/147056244/exponential-jitter-and-retry">increasing delays (jitter)</a> to avoid overwhelming the system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gFi2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gFi2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 424w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 848w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 1272w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gFi2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png" width="1456" height="1056" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1056,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:255038,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gFi2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 424w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 848w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.png 1272w, https://substackcdn.com/image/fetch/$s_!gFi2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d285c6-d78b-4f85-bd95-38fb5ac7e49f_2420x1755.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><figcaption class="image-caption">Fig. Exponential Jitter and Retry</figcaption></figure></div></li><li><p><strong>Circuit Breakers</strong>: Temporarily <a href="https://newsletter.scalablethread.com/i/147056244/circuit-breaker">block requests to a failing service</a> or component until it recovers and is ready to handle traffic again.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XLys!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XLys!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 424w, https://substackcdn.com/image/fetch/$s_!XLys!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 848w, https://substackcdn.com/image/fetch/$s_!XLys!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 1272w, https://substackcdn.com/image/fetch/$s_!XLys!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XLys!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png" width="1456" height="1238" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1238,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:227649,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XLys!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 424w, https://substackcdn.com/image/fetch/$s_!XLys!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 848w, https://substackcdn.com/image/fetch/$s_!XLys!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.png 1272w, https://substackcdn.com/image/fetch/$s_!XLys!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8be1c941-fad2-4d04-9aa8-9f87e7549493_1958x1665.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><figcaption class="image-caption">Fig. Circuit Breaker</figcaption></figure></div><p></p></li><li><p><strong>Timeouts:</strong> Setting time limits for expected responses. The sender assumes a failure if a response is not received within the timeout.</p></li></ul><h4>Idempotency and Quorum</h4><ul><li><p><strong>Idempotency:</strong> Designing operations that can be executed multiple times without unintended side effects. This is particularly important when dealing with potential message loss or retries in distributed systems.</p></li><li><p><strong>Quorum Systems</strong>: Require a <a href="https://newsletter.scalablethread.com/i/146489184/leaderless-replication">majority of nodes to agree</a> before committing changes. This ensures that the system can tolerate a certain number of failures while still maintaining <a href="https://newsletter.scalablethread.com/i/146489166/consistency-and-consistency-model">consistency</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9GO9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9GO9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 424w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 848w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 1272w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9GO9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png" width="680" height="516" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:516,&quot;width&quot;:680,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25686,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/159551346?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9GO9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 424w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 848w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.png 1272w, https://substackcdn.com/image/fetch/$s_!9GO9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb262d1d2-7ac5-481c-acb4-6d1cb88a81d4_680x516.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><figcaption class="image-caption">Fig. Leaderless Replication &#8212; Client sends updates to all five nodes and considers success if any three of them acknowledge (Quorum)</figcaption></figure></div></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[How to Streamline Data Access With Valet Key Pattern?]]></title><description><![CDATA[Bypassing Intermediary Services and Improving Data Access Efficiency]]></description><link>https://newsletter.scalablethread.com/p/how-to-streamline-data-access-with</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/how-to-streamline-data-access-with</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 14 Mar 2025 21:30:38 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e9fdb274-7b87-4018-b45a-3f27d4c3457f_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Software applications often rely on intermediaries (like servers) to manage data streaming, sharing, and storage. When a client needs to access data, it requests it from this service. The service, in turn, retrieves the data from its storage location and forwards it to the client. For example, a cloud service might handle user file uploads by receiving data on its servers and then forwarding it to long-term storage. This approach creates several problems:</p><ul><li><p><strong>Increased Latency:</strong> The extra hop through the intermediary service adds latency to the data retrieval.</p></li><li><p><strong>Resource Consumption:</strong> The intermediary service consumes compute, memory, and network bandwidth to handle data requests. </p></li><li><p><strong>Scalability Issues:</strong> As the number of clients and data requests increase, the intermediary service may become overloaded, leading to performance degradation.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tSm2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tSm2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 424w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 848w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 1272w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tSm2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png" width="1456" height="325" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:325,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:115511,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158990903?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tSm2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 424w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 848w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 1272w, https://substackcdn.com/image/fetch/$s_!tSm2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c9f7fc0-5a7b-4624-b5aa-e37349c0e9e5_2812x628.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">Fig. Accessing data through intermediary service</figcaption></figure></div><h2>What is the Valet Key Pattern?</h2><p>The Valet Key pattern addresses the challenge of securely providing temporary, direct access to data stored in a system without requiring the data to pass through an intermediary service. It uses short-lived tokens (<em>valet keys </em>) to grant clients temporary, direct access to specific resources (e.g., cloud storage buckets or files). Here's how it works:</p><ul><li><p><strong>Client Requests Access</strong>: The client asks the application for permission to read/write data.</p></li><li><p><strong>Authorization:</strong> The intermediary service (the "key issuer") authorizes the client to access specific data.</p></li><li><p><strong>Key Generation:</strong> The key issuer generates a "valet key" (a security token) that grants the client temporary, restricted access to the data. This key includes information such as:</p><ul><li><p>The location of the data.</p></li><li><p>The permissions granted to the client (e.g., read, write).</p></li><li><p>The expiration time of the key.</p></li></ul></li><li><p><strong>Key Delivery:</strong> The key issuer securely delivers the valet key to the client.</p></li><li><p><strong>Direct Access:</strong> The client uses the valet key to access the data storage system directly, bypassing the intermediary service.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ztxz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ztxz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 424w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 848w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 1272w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ztxz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png" width="1456" height="886" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:886,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:215874,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158990903?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ztxz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 424w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 848w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 1272w, https://substackcdn.com/image/fetch/$s_!ztxz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F780c44f2-ab84-4ac8-85a0-036e294262b8_2440x1484.png 1456w" sizes="100vw"></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><figcaption class="image-caption">Fig. Valet Key Pattern</figcaption></figure></div><p>The Valet Key Pattern also allows precise control over data access by enabling granular scoping and revocation mechanisms. Valet keys can be configured to restrict access to specific operations (e.g., read-only, write), files, directories, or even time windows (e.g., valid for 10 minutes). For example, a key might grant temporary upload access to a single folder in cloud storage but block deletions or modifications. Cloud providers like AWS S3 or Azure Blob Storage support these features through pre-signed URLs or SAS tokens, which enforce these limits at the storage layer.</p><h2>Security Implications</h2><p>The Valet Key pattern also introduces some security considerations:</p><ul><li><p><strong>Key Security:</strong> The entire system's security relies on the valet keys' security. Keys must be generated and delivered securely to prevent unauthorized access.</p></li><li><p><strong>Key Expiration:</strong> It is crucial to enforce key expiration to limit the window of opportunity for attackers.</p></li><li><p><strong>Permissions Management:</strong> Carefully manage the permissions granted to each valet key to prevent excessive access.</p></li><li><p><strong>Key Revocation:</strong> The system must provide a mechanism to revoke valet keys in case of compromise.</p></li><li><p><strong>Secure Key Issuing:</strong> The service responsible for generating the valet keys must be very well secured, as it would be a valuable target for attackers.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[What is the Claim-Check Pattern in Event-Driven Systems?]]></title><description><![CDATA[Understanding How to Handle Large & Sensitive Payloads in Distributed Systems]]></description><link>https://newsletter.scalablethread.com/p/what-is-the-claim-check-pattern-in</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/what-is-the-claim-check-pattern-in</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 07 Mar 2025 22:30:29 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/c8b56665-3043-4e59-8f1e-d7c1a24462a9_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In distributed systems, communication between services often relies on message passing. Messages are sent between components to trigger actions, share data, or synchronize states. However, as systems grow in complexity, the size of these messages can become a significant challenge. Transmitting messages containing large payloads directly can overwhelm the message broker, consume excessive bandwidth, and slow down processing. Additionally, some message brokers have size limits for messages, making it impossible to send large payloads directly.</p><h2>What is the Claim-Check Pattern?</h2><p>The Claim-Check Pattern is a messaging design pattern that separates the transmission of large data payloads from the actual message. Instead of sending the entire payload within the message, the pattern stores the payload in an external storage system (e.g., a database, blob storage, or file system) and sends only a reference to the stored data in the message. This reference is often called a "claim check," akin to a ticket you receive at a coat check. This is achieved through a multi-step process:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4OWU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4OWU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 424w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 848w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 1272w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4OWU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png" width="1456" height="848" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:848,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:250174,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158255679?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4OWU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 424w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 848w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.png 1272w, https://substackcdn.com/image/fetch/$s_!4OWU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee38bb12-c213-42b0-90f0-bb8acf8210c0_2770x1613.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><figcaption class="image-caption">Fig. The Claim-Check Pattern</figcaption></figure></div><ul><li><p><strong>Store the Payload</strong>: When a service needs to send a large message, it first uploads the payload to an external storage system. This could be a cloud storage service like Amazon S3, Azure Blob Storage, or a distributed file system.</p></li><li><p><strong>Generate a Reference</strong>: Once the payload is stored, the service or database generates a unique reference (e.g., a URL, file path, or database key) that points to the stored data.</p></li><li><p><strong>Send the Claim Check</strong>: Instead of sending the entire payload, the sender service sends a lightweight message containing only the reference (claim check) and any necessary metadata.</p></li><li><p><strong>Retrieve the Payload</strong>: The receiving service uses the claim check to fetch the payload from the external storage system when needed.</p></li></ul><h2>Claim-Check with Event Sourcing for Privacy</h2><p><a href="https://newsletter.scalablethread.com/p/what-is-event-sourcing">Event Sourcing</a> is a pattern in which an application's state is determined by a sequence of events. However, storing all information directly in events can raise privacy concerns when dealing with sensitive data. Claim-Check improves privacy within Event Sourcing by ensuring sensitive data is not directly stored within the event stream.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B6UC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B6UC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 424w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 848w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 1272w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B6UC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png" width="1456" height="1749" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1749,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:354345,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158255679?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B6UC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 424w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 848w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 1272w, https://substackcdn.com/image/fetch/$s_!B6UC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9eb99d7-14d7-4944-81a8-a508ff2d3f6e_2203x2647.png 1456w" sizes="100vw"></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><figcaption class="image-caption">Fig.  The Claim Check Pattern with Event Sourcing</figcaption></figure></div><ul><li><p><strong>Storing Sensitive Data Separately: </strong>Instead of embedding sensitive data (e.g., personally identifiable information or PII) directly in events, store it in a secure external storage system.</p></li><li><p><strong>Generating Claim Check for Sensitive Data:</strong> Include only a claim check (reference) in the event log. This ensures that sensitive data is not exposed in the event stream.</p></li><li><p><strong>Access Control for Payload Retrieval:</strong> Implement strict access controls on the external storage system to ensure that only authorized services or users can retrieve the sensitive data.</p></li><li><p><strong>Audit Trail: </strong>Event sourcing naturally provides an audit trail of events. Combined with access logs for the storage location, a complete audit trail of data access is maintained.</p></li><li><p><strong>Data Retention Policies: </strong>Apply data retention policies to the external storage system to automatically delete or anonymize sensitive data after a specified period, ensuring compliance with privacy regulations like GDPR.</p></li><li><p><strong>Data Encryption:</strong> Sensitive data can be encrypted before storage, providing an additional layer of security.</p></li></ul><h2>Pros and Cons</h2><h4>Pros</h4><ul><li><p><strong>Reduced Message Size</strong>: By storing large payloads externally, messages become lightweight, improving performance and reducing bandwidth usage.</p></li><li><p><strong>Enhanced Security:</strong> Sensitive data can be stored separately and accessed with controlled authorization. Separate retention policies can be applied to data and metadata.</p></li><li><p><strong>Increased Scalability:</strong> Smaller messages reduce the load on message brokers, enabling the system to handle higher message volumes.</p></li><li><p><strong>Cost Efficiency</strong>: Storing large payloads in cost-effective storage systems (e.g., object storage) can be cheaper than transmitting them through a message broker.</p></li></ul><h4>Cons</h4><ul><li><p><strong>Increased Complexity</strong>: The pattern introduces additional components (e.g., external storage) and steps (e.g., uploading and retrieving payloads), which can complicate the system.</p></li><li><p><strong>Latency Overhead</strong>: Fetching payloads from external storage adds latency, which may not be acceptable in low-latency systems.</p></li><li><p><strong>Dependency on External Storage</strong>: The system becomes dependent on the availability/performance of the external storage system. If the storage system becomes unavailable, messages cannot be fully processed.</p></li><li><p><strong>Potential for Orphaned Data:</strong> If the claim check is lost or not processed, the stored data may become orphaned, consuming storage space without purpose.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[What is Command Query Responsibility Segregation (CQRS)?]]></title><description><![CDATA[Understanding How Separating Reads and Writes Can Improve Performance]]></description><link>https://newsletter.scalablethread.com/p/what-is-command-query-responsibility</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/what-is-command-query-responsibility</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 28 Feb 2025 22:30:41 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4db8dbec-3b87-4c99-8cb2-e540ef30f025_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A data model defines how data is organized and represented in a database. In traditional CRUD application architectures a single data model is used for both reading and writing data. As applications scale, this approach leads to performance bottlenecks and complexity. Read operations optimized for retrieval may clash with write operations optimized for <a href="https://newsletter.scalablethread.com/p/how-transaction-isolation-provides">data integrity</a>. Increased load can degrade responsiveness for both types of operations. Complex domain models further complicate the issue, making optimizing for both read and write scenarios challenging.</p><h2>What is the CQRS Pattern?</h2><p>The Command Query Responsibility Segregation (CQRS) pattern is a design approach that separates a data store's read and write operations into distinct models. The core principle is to use separate interfaces for querying data (reads) and commanding data (writes). The command model handles create, update, and delete operations, focusing on data integrity and business logic. The query model handles data retrieval and is optimized for fast reads. This separation allows for</p><ol><li><p><strong>Optimized Performance</strong>: Read and write operations can be tuned separately, improving overall performance.</p></li><li><p><strong>Independent Scaling</strong>: Read and write databases can be scaled independently based on their specific load requirements.</p></li></ol><h2><strong>CQRS on Single Database Systems</strong></h2><p>In a single database system, the command model may utilize a normalized database schema optimized for writes, and the query model may use denormalized data, or materialized views for performance enhancements. Data synchronization between the command and query models can be done synchronously or asynchronously. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jx87!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jx87!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 424w, https://substackcdn.com/image/fetch/$s_!jx87!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 848w, https://substackcdn.com/image/fetch/$s_!jx87!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 1272w, https://substackcdn.com/image/fetch/$s_!jx87!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jx87!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png" width="1456" height="759" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:759,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:194638,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158035947?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jx87!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 424w, https://substackcdn.com/image/fetch/$s_!jx87!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 848w, https://substackcdn.com/image/fetch/$s_!jx87!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.png 1272w, https://substackcdn.com/image/fetch/$s_!jx87!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb57ee391-8b45-4530-ae13-e25441a73407_2389x1246.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><figcaption class="image-caption">Fig. CQRS on Single Database Systems</figcaption></figure></div><p>Consider a micro-blogging application with a single "Posts" table.</p><h4><strong>Traditional Model:</strong></h4><ul><li><p>When a user creates a post, the system writes to the "Posts" table.</p></li><li><p>When a user views a post, the system reads from the same "Posts" table.</p></li><li><p>If the site becomes very popular, both read and write operations compete for resources, potentially slowing down the system.</p></li></ul><h4><strong>CQRS Model:</strong></h4><ul><li><p><strong>Write Model:</strong> The "Posts" table remains optimized for writes. It ensures data consistency when creating or updating posts.</p></li><li><p><strong>Read Model:</strong> A separate "PublishedPostsView" can be created. This view can use the "Posts" table to pre-calculate summaries or format the content for display. When a user views a post, the system reads from this optimized view. This view might not contain all the details of the original post, but only the data needed for display.</p></li><li><p>Another read model might be created for search functionality, containing indexed terms extracted from the post content.</p></li><li><p>When a new post is created, the write model updates the "Posts" table and then asynchronously updates the "PublishedPostsView" and the search index.</p></li></ul><p>Both operations utilize the same underlying database. However, their access patterns and data structures are logically separated, improving system responsiveness.</p><h2><strong>CQRS on Multi-Database Systems</strong></h2><p>Implementing CQRS on multiple databases involves using separate databases for read and write operations. This approach is suitable for applications that require high scalability and can tolerate <a href="https://newsletter.scalablethread.com/i/146489166/eventual-consistency-model">eventual consistency</a>. The command model might use a relational database for <a href="https://newsletter.scalablethread.com/p/how-transaction-isolation-provides">transactional integrity</a>, while the query model might use a NoSQL database for scalability and flexibility. This separation allows each database to be selected and configured based on its specific workload requirements, addressing the limitations of a single database handling diverse operations. Data synchronization between databases is typically asynchronous.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9yJ4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9yJ4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 424w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 848w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 1272w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9yJ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png" width="1456" height="1462" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1462,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:214840,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/158035947?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9yJ4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 424w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 848w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.png 1272w, https://substackcdn.com/image/fetch/$s_!9yJ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e7a21b0-4fe2-4f3f-9303-93254a5ea131_1989x1997.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><figcaption class="image-caption"><strong>CQRS on Multi-Database Systems</strong></figcaption></figure></div><p>For example, an e-commerce platform might use a relational database for order processing (commands) and a NoSQL database for product catalog queries. When a customer places an order, the relational database handles the transaction. Product details for display are retrieved from the NoSQL database, which is optimized for fast, scalable reads. Changes to product information in the relational database are propagated to the NoSQL database asynchronously.</p><h4>CQRS and Event Sourcing</h4><p><a href="https://newsletter.scalablethread.com/p/what-is-event-sourcing">Event Sourcing</a> is often used in conjunction with CQRS. The command model stores events in an event store, while the query model builds read models by replaying events from the event store. Eventual consistency is inherent in this approach.</p><h2><strong>Pros and Cons</strong></h2><h4>Pros</h4><p>Apart from the ones already stated above,</p><ul><li><p><strong>Increased Flexibility:</strong> Each model can use different database technologies and data models, providing flexibility and adaptability.</p></li><li><p><strong>Improved Maintainability:</strong> Separating concerns makes the codebase easier to understand and maintain.</p></li></ul><h4>Cons</h4><ul><li><p><strong>Eventual Consistency:</strong> Asynchronous synchronization can lead to eventual consistency, which may not be suitable for all applications.</p></li><li><p><strong>Data Duplication:</strong> Read models often involve data duplication, which can increase storage requirements.</p></li><li><p><strong>Infrastructure Overhead:</strong> Multiple databases and messaging systems may increase infrastructure costs.</p></li><li><p><strong>Synchronization Complexity:</strong> Ensuring data consistency between the read and write sides can be difficult.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[What is Saga Pattern in Distributed Systems?]]></title><description><![CDATA[Understanding How Microservices Coordinate Distributed Transactions]]></description><link>https://newsletter.scalablethread.com/p/what-is-saga-pattern-in-distributed</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/what-is-saga-pattern-in-distributed</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 21 Feb 2025 22:30:49 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/0f97bee0-319c-4ae4-8712-b1328a0b8984_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In distributed systems, microservices provide ways for building scalable and resilient applications. These applications often require communication among multiple services to process a single request. For example, a single request, like ordering a product, may involve multiple steps and services: inventory, payment, shipping, etc. This distributed nature makes ensuring <a href="https://newsletter.scalablethread.com/i/146489166/consistency-and-consistency-model">data consistency</a> and atomicity a complex challenge, especially when each service has its own database and operates independently. How do you guarantee that if the payment service succeeds, the inventory is updated, and the shipping process is initiated while maintaining data integrity? Traditional ACID transactions, designed for monolithic applications with a single database, don't work well across multiple services.</p><h2>What is the Saga Pattern?</h2><p>The Saga pattern is a design pattern that helps manage transaction updates across multiple services by breaking them down into a sequence of small local transactional updates, called "saga steps" or "subtransactions." Each step represents a unit of work that interacts with a single service. Once a step is completed, it triggers the next step in the sequence. If any step fails, the saga executes compensating updates to undo the changes made by the previous steps, ensuring that the system returns to its initial state.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BJeH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BJeH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 424w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 848w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 1272w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BJeH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png" width="1456" height="604" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:604,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:232032,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/157438552?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BJeH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 424w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 848w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.png 1272w, https://substackcdn.com/image/fetch/$s_!BJeH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2c90ec68-13fb-428a-a0f1-6d76ee34749a_3245x1346.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><figcaption class="image-caption">Fig. Saga Pattern</figcaption></figure></div><h2><strong>Types of Saga Approaches</strong></h2><p>There are two main approaches to implementing the Saga Pattern: Orchestration and Choreography.</p><h3><strong>Orchestration</strong></h3><p>In this approach, a central orchestrator service coordinates the saga steps. The orchestrator tells each service when to execute its local transaction. It maintains the state of the saga and handles any failures by invoking compensating transactions. The orchestrator knows the entire saga flow.</p><h4>How it works</h4><p>The client initiates the saga by communicating with the orchestrator. The orchestrator then invokes the first service. Upon successful completion, the orchestrator moves to the next step, invoking the corresponding service. If a service fails, the orchestrator triggers compensating transactions in reverse order.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ejvK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ejvK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 424w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 848w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 1272w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ejvK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3952805-6a2d-447e-801a-948121d0471b_3083x2312.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:352758,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/157438552?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ejvK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 424w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 848w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.png 1272w, https://substackcdn.com/image/fetch/$s_!ejvK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3952805-6a2d-447e-801a-948121d0471b_3083x2312.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><figcaption class="image-caption">Fig. Orchestration Approach</figcaption></figure></div><h3><strong>Choreography</strong></h3><p>In the Choreography approach, there is no central coordinator. Instead, each service involved in the saga knows its role and communicates with the other services through events or messages. Each service listens for specific events and performs local transactions when the appropriate event is received. The saga flow is distributed across the services.</p><h4>How it works</h4><p>The client initiates the saga by communicating with the first service. This service performs its transaction and publishes an event. Other services, listening for this event, perform their respective transactions and publish their events. This chain reaction continues until the saga is complete. If a service fails, it publishes a compensating event, triggering other services to execute compensating transactions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!g4aE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!g4aE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 424w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 848w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 1272w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!g4aE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png" width="1456" height="806" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:806,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:310399,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://newsletter.scalablethread.com/i/157438552?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!g4aE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 424w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 848w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.png 1272w, https://substackcdn.com/image/fetch/$s_!g4aE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8182ff0f-31e8-4434-bc15-b0f363afc914_3649x2021.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><figcaption class="image-caption">Fig. Choreography Approach</figcaption></figure></div><h3><strong>Orchestration v/s Choreography</strong></h3><ul><li><p>Choreography has no single point of failure, as each service manages its part of the saga.</p></li><li><p>Orchestration provides simplified error handling and monitoring with centralized control. In contrast, each service needs to handle its errors in Choreography, which can lead to complex error-handling logic.</p></li><li><p>In Orchestration, the coordinator needs to know about all the services involved in the saga, which can lead to tight coupling. In contrast, in Choreography, services need to agree on the events and the order of transactions, which can lead to overhead in coordination.</p></li></ul><h3><strong>Pros and Cons of Saga Pattern</strong></h3><h4>Pros</h4><ul><li><p><strong>Data consistency</strong>: The Saga Pattern ensures data consistency across multiple services, even in the face of failures.</p></li><li><p><strong>Scalability</strong>: It allows for scalable and loosely coupled services, as each service can operate independently.</p></li><li><p><strong>Flexibility</strong>: The Saga Pattern can handle complex business transactions that involve multiple services and steps.</p></li></ul><h4>Cons</h4><ul><li><p><strong>Complexity</strong>: Implementing the Saga Pattern can be complex, especially for large-scale systems with many services.</p></li><li><p><strong>Error handling</strong>: Managing errors and compensating transactions can be challenging, especially in the choreography approach.</p></li><li><p><strong>Performance overhead</strong>: The need to track the saga's state and handle compensating transactions can introduce performance overhead.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item><item><title><![CDATA[What is Event Sourcing?]]></title><description><![CDATA[Understanding Event Sourcing, How it works, and How to Rebuild State]]></description><link>https://newsletter.scalablethread.com/p/what-is-event-sourcing</link><guid isPermaLink="false">https://newsletter.scalablethread.com/p/what-is-event-sourcing</guid><dc:creator><![CDATA[Sid]]></dc:creator><pubDate>Fri, 14 Feb 2025 22:30:23 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/12fe063e-5703-4a3d-bc05-5df768f5aea2_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Event Sourcing</h2><p>Traditional data storage typically focuses on the current state of an entity. For example, in an e-commerce system, you might store the current state of a customer's order: items, quantities, shipping address, etc. Event sourcing takes a different approach. Instead of storing the current state directly, it stores the events that led to that state. Each event represents a fact that happened in the past. Think of it as a detailed log of transactions on your bank statement. These events are immutable and stored in an append-only event store. The core idea is that an application's state can be derived by replaying events in the order they occurred, just like you can get your current bank balance by replaying all the transactions from the beginning. This makes Event Sourcing particularly useful for applications that require a high degree of audibility and traceability.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1dQp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1dQp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 424w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 848w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 1272w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1dQp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png" width="1456" height="897" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:897,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:157107,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1dQp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 424w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 848w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.png 1272w, https://substackcdn.com/image/fetch/$s_!1dQp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F825b2776-19f8-48d5-b906-df6d10ad2c54_2598x1601.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><figcaption class="image-caption">Fig. Event Sourcing</figcaption></figure></div><h2><strong>How Event Sourcing Works</strong></h2><p>Every change to the application state is captured as an event object in an Event Sourcing system. These events are then stored in an event store, a database optimized for handling event data. Here's a step-by-step breakdown of how Event Sourcing works:</p><ol><li><p><strong>Event Creation</strong>: When a user performs an action (e.g., placing an order), the application creates an event object that describes the action.</p></li><li><p><strong>Event Storage</strong>: The event is stored in the event store. This store is append-only, meaning events can only be added and never modified or deleted.</p></li><li><p><strong>State Rebuilding</strong>: To determine the current state of an entity (e.g., an order), the application replays all the events related to that entity from the event store.</p></li></ol><p>Imagine an e-commerce application. Instead of storing the current state of a shopping cart (items, quantities, etc.), we store events like "<em>ItemAddedToCart</em>," "<em>ItemRemovedFromCart</em>," "<em>QuantityUpdated</em>," etc. Each event represents a specific change to the cart. The current state of the cart can be reconstructed at any time by replaying these events in order.</p><h2><strong>Rebuilding State in Event Sourcing</strong></h2><p>Reconstructing the state from events involves reading all the events related to an entity from the event store and applying them in sequence to reconstruct the current state. It's like simulating all the changes that have occurred to construct the current state. For example, consider an e-commerce application where an order goes through various states like "Created," "Paid," and "Shipped." To determine the current state of an order, you would:</p><ol><li><p>Retrieve all events related to the order from the event store.</p></li><li><p>Initialize an empty order object.</p></li><li><p>Apply each event to the order object in the order in which they were stored.</p></li></ol><p>By the end of this process, the order object will reflect the current state of the order.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jBSy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jBSy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 424w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 848w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 1272w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jBSy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png" width="1456" height="1584" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1584,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:189378,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jBSy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 424w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 848w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.png 1272w, https://substackcdn.com/image/fetch/$s_!jBSy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bf36752-8f52-4182-a20b-9e64a3e56565_2095x2279.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><figcaption class="image-caption">Fig. Rebuilding final state from events</figcaption></figure></div><h4><strong>Use of Snapshots in Rebuilding State</strong></h4><p>As the number of events grows, replaying the entire event stream to reconstruct the state can become slow and inefficient. This is where snapshots come in. A snapshot is a saved state of an entity at a specific point in time. Instead of replaying all events from the beginning, the application can load the latest snapshot and then replay only the events that occurred after the snapshot was taken. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oDqV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oDqV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 424w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 848w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 1272w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oDqV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png" width="1456" height="1146" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1146,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:140252,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oDqV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 424w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 848w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.png 1272w, https://substackcdn.com/image/fetch/$s_!oDqV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb46fe5e6-0972-46ad-8648-38689de661b1_2112x1663.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><figcaption class="image-caption">Fig. Rebuilding Final State From Snapshot and Events</figcaption></figure></div><h2><strong>Pros and Cons</strong></h2><h4>Pros</h4><ul><li><p><strong>Auditability</strong>: Event Sourcing provides a complete history of changes, making it easy to audit and trace an entity's state over time.</p></li><li><p><strong>Debugging</strong>: The ability to replay events makes it easier to debug issues by reproducing the exact state of an entity at any point in time.</p></li><li><p><strong>Flexibility</strong>: Since the state is derived from events, new features can be easily added, or existing features can be changed without affecting historical data.</p></li></ul><h4>Cons</h4><ul><li><p><strong>Complexity:</strong> Event sourcing adds complexity to your application. You must design your system around events, implement state rebuilding, and manage snapshots.</p></li><li><p><strong>Storage Requirements</strong>: Storing all events can lead to significant storage requirements, especially for long-running systems with many events.</p></li><li><p><strong>Data Privacy</strong>: Storing all events can raise privacy concerns, as historical data may contain sensitive information that must be managed carefully.</p></li></ul><div><hr></div><p><em>If you enjoyed this article, please hit the &#10084;&#65039; like button.</em></p><p><em>If you think someone else will benefit from this, then please &#128257; share this post.</em></p>]]></content:encoded></item></channel></rss>