<?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: DesignBeyondCode</title>
    <description>The latest articles on DEV Community by DesignBeyondCode (@designbeyondcode).</description>
    <link>https://dev.to/designbeyondcode</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%2F4229721%2F5fdc207c-afb8-44c5-95d0-08460b8d5a87.png</url>
      <title>DEV Community: DesignBeyondCode</title>
      <link>https://dev.to/designbeyondcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/designbeyondcode"/>
    <language>en</language>
    <item>
      <title>Hot keys in a distributed cache: why more shards don't help</title>
      <dc:creator>DesignBeyondCode</dc:creator>
      <pubDate>Tue, 17 Nov 2026 09:47:28 +0000</pubDate>
      <link>https://dev.to/designbeyondcode/hot-keys-in-a-distributed-cache-why-more-shards-dont-help-2hjl</link>
      <guid>https://dev.to/designbeyondcode/hot-keys-in-a-distributed-cache-why-more-shards-dont-help-2hjl</guid>
      <description>&lt;p&gt;Here's a cache problem that catches a lot of people. Your cache cluster has 64 nodes and plenty of spare room. Then one celebrity posts, and a single key gets a million reads a second. The cluster falls over anyway.&lt;/p&gt;

&lt;p&gt;The usual first answer is "add more shards." It doesn't help, and the reason is worth understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why more shards don't help
&lt;/h2&gt;

&lt;p&gt;A distributed cache splits keys across nodes with a hash. Each key lives on exactly one node. That's what makes it scale for many keys. But one key still hashes to one node, no matter how many nodes you add.&lt;/p&gt;

&lt;p&gt;One node serves about 300,000 operations a second. A million reads a second on one key is more than three times that. It's also about half the node's network card, just for that key.&lt;/p&gt;

&lt;h2&gt;
  
  
  The second problem: when it expires
&lt;/h2&gt;

&lt;p&gt;Now say that hot key expires. Every request in that millisecond misses. Every one of them goes to the database at the same time. This is called a stampede, or a thundering herd. The database was sized for a few hundred thousand misses a second across all keys, not a burst like that on one row.&lt;/p&gt;

&lt;p&gt;So a hot key is two problems: too many reads on one node, and a herd when it expires.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: find hot keys automatically
&lt;/h2&gt;

&lt;p&gt;You can't fix what you can't see. Each node samples 1 in 100 requests and reports any key above 50,000 reads a second. Nobody has to guess which keys are hot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: a tiny cache inside each app
&lt;/h2&gt;

&lt;p&gt;Every app process keeps its own small cache of the hottest few thousand keys, for 2 seconds. Now each process asks the cache cluster for a hot key at most once every 2 seconds. Most reads never leave the app.&lt;/p&gt;

&lt;p&gt;The cost is up to 2 seconds of extra staleness on hot keys. For a like count, that's fine. For a bank balance, it isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: copies on several nodes
&lt;/h2&gt;

&lt;p&gt;With 20,000 hosts running 16 processes each, the near cache can still send 160,000 reads a second to one node. So store the hot key as 8 copies, &lt;code&gt;key#1&lt;/code&gt; to &lt;code&gt;key#8&lt;/code&gt;, on 8 different nodes. Readers pick one at random. That's about 20,000 reads a second per copy.&lt;/p&gt;

&lt;p&gt;The catch: every write and delete must now hit all 8 copies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: let only one request refill it
&lt;/h2&gt;

&lt;p&gt;This fixes the herd. When a request misses, the cache gives it a token, called a lease. Other requests for the same key get "wait" instead of a miss. They retry a few milliseconds later. Only the request holding the lease reads the database and fills the cache.&lt;/p&gt;

&lt;p&gt;So a thousand misses become one database read. When Facebook added leases to memcache, peak database queries on one busy key fell from 17,000 a second to 1,300.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;More shards spread many keys, not one key. For one hot key you need to find it, keep it close to the app, copy it, and let only one request refill it.&lt;/p&gt;




