<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vishwa</title>
    <description>The latest articles on DEV Community by Vishwa (@vishwa_7743ae1b748200b774).</description>
    <link>https://dev.to/vishwa_7743ae1b748200b774</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4232004%2Fa1297b58-acf5-48d4-a896-2d4e3662882a.png</url>
      <title>DEV Community: Vishwa</title>
      <link>https://dev.to/vishwa_7743ae1b748200b774</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishwa_7743ae1b748200b774"/>
    <language>en</language>
    <item>
      <title>UPI, Card or COD Without an if/else Chain: SOLID and the Strategy Pattern in Python</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Mon, 30 Nov 2026 17:22:01 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/upi-card-or-cod-without-an-ifelse-chain-solid-and-the-strategy-pattern-in-python-21bm</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/upi-card-or-cod-without-an-ifelse-chain-solid-and-the-strategy-pattern-in-python-21bm</guid>
      <description>&lt;p&gt;&lt;em&gt;How one interface and a few small classes make adding a new payment method a one-class change&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3rhbpaophjosrpfy3fui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3rhbpaophjosrpfy3fui.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A shopping app's checkout supports UPI, card and cash on delivery. Before a big sale, the business asks for a fourth option, Pay Later, by Friday.&lt;/p&gt;

&lt;p&gt;If the checkout code looks like the snippet below, Friday is going to be stressful.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"upi"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"card"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"cod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="c1"&gt;# adding Pay Later means editing this tested function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every new method means opening a function that already works and is already tested. The chain grows, each branch carries its own rules, and a change for one method can break another. The same chain usually gets copied into refunds and receipts too, so one new method becomes edits in several files.&lt;/p&gt;

&lt;p&gt;This breaks the &lt;strong&gt;Open/Closed principle&lt;/strong&gt;: code should be open for extension and closed for modification. Many &lt;code&gt;if/elif&lt;/code&gt; branches on a "type" or "mode" are the usual smell.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: one interface, one class per method
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhyqyuit97ivuf13a9i2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhyqyuit97ivuf13a9i2f.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Strategy pattern&lt;/strong&gt; wraps each algorithm in its own class behind one interface, and the caller is handed the one it needs. Here, the algorithm is "how to take the money".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;abc&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="no"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abstractmethod&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nd"&gt;@abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"UPI: collect request for Rs {amount}"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Card: charging Rs {amount}"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;COD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"COD: collect Rs {amount} at the door"&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="c1"&gt;# the strategy is passed in&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# no if/else on the type&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;UPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1499&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Checkout&lt;/code&gt; no longer knows that UPI exists. It only knows the &lt;code&gt;Payment&lt;/code&gt; contract. That is &lt;strong&gt;Dependency Inversion&lt;/strong&gt;: depend on an abstraction and pass the concrete object in. It also makes testing easy, because a fake payment can be injected.&lt;/p&gt;

&lt;p&gt;The user's choice still arrives as a string from the app, so something has to turn &lt;code&gt;"cod"&lt;/code&gt; into a &lt;code&gt;COD&lt;/code&gt; object. A small lookup table does that in one place, which is the Factory idea from the same set of patterns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;PAYMENTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"upi"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;UPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"card"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"cod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;COD&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;payment_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;choice&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;PAYMENTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Unsupported payment method: {choice}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;PAYMENTS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;payment_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cod"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;799&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Friday's request is one new class and one line in the table. &lt;code&gt;Checkout&lt;/code&gt; is not touched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayLater&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                      &lt;span class="c1"&gt;# new: nothing above changes&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"PayLater: Rs {amount} due next month"&lt;/span&gt;

&lt;span class="no"&gt;PAYMENTS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"paylater"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PayLater&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;payment_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"paylater"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output of all three snippets run together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPI: collect request for Rs 1499
Card: charging Rs 1499
COD: collect Rs 1499 at the door
COD: collect Rs 799 at the door
PayLater: Rs 2999 due next month
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5lmhsb3uopuktedk6y74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5lmhsb3uopuktedk6y74.png" alt=" " width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One more SOLID check: refunds.&lt;/strong&gt; It is tempting to add &lt;code&gt;refund()&lt;/code&gt; to &lt;code&gt;Payment&lt;/code&gt;. But cash on delivery has nothing to refund through a gateway, so &lt;code&gt;COD.refund()&lt;/code&gt; would raise an error, and a loop that refunds a batch of payments would crash halfway. That breaks &lt;strong&gt;Liskov Substitution&lt;/strong&gt;: a child must work wherever the parent is used. The fix is a separate &lt;code&gt;Refundable&lt;/code&gt; interface that only UPI and Card implement. This is also &lt;strong&gt;Interface Segregation&lt;/strong&gt;: no class should be forced to carry methods it cannot honour.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9h498ko46rxou8zm4ar0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9h498ko46rxou8zm4ar0.png" alt=" " width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don't use it for two fixed options.&lt;/strong&gt; If there will only ever be two branches, a plain &lt;code&gt;if&lt;/code&gt; is clearer. I apply SOLID where change hurts, not everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strategy is not Factory.&lt;/strong&gt; The lookup table picks which class to build, once. The strategy is the behaviour that &lt;code&gt;Checkout&lt;/code&gt; runs. Mixing up the two names is a common interview slip.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Python has no interface keyword.&lt;/strong&gt; An &lt;code&gt;abc&lt;/code&gt; class with only abstract methods plays that role.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real payments need more than &lt;code&gt;pay()&lt;/code&gt;.&lt;/strong&gt; Idempotency keys, timeouts and webhooks still matter. The pattern organises that code; it does not replace it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;A long if/elif on a type is a design smell.&lt;/p&gt;

&lt;p&gt;One interface, one class per method, and the caller gets the right one passed in.&lt;/p&gt;

&lt;p&gt;New payment method = new class; old code stays closed.&lt;/p&gt;

</description>
      <category>python</category>
      <category>designpatterns</category>
      <category>solid</category>
      <category>oop</category>
    </item>
    <item>
      <title>Designing a Flash Sale on GCP: CDN, Load Balancer, Cache, Queue and Pre-Scaling</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Mon, 30 Nov 2026 17:14:59 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/designing-a-flash-sale-on-gcp-cdn-load-balancer-cache-queue-and-pre-scaling-h1m</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/designing-a-flash-sale-on-gcp-cdn-load-balancer-cache-queue-and-pre-scaling-h1m</guid>
      <description>&lt;p&gt;&lt;em&gt;How I would keep a Flipkart-style midnight sale standing when everyone taps Buy at once&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5cr1y4079tkmqr865bxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5cr1y4079tkmqr865bxn.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is 11:59 PM. A shopping app has announced a phone at a big discount, starting at midnight. Imagine a few lakh people with the product page open, thumbs over the Buy button.&lt;/p&gt;

