<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vishwakanths on Medium]]></title>
        <description><![CDATA[Stories by Vishwakanths on Medium]]></description>
        <link>https://medium.com/@vishwakanths18?source=rss-1f0023db64e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wULnCeaHCo7v2q5h</url>
            <title>Stories by Vishwakanths on Medium</title>
            <link>https://medium.com/@vishwakanths18?source=rss-1f0023db64e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:58:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vishwakanths18/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing a URL Shortener on GCP: Short Codes, Fast Redirects, Simple Storage]]></title>
            <link>https://medium.com/@vishwakanths18/designing-a-url-shortener-on-gcp-short-codes-fast-redirects-simple-storage-ed46ec033faf?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed46ec033faf</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Thu, 19 Nov 2026 02:22:49 GMT</pubDate>
            <atom:updated>2026-11-19T02:22:49.026Z</atom:updated>
            <content:encoded><![CDATA[<p><em>One write, a million reads: random base62 codes, layered caching and Firestore</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N4g5Snl61-O3LotScVsIHQ.png" /></figure><p>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.</p><h3>The problem</h3><p>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:</p><ul><li><strong>Unique codes.</strong> Two links must never get the same code, even if they are created at the same moment.</li><li><strong>Read-heavy traffic.</strong> If every click hits the database, cost and latency grow with popularity, and one viral link can become a hotspot.</li><li><strong>Storage choice.</strong> Picking a heavier database than the data needs means running replicas and, later, a sharding project for no reason.</li></ul><p>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.</p><h3>The solution</h3><p>Two endpoints: POST /shorten returns sho.rt/aB3x9, and GET /aB3x9 redirects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yhw73EM2dAoJdvVB-V1s2A.png" /></figure><h3>Create path</h3><p>Load Balancer, then API Gateway, then Cloud Run, then Firestore. The gateway sits in front of /shorten 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.</p><h3>Short code generation</h3><p>I use a random 7-character base62 code (a-z, A-Z, 0–9). 62⁷ is about 3.5 trillion possible codes.</p><pre>for attempt in range(3):<br>    code = random_base62(7)<br>    try:<br>        firestore.collection(&quot;links&quot;).document(code).create({&quot;long_url&quot;: url, ...})<br>        return BASE_URL + &quot;/&quot; + code<br>    except AlreadyExists:<br>        continue   # rare collision: try a new code</pre><p>The code is the Firestore document ID, and create() 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.</p><p>A counter needs a separate ID service, since Firestore has no auto-increment, and a truncated hash still collides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z3ebzpyOGxaZeXtDOkrefQ.png" /></figure><h3>Storage: Firestore</h3><p>The data is one collection: document ID is the short code, with fields long_url, user_id, created_at and expires_at. 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.</p><p>One detail: TTL deletion can lag, so the app also checks expires_at on read.</p><h3>Redirect path: layered caching</h3><p>Cloud CDN, then Load Balancer, then Cloud Run, which checks Memorystore (Redis) first and reads Firestore only on a miss.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9FdFil07d64ph39TiZ8IPQ.png" /></figure><p>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 max-age.</p><h3>301 or 302</h3><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhzdzE-vKt5ZGfj89GM9bg.png" /></figure><h3>Trade-offs and limits</h3><ul><li><strong>Per-operation billing makes the cache mandatory.</strong> Firestore bills per read, so a dropping Redis hit ratio raises the bill as well as latency. I would alert on both.</li><li><strong>Never keep a click counter in the link document.</strong> One document supports roughly one sustained write per second. Count clicks in BigQuery.</li><li><strong>Editing a link clears caches in order.</strong> Firestore first, then delete the Redis key, then invalidate the CDN path. A 301 already cached in a browser cannot be cleared at all.</li><li><strong>Revisit the database if needs change.</strong> If accounts, billing or team reports with joins appear, I would add Cloud SQL for that data only.</li><li><strong>Short links get abused.</strong> A production shortener should scan destinations for malware and phishing.</li></ul><h3>Takeaway</h3><p>A URL shortener is read-heavy, so design the redirect path first.</p><p>Random base62 codes with an atomic create give uniqueness without a counter service.</p><p>CDN and Redis absorb the reads; Firestore stays the simple source of truth.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed46ec033faf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pub/Sub for Event-Driven Orders: One Order, Many Independent Reactions]]></title>
            <link>https://medium.com/@vishwakanths18/pub-sub-for-event-driven-orders-one-order-many-independent-reactions-78f0c8002455?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/78f0c8002455</guid>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Thu, 19 Nov 2026 02:13:58 GMT</pubDate>
            <atom:updated>2026-11-19T02:13:58.012Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Publish order_placed once; email, inventory and analytics each get their own copy and their own retries</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G0xvhlYdRRJgxYlKx9dXnw.png" /></figure><p>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.</p><h3>The problem</h3><p>The simple way is for the order service to call each one in turn:</p><pre>place_order()<br>  -&gt; call email service      (wait)<br>  -&gt; call inventory service  (wait)<br>  -&gt; call analytics service  (wait)<br>  -&gt; return &quot;order placed&quot;</pre><p>This breaks in a few ways:</p><ul><li><strong>Slow:</strong> the customer waits for every call, including ones they do not care about.</li><li><strong>Fragile:</strong> 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.</li><li><strong>Tightly coupled:</strong> adding a fourth reaction, like a loyalty-points service, means changing and redeploying the order service.</li></ul><h3>The solution: one topic, one subscription per reaction</h3><p>With Pub/Sub, the order service publishes one order_placed 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.</p><pre>order-service --publish--&gt; topic: order_placed<br>                              |-- email-sub      -&gt; email worker<br>                              |-- inventory-sub  -&gt; inventory worker<br>                              |-- analytics-sub  -&gt; analytics pipeline</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A05Zd6vpNUBUeZaEy8V7pw.png" /></figure><p>The key idea: <strong>each subscription has its own backlog, its own acknowledgements and its own retries.</strong> That gives three useful properties.</p><p><strong>Failures stay local.</strong> If the email worker is failing, its messages wait and retry in email-sub. Inventory and analytics keep going, untouched.</p><p><strong>Slow consumers do not lose data.</strong> If the inventory worker is slow, its backlog grows. Nothing is dropped; it catches up when it can.</p><p><strong>Adding a reaction is a new subscription.</strong> The loyalty-points service subscribes to order_placed. The order service does not change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5vgpIWztPTqlIrqo7njy7w.png" /></figure><h3>Retries and the dead-letter topic</h3><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u29IqS9pqFGA0hTEIs3asQ.png" /></figure><h3>Push or pull</h3><ul><li><strong>Pull:</strong> the worker asks for messages and acknowledges with an explicit ack(). It pulls only what it can handle, which is the most direct form of rate limiting. Good for heavy or batch workers.</li><li><strong>Push:</strong> 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.</li></ul><p>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.</p><h3>Duplicates are normal</h3><p>Pub/Sub delivers at least once, so the same message can arrive twice. Every consumer must be idempotent. The inventory worker can record the order_id it has processed and skip a repeat. The analytics pipeline can drop duplicates using a key such as order_id plus table plus timestamp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*no3zzquS-b7B6W6I1Mryvg.png" /></figure><h3>A smaller version of this pattern</h3><p>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.</p><h3>Trade-offs and limits</h3><ul><li><strong>Not everything should be async.</strong> Checking that stock exists before confirming the order must happen inside the order transaction. Pub/Sub is for reactions after the fact.</li><li><strong>Pick the right tool.</strong> 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.</li><li><strong>Create subscriptions first.</strong> A message published to a topic with no subscription is dropped.</li><li><strong>Watch the dead-letter topics.</strong> A DLQ nobody reads is just a quiet place to lose orders. I would alert on its size.</li><li><strong>Same subscription vs different subscriptions.</strong> 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.</li></ul><h3>Takeaway</h3><p>Publish the event once; each consumer gets its own subscription.</p><p>Retries, backlogs and dead-letter topics are per subscription, so failures stay local.</p><p>Delivery is at least once, so every consumer must be idempotent.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78f0c8002455" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cache-Aside with Memorystore: Serve the Same Product Page Without Asking the Database Every Time]]></title>
            <link>https://medium.com/@vishwakanths18/cache-aside-with-memorystore-serve-the-same-product-page-without-asking-the-database-every-time-ebf698bdfbc6?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebf698bdfbc6</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Tue, 17 Nov 2026 01:22:15 GMT</pubDate>
            <atom:updated>2026-11-17T01:22:15.031Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Read Redis first, fall back to Cloud SQL, and have a plan for stale data</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LO3t-SyY4-h2LBt-u_691Q.png" /></figure><p>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.</p><h3>The problem</h3><p>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.</p><p>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.</p><h3>The solution: cache-aside with Memorystore</h3><p>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.</p><p>Cache-aside means the app owns the cache logic:</p><pre>def get_product(product_id):<br>    key = f&quot;product:{product_id}&quot;<br>    cached = redis.get(key)<br>    if cached:<br>        return cached                      # hit: about 1 ms<br>    product = cloud_sql.fetch_product(product_id)   # miss: go to the source of truth<br>    redis.set(key, product, ex=300 + random_jitter())  # 5 min TTL + jitter<br>    return product</pre><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X0TPiVGmijuUE679OyUJzQ.png" /></figure><h3>Handling stale data</h3><p>A cache is a copy, so it can be wrong. I use three rules.</p><p><strong>1. Write to the database, then delete the key.</strong> When a seller changes the price, the app updates Cloud SQL first and then deletes product:123. The next read misses and fetches the new price. Deleting is simpler and safer than trying to update the cached value in place.</p><p><strong>2. TTL is the safety net.</strong> 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.</p><p><strong>3. Some data is never cached.</strong> The payment amount and live stock at checkout come from Cloud SQL every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xEVhrTqxYWbzWlBIOj8jzQ.png" /></figure><p>Different data gets different TTLs:</p><p>KeyTTLWhyproduct:1235 min + jitterFresh enough, jitter avoids mass expirycart:user4230 minAbandoned carts clean themselves up&quot;not found&quot; result30 to 60 sStops bots hammering missing IDs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I81fviVD22aqWaiqTBunBQ.png" /></figure><h3>When Redis is down</h3><p>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.</p><h3>Where I have used this pattern</h3><p>In VJourney, 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.</p><h3>Trade-offs and limits</h3><ul><li><strong>Short staleness is a product decision.</strong> 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.</li><li><strong>Hot keys still exist.</strong> 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.</li><li><strong>Memory is sized, not elastic.</strong> Memorystore capacity is chosen up front. Before a big sale I would resize or add shards from an alert, not during the spike.</li><li><strong>Keep related keys together on purpose.</strong> In cluster mode, keys are hashed to slots. A hash tag like {user42}:cart keeps one user&#39;s keys on the same shard when that matters.</li><li><strong>Monitor the hit ratio.</strong> A falling hit ratio means more load on Cloud SQL, often from a wrong TTL or evictions.</li></ul><h3>Takeaway</h3><p>Read Redis first; on a miss, read the database and store with a TTL.</p><p>On a write, update the database and delete the key; TTL catches what you miss.</p><p>Redis is a cache, and the database is always the source of truth.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebf698bdfbc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cloud Run vs GKE: Picking a Home for Your Containers]]></title>
            <link>https://medium.com/@vishwakanths18/cloud-run-vs-gke-picking-a-home-for-your-containers-117337bd39fe?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/117337bd39fe</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Sun, 15 Nov 2026 10:34:01 GMT</pubDate>
            <atom:updated>2026-11-15T10:34:01.114Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A small API with spiky traffic and a 20-service platform need different things</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MHP1L2iPcoICqQ3pjwwVSQ.png" /></figure><p>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?</p><h3>The problem</h3><p>The same container can run on Cloud Run or on GKE (Google Kubernetes Engine). Picking the wrong one hurts in a predictable way:</p><ul><li>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.</li><li>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.</li></ul><p>The choice is not about which tool is better. It is about traffic pattern and how much control you need.</p><h3>The solution: choose by traffic and control</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qT03ztnFarjffCnLr7DG8A.png" /></figure><p><strong>Cloud Run</strong> is serverless. You deploy a container and Google runs it.</p><ul><li>Scales to zero when idle, and adds instances when traffic arrives.</li><li>Billed per request and compute time, so a quiet night costs close to nothing.</li><li>Best for stateless services with spiky traffic.</li><li>Scaling is set at deploy time:</li></ul><pre>gcloud run deploy product-service \<br>  --image=REGION-docker.pkg.dev/PROJECT/repo/product-service:a1b2c3d \<br>  --min-instances=0 --max-instances=20</pre><p><strong>GKE</strong> is managed Kubernetes. You run pods on nodes.</p><ul><li>You pay for node VMs that are always on, plus a cluster fee.</li><li>Best for steady load, sidecars and strict networking such as Kubernetes NetworkPolicy (“payment accepts traffic only from checkout”).</li><li>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.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3C3WJp8nKxF0wxc5S5-8Yg.png" /></figure><h3>How each one sits behind the load balancer</h3><p>Both can sit behind the same External Application Load Balancer, but they attach differently.</p><p>Cloud RunGKEAttached viaServerless NEGPod NEGLB health checksNone to configureYes, on podsWho spreads load across instancesCloud RunThe LB, straight to podsScalingCloud Run autoscalingHPA for pods, Cluster Autoscaler for nodesYou manageVery littleCluster, nodes, YAML</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G3pu1Sv70Q9hl3EHlRrXcA.png" /></figure><h3>Applying it to the two teams</h3><p><strong>The small spiky API goes on Cloud Run.</strong> 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.</p><p><strong>The 20-service platform probably goes on GKE</strong>, 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.</p><p>A mixed setup is normal. One load balancer and one URL map can send /api/product/* to Cloud Run and /api/checkout/* to GKE pods.</p><h3>Trade-offs and limits</h3><ul><li><strong>Twenty services is not an automatic GKE.</strong> 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.</li><li><strong>Cloud Run and Cloud SQL connections.</strong> 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.</li><li><strong>Close the public URL.</strong> Every Cloud Run service has a public run.app address that skips the load balancer. I would set ingress to internal and Cloud Load Balancing.</li><li><strong>GKE needs pre-scaling for known spikes.</strong> At max replicas, requests queue, latency rises, then the load balancer returns 503. For a planned sale I would raise minimums before it starts.</li><li><strong>Watch idle clusters.</strong> A forgotten practice GKE cluster keeps billing. A budget alert does not stop anything by itself, but it tells you early.</li></ul><h3>Takeaway</h3><p>Cloud Run: spiky, stateless, little to manage, pay per use.</p><p>GKE: many services, steady load, control over pods, nodes and networking.</p><p>Both can live behind one load balancer, so pick per service.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=117337bd39fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Database Scaling Ladder: Fix a Slow Database in the Right Order]]></title>
            <link>https://medium.com/@vishwakanths18/the-database-scaling-ladder-fix-a-slow-database-in-the-right-order-0d20e3baea04?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d20e3baea04</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Wed, 11 Nov 2026 16:47:04 GMT</pubDate>
            <atom:updated>2026-11-11T16:47:04.902Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Indexes, then cache, then read replicas, then a bigger machine, and sharding only at the top</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W4jlQg0M3cg-Psds2pVLsQ.png" /></figure><p>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.</p><h3>The problem</h3><p>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.</p><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KmL9Ik9VFyUxdkSmFscbkQ.png" /></figure><h3>The solution: five rungs</h3><h3>Rung 1: indexes and queries</h3><p>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.</p><pre>EXPLAIN ANALYZE<br>SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;</pre><pre>-- if it shows a full table scan:<br>CREATE INDEX idx_orders_user_created ON orders (user_id, created_at DESC);</pre><p>This costs almost nothing and is the first thing I check.</p><h3>Rung 2: cache</h3><p>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.</p><h3>Rung 3: read replicas</h3><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bKXURPgoU86gFbPk71YqWw.png" /></figure><h3>Rung 4: scale up (vertical scaling)</h3><p>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.</p><p>How I would do it:</p><ol><li>Confirm the cause again. A bad query needs a fix, not a bigger machine.</li><li>Pick an off-peak time and tell the team.</li><li>Take an on-demand backup.</li><li>Edit the instance and choose the new machine type.</li><li>The instance restarts, connections drop briefly, and the app reconnects.</li><li>Watch CPU, memory and latency while the cache warms up again.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xy9i0GAdYCpk1PVoC_hZYA.png" /></figure><p>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.</p><h3>Rung 5: shard</h3><p>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.</p><h3>Trade-offs and limits</h3><ul><li><strong>Measure before climbing.</strong> Query insights and slow query logs tell you which rung you are actually on. Guessing is how teams end up sharding an unindexed table.</li><li><strong>Retries are part of scaling up.</strong> The restart is only a short blip if the app retries connections. Without retries, it looks like an outage.</li><li><strong>Caching brings staleness.</strong> 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.</li><li><strong>Replicas bring lag.</strong> I would route read-after-write paths to the primary on purpose, not by accident.</li><li><strong>Sharding is a project, not a setting.</strong> 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.</li></ul><h3>Takeaway</h3><p>Check queries and indexes before touching infrastructure.</p><p>Cache and replicas scale reads; a bigger machine buys time for everything.</p><p>Shard only when one primary can no longer take the writes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d20e3baea04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CDN vs Load Balancer vs API Gateway: Who Does What on GCP]]></title>
            <link>https://medium.com/@vishwakanths18/cdn-vs-load-balancer-vs-api-gateway-who-does-what-on-gcp-f691368488eb?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f691368488eb</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[api-gateway]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Mon, 09 Nov 2026 17:37:21 GMT</pubDate>
            <atom:updated>2026-11-09T17:37:21.416Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Images through the CDN, the cart through the load balancer, the seller API through a gateway</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FgI53qeY1DQWvWiYxR8v8A.png" /></figure><p>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.</p><h3>The problem</h3><p>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:</p><ul><li><strong>Caching the wrong thing.</strong> A cart response cached at the edge can show one user’s data to another.</li><li><strong>Re-building the same checks in every service.</strong> Each microservice writes its own API key check and rate limit, slightly differently, and one of them gets it wrong.</li></ul><p>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.</p><h3>The solution: one entry point, split by path</h3><p>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.</p><pre>pathRules:<br>  - paths: [&quot;/img/*&quot;, &quot;/static/*&quot;]<br>    service: image-backend-bucket       # Cloud CDN: ON<br>  - paths: [&quot;/api/cart/*&quot;]<br>    service: cart-backend-service       # Cloud CDN: OFF<br>  - paths: [&quot;/seller/*&quot;]<br>    service: api-gateway-backend        # gateway, then the seller service</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CwsIujFI6lSIZ3IsGks4nA.png" /></figure><h3>Images: Cloud CDN</h3><p>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.</p><p>The origin decides what is cached, through response headers:</p><pre>/img/iphone-v3.jpg   Cache-Control: public, max-age=31536000<br>/api/cart            Cache-Control: no-store</pre><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9pralbQSN47Ua7JD-voMA.png" /></figure><h3>Cart: load balancer straight to the service</h3><p>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.</p><h3>Seller API: API Gateway</h3><p>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.</p><pre>paths:<br>  /seller/stock:<br>    post:<br>      security:<br>        - api_key: []<br>      x-google-backend:<br>        address: https://seller-service-xyz.run.app<br># simplified; check the current API Gateway docs for the full spec</pre><p>A bad or over-limit request is rejected at the gateway, so the inventory service only sees clean, authorized calls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vzxePTDRataC7NbrRdduSQ.png" /></figure><h3>Trade-offs and limits</h3><ul><li><strong>One load balancer is usually enough.</strong> I would add another only for a separate domain, internal-only traffic or non-HTTP protocols.</li><li><strong>Lock the side door.</strong> Every Cloud Run service has its own public run.app URL that skips the load balancer and gateway. I would set ingress to internal and Cloud Load Balancing only.</li><li><strong>API Gateway has limits.</strong> 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.</li><li><strong>Gateway is not the first line against floods.</strong> 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.</li><li><strong>Databases are never behind the load balancer.</strong> The app calls Cloud SQL and Memorystore directly over private networking.</li></ul><h3>Takeaway</h3><p>CDN: same file for everyone, served from the edge.</p><p>Load balancer: one entry point that picks a healthy server.</p><p>API Gateway: auth and quotas before your code runs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f691368488eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cloud SQL: From One Database to Three Shards Without Downtime]]></title>
            <link>https://medium.com/@vishwakanths18/cloud-sql-from-one-database-to-three-shards-without-downtime-0343bec8aa41?source=rss-1f0023db64e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/0343bec8aa41</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[database-sharding]]></category>
            <dc:creator><![CDATA[Vishwakanths]]></dc:creator>
            <pubDate>Sun, 08 Nov 2026 08:48:10 GMT</pubDate>
            <atom:updated>2026-11-08T08:48:10.927Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Split users into 1024 buckets, then move one bucket at a time: freeze, catch up, flip</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QwsaoszpYR8k5Fau4YeZEg.png" /></figure><p>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.</p><h3>The problem</h3><p>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.</p><p>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 user_id mod 3 has a different problem: the day you add a fourth shard, almost every user maps to a new place.</p><h3>The solution: buckets and a shard map</h3><p>I split the problem into two layers.</p><p><strong>1. A fixed rule from user to bucket.</strong> A bucket is a number computed in code, never stored per user:</p><pre>bucket = int(first 8 hex digits of user UUID) mod 1024<br># 07a1c3e4-... -&gt; 128,041,956 mod 1024 -&gt; bucket 996</pre><p>This rule never changes.</p><p><strong>2. A small table from bucket to shard.</strong> Only this table, shard_map, is stored: one row per bucket, 1024 rows in total.</p><p>bucketshard_name (live)target_shard (planned)frozen996old_dbshard_afalse</p><p>The app reads shard_map to route every query. Moving users now means changing rows in a small table, not rehashing anyone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yHsszApUPnirn49vntFmAw.png" /></figure><p>What moves and what stays: users, orders, addresses and payments move to shards A, B and C, about a third of users each. products, offers and other shared tables stay on the old database, along with shard_map.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lBnW7Y416La-dMOszCLFXA.png" /></figure><h3>Step 1: prepare with no visible change</h3><p>Create the three shards with the same schema. Seed shard_map with all 1024 buckets pointing to old_db. Deploy the shard-aware app. It behaves exactly as before, because every bucket still points to the old database.</p><h3>Step 2: copy and catch up</h3><p>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.</p><h3>Step 3: cut over one bucket</h3><p>For one bucket:</p><ol><li>Set frozen = true. New writes for that bucket get a 503; reads still work.</li><li>Wait for in-flight writes that started before the freeze to finish.</li><li>Run a final catch-up for that bucket and verify the counts.</li><li>Flip shard_name to shard_a and set frozen = false.</li></ol><p>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.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*14PUeGmpcboGwLXqPz0u_g.png" /></figure><h3>Step 4: roll out in waves</h3><p>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.</p><p>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.</p><h3>Adding a fourth shard later</h3><p>The same machinery works. Set target_shard = &#39;shard_d&#39; for the chosen buckets, copy from each bucket&#39;s current shard, then freeze and flip bucket by bucket. The bucket rule never changes, so no user is rehashed. Only whole buckets move.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D4tBEeTzFmd1BvxHUmjOTQ.png" /></figure><h3>Trade-offs and limits</h3><ul><li><strong>Shard last, not first.</strong> 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.</li><li><strong>Cross-shard queries get harder.</strong> “All orders in Chennai yesterday” now touches three databases. Reports should come from an analytics copy, not from the shards.</li><li><strong>The 503 is a real cost.</strong> It is small, but the client must retry safely, so writes need to be idempotent.</li><li><strong>Disk does not shrink by itself.</strong> Dropping the moved tables frees used space, but the provisioned disk size stays where it was.</li><li><strong>Pick the bucket count up front.</strong> Changing the bucket rule later means rehashing everyone.</li></ul><h3>Takeaway</h3><p>Map users to fixed buckets, and store only the bucket-to-shard map.</p><p>Move one bucket at a time: freeze, final catch-up, verify, flip.</p><p>The same steps that create the first shards also add the next one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0343bec8aa41" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>