<?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>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>