&lt;p&gt;At 12:00 they all tap. This post is how I would design a Flipkart-style app so that moment is boring. It is an illustrative design, not a description of any company's real system.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;A normal day and a sale day differ in one way: the traffic arrives at the same second. Without a plan, this is what breaks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Images and scripts&lt;/strong&gt; pull bandwidth from the origin servers that should be serving checkout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bots&lt;/strong&gt; hammer &lt;code&gt;/api/checkout&lt;/code&gt; and take capacity from real buyers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Product reads&lt;/strong&gt; all go to the database, which is also trying to take orders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Autoscaling&lt;/strong&gt; reacts, but adding pods and then nodes takes time. At max replicas, requests queue, latency rises, and the load balancer returns 503.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Side work&lt;/strong&gt; like confirmation messages and analytics runs inside the order request and makes every order slower.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The solution: one layer for each problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5d7au3le94kivtravn9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5d7au3le94kivtravn9g.png" alt=" " width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rule I follow is simple: each layer answers if it can, and only a miss goes one step deeper.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. CDN for everything static.&lt;/strong&gt; Product images and JS are served from the nearest edge with a one-year TTL and versioned file names, so a new image gets a new name. Cloud CDN is a switch on a backend inside the load balancer: on for the image bucket, off for cart and checkout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Load balancer and Cloud Armor at the front.&lt;/strong&gt; The load balancer is the one public entry and sends each path to a healthy backend. Cloud Armor runs on it, at the edge, and rate-limits per IP and challenges suspicious clients, so the checkout pods only see real traffic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. API Gateway for identity and limits.&lt;/strong&gt; The gateway checks the token and applies a stricter rate limit on the order route. A client over the limit gets 429 and waits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Redis for hot reads.&lt;/strong&gt; The product price and details come from Redis with a 5-minute TTL plus random jitter, so thousands of keys do not expire in the same second. A "not found" result is cached for 30-60 seconds, so bots asking for missing IDs never reach the database. The payment amount and live stock at checkout are never cached; they come from Cloud SQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fux2d6f97ai0kz90jdsj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fux2d6f97ai0kz90jdsj1.png" alt=" " width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. A gate before the database for stock.&lt;/strong&gt; When a few units meet a huge crowd, Redis can act as a gate: &lt;code&gt;DECR&lt;/code&gt; a stock counter, and anyone who gets a value below zero is told "sold out" immediately. Only the few who pass reach Cloud SQL, where one transaction inserts the order and reduces stock, and rolls back if anything fails. An idempotency key on the order request stops a double charge when the user retries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. A queue for everything that can wait.&lt;/strong&gt; After the order commits, the service publishes &lt;code&gt;order_placed&lt;/code&gt; to Pub/Sub and returns. Confirmation messages, invoices and the sales dashboard are subscribers. If one is slow, its backlog grows and nothing is dropped; if one fails, it retries and then goes to its dead-letter topic, without slowing the others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzvkq6qn5m9pf0q9ykqh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzvkq6qn5m9pf0q9ykqh9.png" alt=" " width="800" height="440"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;7. Pre-scale before the sale, not during it.&lt;/strong&gt; Checkout runs on GKE so the team controls pod counts and node pools. HPA adds pods when CPU passes a threshold, and Cluster Autoscaler adds nodes when pods cannot be placed, but both take time. So I would raise the minimums before midnight. Redis memory is chosen, not dynamic, so I would resize or add shards before the sale too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhjjxq13i97zpcye9y31y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhjjxq13i97zpcye9y31y.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pre-scaling costs money.&lt;/strong&gt; Warm pods and bigger Redis instances are paid for even if the sale is quieter than expected. I would size from past sale data where it exists and scale down right after.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A 503 "try again" is a valid answer.&lt;/strong&gt; If capacity runs out, a fast, clear failure is better than a page that hangs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Redis gate is a hint, not the truth.&lt;/strong&gt; If Redis fails, the database transaction still prevents overselling, just more slowly. The counter can be rebuilt from the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud SQL fits one region.&lt;/strong&gt; If stock had to be one consistent count across regions, I would look at Spanner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test it before the real day.&lt;/strong&gt; I would run a load test at the expected peak and watch the 5xx rate, p95 latency and Pub/Sub backlog.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;A flash sale is a normal day compressed into one second.&lt;/p&gt;

&lt;p&gt;CDN, Armor, gateway, Redis and Pub/Sub each take load off the database.&lt;/p&gt;

&lt;p&gt;Scale up before the sale; autoscaling alone reacts too late.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>gcp</category>
      <category>scalability</category>
      <category>redis</category>
    </item>
    <item>
      <title>Choosing the Right Database on GCP: Ask What the App Does With the Data</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Thu, 26 Nov 2026 01:54:43 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/choosing-the-right-database-on-gcp-ask-what-the-app-does-with-the-data-jee</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/choosing-the-right-database-on-gcp-ask-what-the-app-does-with-the-data-jee</guid>
      <description>&lt;p&gt;&lt;em&gt;Orders in Cloud SQL, the cart in Redis or Firestore, reports in BigQuery, and why one database is rarely enough&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9z7yn93oe1vkwfahhae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9z7yn93oe1vkwfahhae.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture a shopping app. A customer adds shoes to the cart on her phone and expects to see them on the laptop a second later. She pays, and stock must drop by exactly one. That evening, a manager asks for sales by category for the whole month.&lt;/p&gt;

&lt;p&gt;These are three different questions, and I would not answer all of them with the same database.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;The easy choice is "put everything in one database". It works on day one. Then it breaks in quiet ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The month-end sales report scans millions of order rows on the same primary that is taking payments, and checkout slows down.&lt;/li&gt;
&lt;li&gt;Cart updates, which happen constantly and matter only for a short time, compete with orders for the same connections.&lt;/li&gt;
&lt;li&gt;A product catalog where a phone has RAM and a shirt has a size gets forced into a table full of empty columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The real question is not "which database is popular". It is "what does the app do with this data?"&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: choose by the read
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flunb20lflxhya4hvep2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flunb20lflxhya4hvep2c.png" alt=" " width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is how I split the shopping app:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data&lt;/th&gt;
&lt;th&gt;Store&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Orders, payments, stock&lt;/td&gt;
&lt;td&gt;Cloud SQL (Spanner if global)&lt;/td&gt;
&lt;td&gt;Money and stock must change together or not at all: transactions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Live cart&lt;/td&gt;
&lt;td&gt;Firestore&lt;/td&gt;
&lt;td&gt;One key per user, live sync to every open screen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cart / session cache&lt;/td&gt;
&lt;td&gt;Redis (Memorystore)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;cart:user42&lt;/code&gt; with a 30-minute TTL, so abandoned carts clean themselves up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Product catalog&lt;/td&gt;
&lt;td&gt;MongoDB Atlas or Firestore&lt;/td&gt;
&lt;td&gt;Flexible documents, different attributes per product&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clicks and views&lt;/td&gt;
&lt;td&gt;Bigtable&lt;/td&gt;
&lt;td&gt;A firehose of writes, read back by key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sales reports&lt;/td&gt;
&lt;td&gt;BigQuery&lt;/td&gt;
&lt;td&gt;SQL scans over history&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Orders in Cloud SQL.&lt;/strong&gt; Placing an order is one transaction: insert the order and its items, reduce stock. If any step fails, everything rolls back, so there is no order without stock and no charge without an order. Cloud SQL fits one region. If I needed one globally consistent stock count across regions, that is where Spanner would replace it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cart in Firestore or Redis.&lt;/strong&gt; These two solve slightly different needs. Firestore gives real-time listeners, so the cart change is pushed to the other open screens with no polling. Redis is the fast option when the cart is a short-lived cache next to the app, with a TTL. Either way, the cart is not the source of truth for money; the checkout re-reads price and stock from Cloud SQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr67a1cpkuq30aawzh678.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr67a1cpkuq30aawzh678.png" alt=" " width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analytics in BigQuery.&lt;/strong&gt; Reports never read the app database. Order changes are sent as small JSON messages through Pub/Sub, Dataflow validates, removes duplicates and enriches them, and writes them into date-partitioned BigQuery tables. A query for one day reads only that partition, and since BigQuery stores data by column, it reads only the columns the query uses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd1yxprnhzzqhg4qcojdy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd1yxprnhzzqhg4qcojdy.png" alt=" " width="799" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CAP helps break ties.&lt;/strong&gt; When the network splits, some data must stay correct and some must stay available. A bank balance or stock count should refuse a write rather than risk a conflict (CP). A view counter should keep counting and merge later (AP).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgcm9r5zrnt46oz2udfjd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgcm9r5zrnt46oz2udfjd.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;More databases means more to run.&lt;/strong&gt; Every extra store adds backups, monitoring and a skill the team must have. For a small product I would start with Cloud SQL plus a Redis cache and add the others only when a real access pattern needs them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copies are eventually consistent.&lt;/strong&gt; The BigQuery report is a copy, so it can be a little behind. That is fine for a sales dashboard and wrong for a payment screen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pipelines deliver at least once.&lt;/strong&gt; Pub/Sub can deliver the same message twice, so the pipeline needs a dedupe key (here &lt;code&gt;order_id + table + ts&lt;/code&gt;) and a dead-letter table for bad rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bigtable has no SQL.&lt;/strong&gt; You read by row key or key range, so the key must be designed around the main read before any data goes in. Getting it wrong means full scans or one hot node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cart in two places is a trap.&lt;/strong&gt; If I use both Firestore and Redis for the cart, I must be clear which one is the truth. Usually I would pick one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Ask what the app does with the data, then pick the store.&lt;/p&gt;