&lt;p&gt;I wrote the full version on my site. It has the race that leases also fix, failover, eviction, a calculator for the numbers, and code in five languages. &lt;a href="https://designbeyondcode.com/system-design/problems/distributed-cache/" rel="noopener noreferrer"&gt;Design a distributed cache&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>systemdesign</category>
      <category>caching</category>
      <category>interview</category>
    </item>
    <item>
      <title>Design a URL shortener: start with one server and let the numbers grow it</title>
      <dc:creator>DesignBeyondCode</dc:creator>
      <pubDate>Mon, 09 Nov 2026 18:01:04 +0000</pubDate>
      <link>https://dev.to/designbeyondcode/design-a-url-shortener-start-with-one-server-and-let-the-numbers-grow-it-46ec</link>
      <guid>https://dev.to/designbeyondcode/design-a-url-shortener-start-with-one-server-and-let-the-numbers-grow-it-46ec</guid>
      <description>&lt;p&gt;Most answers to "design a URL shortener" start with the final picture: load balancers, a cache cluster, Kafka, ten boxes. Interviewers see that every day. It shows you remember a diagram, not that you can design.&lt;/p&gt;

&lt;p&gt;A better answer starts small. Then you let the numbers decide each next step. Here's the short version.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, the numbers
&lt;/h2&gt;

&lt;p&gt;Say people create 100 million links a month, and each link gets about 100 clicks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That's about 40 new links a second. One normal database can handle it.&lt;/li&gt;
&lt;li&gt;It's about 4,000 clicks a second, and maybe 10 times that when one link goes viral. That's a lot of reads, so we'll need a cache.&lt;/li&gt;
&lt;li&gt;Over 10 years we need about 12 billion short codes. Seven characters (letters and digits) give us 3.5 trillion, so 7 is plenty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the data is small, and the reads are many. Everything below follows from that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: one server, one table
&lt;/h2&gt;

&lt;p&gt;A table that maps each short code to its long URL. One endpoint to create a link, one to redirect. Start here in the interview. It's fine to say it won't last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: make codes without waiting
&lt;/h2&gt;

&lt;p&gt;If every new link asks the database for the next number, every server waits on that one database. Instead, each server takes a block of 10,000 numbers at once and hands them out from memory. Two servers never get the same block, so codes never clash.&lt;/p&gt;

&lt;p&gt;We also shuffle the numbers with a secret key. Otherwise codes made one after another look alike, and anyone could walk through every link.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: a cache for clicks
&lt;/h2&gt;

&lt;p&gt;Here's the nice part. A link's target never changes. So a cached copy can be missing, but it can never be wrong. Each click checks a small cache in the server's memory, then Redis, and only then the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: count clicks later
&lt;/h2&gt;

&lt;p&gt;The redirect shouldn't wait for analytics. It drops a small "someone clicked" event into a queue and answers right away. Another job adds up the clicks in the background.&lt;/p&gt;

&lt;h2&gt;
  
  
  The question that changes the answer
&lt;/h2&gt;

&lt;p&gt;Should the redirect be a 301 or a 302? A 301 tells browsers to remember it forever. You get less traffic, but you stop seeing clicks, and you can't stop a bad link after you delete it. A 302 keeps every click coming back to you. So ask the interviewer about analytics early. It decides this.&lt;/p&gt;

&lt;h2&gt;
  
  
  When one link goes viral
&lt;/h2&gt;

&lt;p&gt;The traffic itself is fine. The danger is the moment that link drops out of the cache. Thousands of requests miss at once and all hit the database. The fix: keep hot links in each server's memory for a few seconds, and let only one request reload a link while the others wait.&lt;/p&gt;




&lt;p&gt;I wrote the full version on my site. It has a diagram you can grow step by step, a calculator for the numbers, "what breaks" scenarios, and code in five languages. It's free, no sign-up: &lt;a href="https://designbeyondcode.com/system-design/problems/url-shortener/" rel="noopener noreferrer"&gt;Design a URL shortener&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>interview</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