&lt;p&gt;Orders in Cloud SQL, live cart in Firestore or Redis, history in BigQuery.&lt;/p&gt;

&lt;p&gt;Keep the app database as the truth and treat everything else as a copy.&lt;/p&gt;

</description>
      <category>database</category>
      <category>gcp</category>
      <category>systemdesign</category>
      <category>bigquery</category>
    </item>
    <item>
      <title>LRU Cache and Rate Limiter: Two Small Designs That Protect an API</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Thu, 26 Nov 2026 01:46:26 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/lru-cache-and-rate-limiter-two-small-designs-that-protect-an-api-34ef</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/lru-cache-and-rate-limiter-two-small-designs-that-protect-an-api-34ef</guid>
      <description>&lt;p&gt;&lt;em&gt;How I would stop one user from calling an API 1,000 times a minute, with Python you can run&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fikx8xetkkyzx0uvix6r4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fikx8xetkkyzx0uvix6r4.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a link-shortening service. A script starts calling &lt;code&gt;POST /shorten&lt;/code&gt; in a loop, say 1,000 times a minute from one API key. At the same moment, real users are opening product pages that read the same few items again and again.&lt;/p&gt;

&lt;p&gt;Two small designs handle both problems: a rate limiter that says "enough" to the noisy caller, and an LRU cache that keeps the hot data close and throws out what nobody is reading.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Without a limit, one client can use up the capacity meant for everyone. The database fills with junk rows, latency climbs, and real users start seeing errors they did nothing to cause.&lt;/p&gt;

&lt;p&gt;Without a sensible cache, every read goes to the database. And a cache with no size limit is not a fix either: memory is fixed, so something has to decide what leaves when it is full.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution, part 1: the rate limiter
&lt;/h2&gt;

&lt;p&gt;A rate limit is the maximum number of requests allowed per user or key in a time window. When a client goes over it, the API returns &lt;strong&gt;429 Too Many Requests&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ndqgj5y37j7h174tr6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7ndqgj5y37j7h174tr6h.png" alt=" " width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On GCP there are three places to enforce it, and they answer different questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Armor&lt;/strong&gt; runs at the edge, on the load balancer. It limits per IP, so it is good for bots and floods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Gateway&lt;/strong&gt; knows the caller's API key or JWT. A fixed quota, such as 100 requests a minute per key, is configuration, not code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your own code with Redis counters&lt;/strong&gt; is needed for dynamic limits, for example free and premium tiers with different quotas.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcz7y2au6w9by7b46nsrr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcz7y2au6w9by7b46nsrr.png" alt=" " width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the two algorithms I reach for. Both take an optional &lt;code&gt;now&lt;/code&gt; so the demo is repeatable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FixedWindowLimiter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;window_s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;window_s&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;                     &lt;span class="c1"&gt;# (user, window) -&amp;gt; requests&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TokenBucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refill_per_s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refill_per_s&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;monotonic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                              &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;

&lt;span class="n"&gt;fw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FixedWindowLimiter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fixed window:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user42"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TokenBucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;refill_per_s&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"token bucket:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"30 s later:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;40.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# fixed window: 100 / token bucket: 100 / 30 s later: 50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fixed window is one counter per user per minute. Token bucket allows a short burst (the capacity) and then a steady refill rate. In a real service the counter lives in Redis, not in a Python dict, so every instance sees the same count: &lt;code&gt;INCR&lt;/code&gt; the key for this user and minute, and set an expiry so old windows clean themselves up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzgd4r65n3a4k1wuamn8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzgd4r65n3a4k1wuamn8y.png" alt=" " width="799" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution, part 2: the LRU cache
&lt;/h2&gt;

&lt;p&gt;LRU means Least Recently Used: when the cache is full, drop the item that was read longest ago. Python's &lt;code&gt;OrderedDict&lt;/code&gt; makes this short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OrderedDict&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderedDict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move_to_end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# now the most recently used&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move_to_end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popitem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# drop the least recently used&lt;/span&gt;

&lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"product:1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Phone"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"product:2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Shoes"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"product:1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                      &lt;span class="c1"&gt;# product:1 is used again&lt;/span&gt;
&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"product:3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Shirt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             &lt;span class="c1"&gt;# full, so product:2 goes&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                     &lt;span class="c1"&gt;# ['product:1', 'product:3']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;put&lt;/code&gt; are O(1). In production I would not write this myself. Redis can evict least-recently-used keys when it reaches its memory limit, and I would still give every key a TTL, the way a product key gets 5 minutes plus jitter in my Flipkart-style design.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp3j9h2876xb3ujbbtjmp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp3j9h2876xb3ujbbtjmp.png" alt=" " width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fixed window has an edge problem.&lt;/strong&gt; A client can send the full limit at 10:00:59 and again at 10:01:00, so twice the limit in two seconds. Token bucket or a sliding window smooths this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The in-memory versions are per process.&lt;/strong&gt; With several Cloud Run instances, each keeps its own count. Shared limits need Redis or the gateway.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The dict in &lt;code&gt;FixedWindowLimiter&lt;/code&gt; never shrinks.&lt;/strong&gt; That is fine for a demo. Redis expiry solves it in a real service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LRU is not always right.&lt;/strong&gt; An item read once by a big scan can push out items that are read all day. For skewed traffic I would look at frequency-based eviction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limit at the right layer.&lt;/strong&gt; In my URL-shortener design, the gateway protects &lt;code&gt;/shorten&lt;/code&gt; only. Redirects skip it, because they must stay fast.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Rate limiting decides who gets in. Caching decides what stays close.&lt;/p&gt;

&lt;p&gt;Fixed window is simple; token bucket handles bursts better.&lt;/p&gt;

&lt;p&gt;On GCP: Cloud Armor per IP, API Gateway per key, Redis for anything dynamic.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>python</category>
      <category>api</category>
      <category>gcp</category>
    </item>
    <item>
      <title>Designing a URL Shortener on GCP: Short Codes, Fast Redirects, Simple Storage</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Thu, 19 Nov 2026 02:24:47 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/designing-a-url-shortener-on-gcp-short-codes-fast-redirects-simple-storage-1b18</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/designing-a-url-shortener-on-gcp-short-codes-fast-redirects-simple-storage-1b18</guid>
      <description>&lt;p&gt;&lt;em&gt;One write, a million reads: random base62 codes, layered caching and Firestore&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftxni9ci9blwlj0ljfxwp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftxni9ci9blwlj0ljfxwp.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A shop creates one Diwali promo link in the morning and sends it by SMS. By evening, say a million people have clicked it. That is one write and a million reads, and every click should feel instant.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;A URL shortener looks simple: store a code, look it up, redirect. The difficulty is in the shape of the traffic and in a few details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unique codes.&lt;/strong&gt; Two links must never get the same code, even if they are created at the same moment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read-heavy traffic.&lt;/strong&gt; If every click hits the database, cost and latency grow with popularity, and one viral link can become a hotspot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage choice.&lt;/strong&gt; Picking a heavier database than the data needs means running replicas and, later, a sharding project for no reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To size it, I start with assumptions. Say 100 million new links a month, which is about 40 creates a second. With a 100:1 read-to-write ratio, that is about 4,000 redirects a second. The system is read-heavy, so the redirect path gets the most attention.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Two endpoints: &lt;code&gt;POST /shorten&lt;/code&gt; returns &lt;code&gt;sho.rt/aB3x9&lt;/code&gt;, and &lt;code&gt;GET /aB3x9&lt;/code&gt; redirects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fysgb8fozcpyqkcyc66a6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fysgb8fozcpyqkcyc66a6.png" alt=" " width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create path
&lt;/h3&gt;

&lt;p&gt;Load Balancer, then API Gateway, then Cloud Run, then Firestore. The gateway sits in front of &lt;code&gt;/shorten&lt;/code&gt; only. It checks the API key and rate-limits, so a bot sending thousands of creates gets 429s. Redirects skip the gateway, because they must be fast.&lt;/p&gt;

&lt;h3&gt;
  
  
  Short code generation
&lt;/h3&gt;

&lt;p&gt;I use a random 7-character base62 code (a-z, A-Z, 0-9). 62^7 is about 3.5 trillion possible codes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;random_base62&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;firestore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"links"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"long_url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;BASE_URL&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;AlreadyExists&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;   &lt;span class="c1"&gt;# rare collision: try a new code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is the Firestore document ID, and &lt;code&gt;create()&lt;/code&gt; fails if that ID already exists. The check and the write happen as one atomic step, so two creators cannot both win. Even after a billion links, only about 1 in 3,000 creates would hit a collision and retry. A custom alias that is already taken returns 409 instead of retrying.&lt;/p&gt;

&lt;p&gt;A counter needs a separate ID service, since Firestore has no auto-increment, and a truncated hash still collides.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F73sm13ldrxebgzfa0mx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F73sm13ldrxebgzfa0mx5.png" alt=" " width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Storage: Firestore
&lt;/h3&gt;

&lt;p&gt;The data is one collection: document ID is the short code, with fields &lt;code&gt;long_url&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;, &lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;expires_at&lt;/code&gt;. Only key lookups, no joins. Firestore gives atomic uniqueness, a TTL policy for expiry, automatic scaling and multi-region availability, with nothing to resize or shard. Cloud SQL would work too, but it brings replicas, failover and a future sharding project. Bigtable is built for far higher throughput and has a per-node minimum cost; it fits click events better than the link table.&lt;/p&gt;

&lt;p&gt;One detail: TTL deletion can lag, so the app also checks &lt;code&gt;expires_at&lt;/code&gt; on read.&lt;/p&gt;

&lt;h3&gt;
  
  
  Redirect path: layered caching
&lt;/h3&gt;

&lt;p&gt;Cloud CDN, then Load Balancer, then Cloud Run, which checks Memorystore (Redis) first and reads Firestore only on a miss.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwpc74bgstu5g2y6pgnj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwpc74bgstu5g2y6pgnj6.png" alt=" " width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a viral link, the first click reads Firestore and later clicks come from Redis. A CDN can also cache the redirect itself, since it is a tiny response that is the same for every user, as long as the origin sends an explicit &lt;code&gt;max-age&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  301 or 302
&lt;/h3&gt;

&lt;p&gt;A 301 is cached by browsers, so later clicks may never reach my server. A 302 asks the server every time. I use 302 when the business needs click counts or editable links, and keep a short CDN TTL so edits go live quickly. Clicks are published to Pub/Sub without waiting and loaded into BigQuery. Clicks answered by the CDN are counted from load balancer logs through a log sink, since those requests never reach Cloud Run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0r3pnd2ta6tp7hcy0zxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0r3pnd2ta6tp7hcy0zxy.png" alt=" " width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Per-operation billing makes the cache mandatory.&lt;/strong&gt; Firestore bills per read, so a dropping Redis hit ratio raises the bill as well as latency. I would alert on both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never keep a click counter in the link document.&lt;/strong&gt; One document supports roughly one sustained write per second. Count clicks in BigQuery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Editing a link clears caches in order.&lt;/strong&gt; Firestore first, then delete the Redis key, then invalidate the CDN path. A 301 already cached in a browser cannot be cleared at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Revisit the database if needs change.&lt;/strong&gt; If accounts, billing or team reports with joins appear, I would add Cloud SQL for that data only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short links get abused.&lt;/strong&gt; A production shortener should scan destinations for malware and phishing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;A URL shortener is read-heavy, so design the redirect path first.&lt;/p&gt;

&lt;p&gt;Random base62 codes with an atomic create give uniqueness without a counter service.&lt;/p&gt;

&lt;p&gt;CDN and Redis absorb the reads; Firestore stays the simple source of truth.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>googlecloud</category>
      <category>interview</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Pub/Sub for Event-Driven Orders: One Order, Many Independent Reactions</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Thu, 19 Nov 2026 02:16:26 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/pubsub-for-event-driven-orders-one-order-many-independent-reactions-42aj</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/pubsub-for-event-driven-orders-one-order-many-independent-reactions-42aj</guid>
      <description>&lt;p&gt;&lt;em&gt;Publish order_placed once; email, inventory and analytics each get their own copy and their own retries&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp6p35vab6ateq8q4diq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fp6p35vab6ateq8q4diq2.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A customer taps "Place order". Behind that tap, several things should happen: a confirmation email goes out, inventory is updated, and the order shows up in the analytics reports. The customer only cares that the order went through.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;The simple way is for the order service to call each one in turn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;place_order()
  -&amp;gt; call email service      (wait)
  -&amp;gt; call inventory service  (wait)
  -&amp;gt; call analytics service  (wait)
  -&amp;gt; return "order placed"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This breaks in a few ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow:&lt;/strong&gt; the customer waits for every call, including ones they do not care about.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fragile:&lt;/strong&gt; if the email service is down, does the order fail? If you add a retry loop, you are now writing a scheduler inside the order service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tightly coupled:&lt;/strong&gt; adding a fourth reaction, like a loyalty-points service, means changing and redeploying the order service.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The solution: one topic, one subscription per reaction
&lt;/h2&gt;

&lt;p&gt;With Pub/Sub, the order service publishes one &lt;code&gt;order_placed&lt;/code&gt; event to a topic and returns. Each reacting service has its own subscription to that topic. Pub/Sub puts a copy of every message in every subscription.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order-service --publish--&amp;gt; topic: order_placed
                              |-- email-sub      -&amp;gt; email worker
                              |-- inventory-sub  -&amp;gt; inventory worker
                              |-- analytics-sub  -&amp;gt; analytics pipeline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3k53v72wz5jpxhh82964.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3k53v72wz5jpxhh82964.png" alt=" " width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The key idea: &lt;strong&gt;each subscription has its own backlog, its own acknowledgements and its own retries.&lt;/strong&gt; That gives three useful properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failures stay local.&lt;/strong&gt; If the email worker is failing, its messages wait and retry in &lt;code&gt;email-sub&lt;/code&gt;. Inventory and analytics keep going, untouched.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Slow consumers do not lose data.&lt;/strong&gt; If the inventory worker is slow, its backlog grows. Nothing is dropped; it catches up when it can.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a reaction is a new subscription.&lt;/strong&gt; The loyalty-points service subscribes to &lt;code&gt;order_placed&lt;/code&gt;. The order service does not change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxw5deqx1v6zkk7zbeiee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxw5deqx1v6zkk7zbeiee.png" alt=" " width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Retries and the dead-letter topic
&lt;/h3&gt;

&lt;p&gt;Pub/Sub does the retrying, so no scheduler is needed. If a worker does not acknowledge a message, Pub/Sub redelivers it with exponential backoff. After a maximum number of attempts (for example, 5), the message moves to that subscription's dead-letter topic, where someone can inspect it, fix the cause and republish.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb801uxq5gg60dq583z51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb801uxq5gg60dq583z51.png" alt=" " width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Push or pull
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pull:&lt;/strong&gt; the worker asks for messages and acknowledges with an explicit &lt;code&gt;ack()&lt;/code&gt;. It pulls only what it can handle, which is the most direct form of rate limiting. Good for heavy or batch workers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Push:&lt;/strong&gt; Pub/Sub POSTs each message to a URL, such as a Cloud Run service. A 2xx response is the ack. Rate control is indirect, through Cloud Run max instances and Pub/Sub backing off on errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this example, I would use push to Cloud Run for email and pull for the analytics pipeline, which can feed Dataflow and then BigQuery.&lt;/p&gt;

&lt;h3&gt;
  
  
  Duplicates are normal
&lt;/h3&gt;

&lt;p&gt;Pub/Sub delivers at least once, so the same message can arrive twice. Every consumer must be idempotent. The inventory worker can record the &lt;code&gt;order_id&lt;/code&gt; it has processed and skip a repeat. The analytics pipeline can drop duplicates using a key such as &lt;code&gt;order_id&lt;/code&gt; plus table plus timestamp.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9uwklsvqid7jv61sc008.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9uwklsvqid7jv61sc008.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  A smaller version of this pattern
&lt;/h3&gt;

&lt;p&gt;In VJourney, my personal travel-companion project, notifications to a trip host when someone joins are delivered asynchronously through a Redis Stream consumer group. It is the same idea at a smaller scale: the request finishes quickly, and a separate consumer handles the follow-up work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Not everything should be async.&lt;/strong&gt; Checking that stock exists before confirming the order must happen inside the order transaction. Pub/Sub is for reactions after the fact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick the right tool.&lt;/strong&gt; Pub/Sub is for broadcasting an event to many subscribers. A command to one target with a delay ("remind in 30 minutes") fits Cloud Tasks better. If you need to replay a long history of events as a core feature, a log like Kafka fits better.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create subscriptions first.&lt;/strong&gt; A message published to a topic with no subscription is dropped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch the dead-letter topics.&lt;/strong&gt; A DLQ nobody reads is just a quiet place to lose orders. I would alert on its size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Same subscription vs different subscriptions.&lt;/strong&gt; Several pods on one subscription share the work. Different services need different subscriptions to each get a copy. Mixing these up is a common bug.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Publish the event once; each consumer gets its own subscription.&lt;/p&gt;

&lt;p&gt;Retries, backlogs and dead-letter topics are per subscription, so failures stay local.&lt;/p&gt;

&lt;p&gt;Delivery is at least once, so every consumer must be idempotent.&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>eventdriven</category>
      <category>systemdesign</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Cache-Aside with Memorystore: Serve the Same Product Page Without Asking the Database Every Time</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Tue, 17 Nov 2026 01:19:45 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/cache-aside-with-memorystore-serve-the-same-product-page-without-asking-the-database-every-time-22jd</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/cache-aside-with-memorystore-serve-the-same-product-page-without-asking-the-database-every-time-22jd</guid>
      <description>&lt;p&gt;&lt;em&gt;Read Redis first, fall back to Cloud SQL, and have a plan for stale data&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh4ms5dkps1rkk9dacnbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh4ms5dkps1rkk9dacnbm.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Say a phone goes on offer on a Flipkart-style store, and its product page is opened 10,000 times in a few minutes. The details and price are the same for everyone. Without a cache, the database answers the same question 10,000 times.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Cloud SQL is the source of truth for product details and price. It is good at that, but every read costs CPU, connections and time. When a few popular products get most of the traffic, the database spends its effort repeating itself, and real work like placing orders waits in the same queue.&lt;/p&gt;

&lt;p&gt;The CDN does not solve this one. It is great for files that are the same for everyone, such as product images. Price and stock are data that the app builds and changes, so they belong in a cache next to the app, not at the edge.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: cache-aside with Memorystore
&lt;/h2&gt;

&lt;p&gt;Memorystore is Google Cloud's managed Redis. It is not behind the load balancer: it has a private IP in your VPC, and the app talks to it directly.&lt;/p&gt;

&lt;p&gt;Cache-aside means the app owns the cache logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"product:{product_id}"&lt;/span&gt;
    &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt;                      &lt;span class="c1"&gt;# hit: about 1 ms&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cloud_sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch_product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# miss: go to the source of truth&lt;/span&gt;
    &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;random_jitter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 5 min TTL + jitter&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the first request, Redis misses, the app reads Cloud SQL and stores the result with a TTL. Later requests are served from memory until the TTL ends. For the phone on offer, the database sees one read per TTL window instead of one per visitor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fukke59g9nwtklbhtdm0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fukke59g9nwtklbhtdm0i.png" alt=" " width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling stale data
&lt;/h3&gt;

&lt;p&gt;A cache is a copy, so it can be wrong. I use three rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Write to the database, then delete the key.&lt;/strong&gt; When a seller changes the price, the app updates Cloud SQL first and then deletes &lt;code&gt;product:123&lt;/code&gt;. The next read misses and fetches the new price. Deleting is simpler and safer than trying to update the cached value in place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. TTL is the safety net.&lt;/strong&gt; If the delete is missed, for example because the app crashed between the two steps, the key still expires in a few minutes. A random jitter on the TTL stops many keys from expiring at the same moment and sending a burst of misses to the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Some data is never cached.&lt;/strong&gt; The payment amount and live stock at checkout come from Cloud SQL every time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9v1u86jlvj9li02iwqc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9v1u86jlvj9li02iwqc5.png" alt=" " width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Different data gets different TTLs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;TTL&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;product:123&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5 min + jitter&lt;/td&gt;
&lt;td&gt;Fresh enough, jitter avoids mass expiry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cart:user42&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;30 min&lt;/td&gt;
&lt;td&gt;Abandoned carts clean themselves up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"not found" result&lt;/td&gt;
&lt;td&gt;30 to 60 s&lt;/td&gt;
&lt;td&gt;Stops bots hammering missing IDs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F05ov8avw9tezljpx87ko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F05ov8avw9tezljpx87ko.png" alt=" " width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When Redis is down
&lt;/h3&gt;

&lt;p&gt;The app uses a short timeout on Redis. If Redis is slow or unreachable, it treats the request as a miss and reads Cloud SQL. The site gets slower, but it stays up. Memorystore replication is asynchronous, so if a primary fails, a replica is promoted and a very recent write might be lost. For a cache that is fine: the lost entry becomes a miss.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where I have used this pattern
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://v-journey.vercel.app/" rel="noopener noreferrer"&gt;VJourney&lt;/a&gt;, my personal travel-companion project, Redis (Upstash) is used as a cache-aside layer, with Postgres and Firestore holding the data. Every part of that stack maps to a GCP service, and in a GCP deployment this role would be played by Memorystore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Short staleness is a product decision.&lt;/strong&gt; For a few minutes a user might see an old price on the product page. That is acceptable only because checkout reads the real price from Cloud SQL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hot keys still exist.&lt;/strong&gt; One very popular key sits on one Redis shard. For extreme cases, a few seconds of in-process caching on each app instance spreads the load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory is sized, not elastic.&lt;/strong&gt; Memorystore capacity is chosen up front. Before a big sale I would resize or add shards from an alert, not during the spike.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep related keys together on purpose.&lt;/strong&gt; In cluster mode, keys are hashed to slots. A hash tag like &lt;code&gt;{user42}:cart&lt;/code&gt; keeps one user's keys on the same shard when that matters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor the hit ratio.&lt;/strong&gt; A falling hit ratio means more load on Cloud SQL, often from a wrong TTL or evictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Read Redis first; on a miss, read the database and store with a TTL.&lt;/p&gt;

&lt;p&gt;On a write, update the database and delete the key; TTL catches what you miss.&lt;/p&gt;

&lt;p&gt;Redis is a cache, and the database is always the source of truth.&lt;/p&gt;

</description>
      <category>redis</category>
      <category>googlecloud</category>
      <category>caching</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Cloud Run vs GKE: Picking a Home for Your Containers</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Sun, 15 Nov 2026 10:33:10 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/cloud-run-vs-gke-picking-a-home-for-your-containers-4h7</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/cloud-run-vs-gke-picking-a-home-for-your-containers-4h7</guid>
      <description>&lt;p&gt;&lt;em&gt;A small API with spiky traffic and a 20-service platform need different things&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcr0r2qzogmotf4d8f6fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcr0r2qzogmotf4d8f6fc.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine two teams. One has a small product API that is quiet at 3 am and busy at lunch. The other runs about 20 microservices for checkout, payments and orders, with steady traffic all day and strict rules about which service can talk to which. Both have Docker images ready. Where should each one run?&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;The same container can run on Cloud Run or on GKE (Google Kubernetes Engine). Picking the wrong one hurts in a predictable way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Put the small spiky API on GKE, and you pay for node VMs that sit idle at night, and someone has to look after a cluster.&lt;/li&gt;
&lt;li&gt;Put a large, tightly networked platform on Cloud Run, and you can end up fighting for control you do not have: sidecars, pod-level network rules, node pools tuned for a sale.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The choice is not about which tool is better. It is about traffic pattern and how much control you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: choose by traffic and control
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08c17e7jy412h0jnl5t8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08c17e7jy412h0jnl5t8.png" alt=" " width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cloud Run&lt;/strong&gt; is serverless. You deploy a container and Google runs it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scales to zero when idle, and adds instances when traffic arrives.&lt;/li&gt;
&lt;li&gt;Billed per request and compute time, so a quiet night costs close to nothing.&lt;/li&gt;
&lt;li&gt;Best for stateless services with spiky traffic.&lt;/li&gt;
&lt;li&gt;Scaling is set at deploy time:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcloud run deploy product-service \
  --image=REGION-docker.pkg.dev/PROJECT/repo/product-service:a1b2c3d \
  --min-instances=0 --max-instances=20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GKE&lt;/strong&gt; is managed Kubernetes. You run pods on nodes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You pay for node VMs that are always on, plus a cluster fee.&lt;/li&gt;
&lt;li&gt;Best for steady load, sidecars and strict networking such as Kubernetes NetworkPolicy ("payment accepts traffic only from checkout").&lt;/li&gt;
&lt;li&gt;Scaling has layers: the HPA adds pods when CPU crosses a target, and when nodes fill up, the Cluster Autoscaler adds nodes. Both shrink after the peak.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9yiaub3gag44nvg4b8zq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9yiaub3gag44nvg4b8zq.png" alt=" " width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How each one sits behind the load balancer
&lt;/h3&gt;

&lt;p&gt;Both can sit behind the same External Application Load Balancer, but they attach differently.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Cloud Run&lt;/th&gt;
&lt;th&gt;GKE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Attached via&lt;/td&gt;
&lt;td&gt;Serverless NEG&lt;/td&gt;
&lt;td&gt;Pod NEG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LB health checks&lt;/td&gt;
&lt;td&gt;None to configure&lt;/td&gt;
&lt;td&gt;Yes, on pods&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who spreads load across instances&lt;/td&gt;
&lt;td&gt;Cloud Run&lt;/td&gt;
&lt;td&gt;The LB, straight to pods&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaling&lt;/td&gt;
&lt;td&gt;Cloud Run autoscaling&lt;/td&gt;
&lt;td&gt;HPA for pods, Cluster Autoscaler for nodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You manage&lt;/td&gt;
&lt;td&gt;Very little&lt;/td&gt;
&lt;td&gt;Cluster, nodes, YAML&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzofpehcbo917t7r0lzxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzofpehcbo917t7r0lzxy.png" alt=" " width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Applying it to the two teams
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The small spiky API goes on Cloud Run.&lt;/strong&gt; It scales to zero at night and scales out at lunch on its own. There is no cluster to patch and no node pool to size.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The 20-service platform probably goes on GKE&lt;/strong&gt;, at least for the core services. With that many services talking to each other, the team benefits from Kubernetes networking rules, sidecars and the ability to pre-scale node pools before a known sale. In a Flipkart-style example design, checkout runs on GKE so the team controls pod counts and node pools, while product and auth stay on Cloud Run because they need less tuning.&lt;/p&gt;

&lt;p&gt;A mixed setup is normal. One load balancer and one URL map can send &lt;code&gt;/api/product/*&lt;/code&gt; to Cloud Run and &lt;code&gt;/api/checkout/*&lt;/code&gt; to GKE pods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Twenty services is not an automatic GKE.&lt;/strong&gt; If those services are stateless and loosely coupled, Cloud Run can run all of them. I would ask what the team actually needs from Kubernetes before paying for it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Run and Cloud SQL connections.&lt;/strong&gt; Cloud Run can scale to many instances, and each one opens database connections. I would keep a small pool per instance, cap max instances and use pooling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close the public URL.&lt;/strong&gt; Every Cloud Run service has a public &lt;code&gt;run.app&lt;/code&gt; address that skips the load balancer. I would set ingress to internal and Cloud Load Balancing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GKE needs pre-scaling for known spikes.&lt;/strong&gt; At max replicas, requests queue, latency rises, then the load balancer returns 503. For a planned sale I would raise minimums before it starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch idle clusters.&lt;/strong&gt; A forgotten practice GKE cluster keeps billing. A budget alert does not stop anything by itself, but it tells you early.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Cloud Run: spiky, stateless, little to manage, pay per use.&lt;/p&gt;

&lt;p&gt;GKE: many services, steady load, control over pods, nodes and networking.&lt;/p&gt;

&lt;p&gt;Both can live behind one load balancer, so pick per service.&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>kubernetes</category>
      <category>serverless</category>
      <category>devops</category>
    </item>
    <item>
      <title>The Database Scaling Ladder: Fix a Slow Database in the Right Order</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Wed, 11 Nov 2026 16:45:08 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/the-database-scaling-ladder-fix-a-slow-database-in-the-right-order-44ii</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/the-database-scaling-ladder-fix-a-slow-database-in-the-right-order-44ii</guid>
      <description>&lt;p&gt;&lt;em&gt;Indexes, then cache, then read replicas, then a bigger machine, and sharding only at the top&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flriwic00tp3dxtdkq28n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flriwic00tp3dxtdkq28n.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Say the order page of an online store starts taking a few seconds to load. Someone looks at the Cloud SQL dashboard, sees CPU near the top, and says, "Let's shard." That is usually the most expensive answer to the question.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;A slow database has many possible causes, and each fix costs a different amount of effort and money. Jump to the top fix too early and you pay for a sharding project when a missing index was the real issue. Stay too long at the bottom and you keep tuning queries while a single primary has truly run out of room.&lt;/p&gt;

&lt;p&gt;I think of it as a ladder. Climb one rung at a time, cheapest first, and only go up when the current rung stops helping.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz57w0pq8bl6lwryuxu3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz57w0pq8bl6lwryuxu3s.png" alt=" " width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: five rungs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Rung 1: indexes and queries
&lt;/h3&gt;

&lt;p&gt;First, confirm the real cause. A query that scans a whole table because a column is not indexed will be slow on any machine size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- if it shows a full table scan:&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_orders_user_created&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This costs almost nothing and is the first thing I check.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rung 2: cache
&lt;/h3&gt;

&lt;p&gt;Many reads ask for the same thing again and again: a product's details, a category list. A Memorystore (Redis) cache in front of Cloud SQL answers those repeated reads in memory, and the database only sees misses. Every key gets a TTL, and when the data changes, the app updates Cloud SQL and then deletes the key. The database stays the source of truth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rung 3: read replicas
&lt;/h3&gt;

&lt;p&gt;If reads are still heavy after caching, read replicas take read-only queries off the primary. Writes still go to the primary. The catch is replication lag: a replica can be slightly behind, so a user who just placed an order might not see it on a replica for a moment. Reads that must be fresh, like "show the order I just placed", should go to the primary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk3yen15ou7vs9yjg5zii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk3yen15ou7vs9yjg5zii.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rung 4: scale up (vertical scaling)
&lt;/h3&gt;

&lt;p&gt;Now give the same instance a bigger machine. For example, going from 2 vCPU and 8 GB RAM to 8 vCPU and 32 GB RAM. The data, instance name and connection address stay the same. Only the machine type changes.&lt;/p&gt;

&lt;p&gt;How I would do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Confirm the cause again. A bad query needs a fix, not a bigger machine.&lt;/li&gt;
&lt;li&gt;Pick an off-peak time and tell the team.&lt;/li&gt;
&lt;li&gt;Take an on-demand backup.&lt;/li&gt;
&lt;li&gt;Edit the instance and choose the new machine type.&lt;/li&gt;
&lt;li&gt;The instance restarts, connections drop briefly, and the app reconnects.&lt;/li&gt;
&lt;li&gt;Watch CPU, memory and latency while the cache warms up again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fprn3phjp5tgej1gn8us3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fprn3phjp5tgej1gn8us3.png" alt=" " width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few details matter here. A machine type change needs a restart, so there is brief downtime (HA setups can shorten it; check the current docs for your edition). Growing the disk does not need a restart and can happen automatically. Shrinking a disk has limited support. Read replicas should be resized too, so they keep up with the primary. And a primary does not resize itself: you do it, or you automate it from monitoring.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rung 5: shard
&lt;/h3&gt;

&lt;p&gt;Stop scaling up when the largest machine type is reached, when cost grows faster than the gain, or when the single primary is still the write bottleneck. Replicas help reads, not writes. That is when sharding makes sense: spread users across several databases so writes are spread too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure before climbing.&lt;/strong&gt; Query insights and slow query logs tell you which rung you are actually on. Guessing is how teams end up sharding an unindexed table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries are part of scaling up.&lt;/strong&gt; The restart is only a short blip if the app retries connections. Without retries, it looks like an outage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching brings staleness.&lt;/strong&gt; It needs a clear rule for invalidation and a TTL as a safety net. Some reads, like payment amounts or stock at checkout, should never come from cache.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replicas bring lag.&lt;/strong&gt; I would route read-after-write paths to the primary on purpose, not by accident.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharding is a project, not a setting.&lt;/strong&gt; It needs a routing layer, a migration plan and a new answer for cross-shard reports. That is exactly why it sits at the top.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Check queries and indexes before touching infrastructure.&lt;/p&gt;

&lt;p&gt;Cache and replicas scale reads; a bigger machine buys time for everything.&lt;/p&gt;

&lt;p&gt;Shard only when one primary can no longer take the writes.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>database</category>
      <category>googlecloud</category>
      <category>performance</category>
    </item>
    <item>
      <title>CDN vs Load Balancer vs API Gateway: Who Does What on GCP</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Mon, 09 Nov 2026 17:40:51 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/cdn-vs-load-balancer-vs-api-gateway-who-does-what-on-gcp-2c8c</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/cdn-vs-load-balancer-vs-api-gateway-who-does-what-on-gcp-2c8c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuo6mn703sk35fpb85dyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuo6mn703sk35fpb85dyk.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[IMAGE 1: upload 1.png. Caption: The CDN skips the server, the load balancer picks a server, and the API Gateway decides if a request is allowed.]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Picture a shopper opening a product page on a Flipkart-style store. In one second, the phone asks for a product photo, the shopper's cart and, somewhere else, a seller's system is updating stock through an API. Three requests, three very different needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;These three terms get mixed up because all of them sit "in front of" the app. Without a clear split, teams make two kinds of mistakes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Caching the wrong thing.&lt;/strong&gt; A cart response cached at the edge can show one user's data to another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-building the same checks in every service.&lt;/strong&gt; Each microservice writes its own API key check and rate limit, slightly differently, and one of them gets it wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cleanest way I know to separate them is one line each: the CDN saves a trip to your servers, the load balancer decides which server, and the gateway decides whether the request is allowed at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: one entry point, split by path
&lt;/h2&gt;

&lt;p&gt;On GCP, every request enters through one External Application Load Balancer with one global IP. Its URL map sends each path to the right backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;pathRules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/img/*"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/static/*"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;image-backend-bucket&lt;/span&gt;       &lt;span class="c1"&gt;# Cloud CDN: ON&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/cart/*"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cart-backend-service&lt;/span&gt;       &lt;span class="c1"&gt;# Cloud CDN: OFF&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/seller/*"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;api-gateway-backend&lt;/span&gt;        &lt;span class="c1"&gt;# gateway, then the seller service&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkrbs0gk51rdwb8hj2hos.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkrbs0gk51rdwb8hj2hos.png" alt=" " width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Images: Cloud CDN
&lt;/h3&gt;

&lt;p&gt;Cloud CDN is not a separate box. It is a checkbox on a backend inside the same load balancer. On a hit, the edge near the user answers. On a miss, it fetches once from the origin, stores it and serves it.&lt;/p&gt;

&lt;p&gt;The origin decides what is cached, through response headers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/img/iphone-v3.jpg   Cache-Control: public, max-age=31536000
/api/cart            Cache-Control: no-store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Product images get a long TTL with versioned filenames, so a new image gets a new name and never fights the old cache. The frontend never sets caching rules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgk3yq9t7ddrq9zl1emg5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgk3yq9t7ddrq9zl1emg5.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Cart: load balancer straight to the service
&lt;/h3&gt;

&lt;p&gt;The cart is personal and changes every few seconds, so CDN stays off. The load balancer's job here is to spread live traffic across healthy instances. If one instance fails its health check, it stops getting traffic. If traffic grows past capacity, requests queue briefly, latency rises and then the load balancer returns 503. They are not silently dropped.&lt;/p&gt;

&lt;h3&gt;
  
  
  Seller API: API Gateway
&lt;/h3&gt;

&lt;p&gt;Sellers call the store from their own systems with API keys. The path goes load balancer, then API Gateway, then the seller service. The gateway checks the key or JWT and enforces a fixed quota before any business code runs. You do not deploy it as a service; you give it an OpenAPI spec.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;/seller/stock&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;security&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;api_key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[]&lt;/span&gt;
      &lt;span class="na"&gt;x-google-backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://seller-service-xyz.run.app&lt;/span&gt;
&lt;span class="c1"&gt;# simplified; check the current API Gateway docs for the full spec&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A bad or over-limit request is rejected at the gateway, so the inventory service only sees clean, authorized calls.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftima3bx0c62tzxc1jft8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftima3bx0c62tzxc1jft8.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One load balancer is usually enough.&lt;/strong&gt; I would add another only for a separate domain, internal-only traffic or non-HTTP protocols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lock the side door.&lt;/strong&gt; Every Cloud Run service has its own public &lt;code&gt;run.app&lt;/code&gt; URL that skips the load balancer and gateway. I would set ingress to internal and Cloud Load Balancing only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Gateway has limits.&lt;/strong&gt; It handles auth and fixed quotas well. Tiered limits (free vs premium), developer portals or monetization point to Apigee or custom code with Redis counters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gateway is not the first line against floods.&lt;/strong&gt; Cloud Armor runs at the edge on the load balancer and handles bad IPs, bots and attack patterns. The gateway then enforces who may call what. I would use both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Databases are never behind the load balancer.&lt;/strong&gt; The app calls Cloud SQL and Memorystore directly over private networking.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;CDN: same file for everyone, served from the edge.&lt;/p&gt;

&lt;p&gt;Load balancer: one entry point that picks a healthy server.&lt;/p&gt;

&lt;p&gt;API Gateway: auth and quotas before your code runs.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>googlecloud</category>
      <category>cloud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Cloud SQL: From One Database to Three Shards Without Downtime</title>
      <dc:creator>Vishwa</dc:creator>
      <pubDate>Sun, 08 Nov 2026 08:42:13 +0000</pubDate>
      <link>https://dev.to/vishwa_7743ae1b748200b774/cloud-sql-from-one-database-to-three-shards-without-downtime-2l9e</link>
      <guid>https://dev.to/vishwa_7743ae1b748200b774/cloud-sql-from-one-database-to-three-shards-without-downtime-2l9e</guid>
      <description>&lt;p&gt;&lt;em&gt;Split users into 1024 buckets, then move one bucket at a time: freeze, catch up, flip&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs7k04kmuj0pc6ztuc5zd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs7k04kmuj0pc6ztuc5zd.png" alt=" " width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a Flipkart-style store running on one Cloud SQL database. The disk is about 70% full, queries are slowing down, and the sale season is coming. Every order, address and payment still lands on that one primary.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;One primary takes every write. Indexes, a cache, replicas and a bigger machine help, but at some point that primary is still the bottleneck. The next step is sharding: spreading per-user data across several databases.&lt;/p&gt;

&lt;p&gt;The hard part is not creating three new databases. It is moving live users onto them while those users keep placing orders. A naive plan, such as "stop the site, copy everything, switch", means downtime. A hash like &lt;code&gt;user_id mod 3&lt;/code&gt; has a different problem: the day you add a fourth shard, almost every user maps to a new place.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution: buckets and a shard map
&lt;/h2&gt;

&lt;p&gt;I split the problem into two layers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. A fixed rule from user to bucket.&lt;/strong&gt; A bucket is a number computed in code, never stored per user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bucket = int(first 8 hex digits of user UUID) mod 1024
# 07a1c3e4-... -&amp;gt; 128,041,956 mod 1024 -&amp;gt; bucket 996
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This rule never changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. A small table from bucket to shard.&lt;/strong&gt; Only this table, &lt;code&gt;shard_map&lt;/code&gt;, is stored: one row per bucket, 1024 rows in total.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;bucket&lt;/th&gt;
&lt;th&gt;shard_name (live)&lt;/th&gt;
&lt;th&gt;target_shard (planned)&lt;/th&gt;
&lt;th&gt;frozen&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;996&lt;/td&gt;
&lt;td&gt;old_db&lt;/td&gt;
&lt;td&gt;shard_a&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The app reads &lt;code&gt;shard_map&lt;/code&gt; to route every query. Moving users now means changing rows in a small table, not rehashing anyone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fstegxpl4tsc025c3bnxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fstegxpl4tsc025c3bnxb.png" alt=" " width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What moves and what stays: &lt;code&gt;users&lt;/code&gt;, &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;addresses&lt;/code&gt; and &lt;code&gt;payments&lt;/code&gt; move to shards A, B and C, about a third of users each. &lt;code&gt;products&lt;/code&gt;, offers and other shared tables stay on the old database, along with &lt;code&gt;shard_map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fspw9ztlr4bmwrfqyvrqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fspw9ztlr4bmwrfqyvrqw.png" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: prepare with no visible change
&lt;/h3&gt;

&lt;p&gt;Create the three shards with the same schema. Seed &lt;code&gt;shard_map&lt;/code&gt; with all 1024 buckets pointing to &lt;code&gt;old_db&lt;/code&gt;. Deploy the shard-aware app. It behaves exactly as before, because every bucket still points to the old database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: copy and catch up
&lt;/h3&gt;

&lt;p&gt;A one-off Cloud Run job copies existing users to their target shards, then runs catch-up passes for rows changed since the last pass. Users keep writing to the old database the whole time, so each pass finds fewer rows. Once a pass finds only a small number of rows a few times in a row, the job stops. Catch-up can never reach exactly zero while users are writing, and that is what the cutover is for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: cut over one bucket
&lt;/h3&gt;

&lt;p&gt;For one bucket:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set &lt;code&gt;frozen = true&lt;/code&gt;. New writes for that bucket get a 503; reads still work.&lt;/li&gt;
&lt;li&gt;Wait for in-flight writes that started before the freeze to finish.&lt;/li&gt;
&lt;li&gt;Run a final catch-up for that bucket and verify the counts.&lt;/li&gt;
&lt;li&gt;Flip &lt;code&gt;shard_name&lt;/code&gt; to &lt;code&gt;shard_a&lt;/code&gt; and set &lt;code&gt;frozen = false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The client retries the 503, and the retry lands on the new shard. Users in that bucket see a few seconds of "try again", and nobody else notices anything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8cachbufpsmvtubrvg9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8cachbufpsmvtubrvg9i.png" alt=" " width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: roll out in waves
&lt;/h3&gt;

&lt;p&gt;Start with one quiet bucket as a pilot. Then move a wave of 10, then a larger wave, then the rest. Only one bucket is frozen at a time. Watch errors, latency, 503 counts and shard CPU and disk between waves.&lt;/p&gt;

&lt;p&gt;After the last flip, keep the old copies of the moved tables for a safe window as a rollback option. Take a final backup, then drop them. The old database lives on as the products database, with its replicas and cache.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a fourth shard later
&lt;/h3&gt;

&lt;p&gt;The same machinery works. Set &lt;code&gt;target_shard = 'shard_d'&lt;/code&gt; for the chosen buckets, copy from each bucket's current shard, then freeze and flip bucket by bucket. The bucket rule never changes, so no user is rehashed. Only whole buckets move.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff48jn8bu4ewzjcgnxpjl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff48jn8bu4ewzjcgnxpjl.png" alt=" " width="800" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs and limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shard last, not first.&lt;/strong&gt; Sharding adds a router, a migration job and cross-shard query problems. I would only do it after indexes, cache, replicas and a bigger machine have run out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-shard queries get harder.&lt;/strong&gt; "All orders in Chennai yesterday" now touches three databases. Reports should come from an analytics copy, not from the shards.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The 503 is a real cost.&lt;/strong&gt; It is small, but the client must retry safely, so writes need to be idempotent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disk does not shrink by itself.&lt;/strong&gt; Dropping the moved tables frees used space, but the provisioned disk size stays where it was.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick the bucket count up front.&lt;/strong&gt; Changing the bucket rule later means rehashing everyone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Map users to fixed buckets, and store only the bucket-to-shard map.&lt;/p&gt;

&lt;p&gt;Move one bucket at a time: freeze, final catch-up, verify, flip.&lt;/p&gt;

&lt;p&gt;The same steps that create the first shards also add the next one.&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>googlecloud</category>
      <category>database</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
