Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/System Design

System design: build a short-link service and scale it

A short link is just a lookup. Build one on a single machine, then watch each bottleneck show up and get fixed.

Derpx0621 Sept · 5 min read
Contents
What's the simplest version?Where do the codes come from?How big does it get?Should the redirect be 301 or 302?What happens when the database gets tired?What if one database still can't answer everyone?What about IDs from many machines?Design it yourselfSources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
What's the simplest version?Where do the codes come from?How big does it get?Should the redirect be 301 or 302?What happens when the database gets tired?What if one database still can't answer everyone?What about IDs from many machines?Design it yourselfSources

How does a link like short.ly/aZ3k9Q know where to send you? Nothing is hidden in the letters. It's a lookup, like a contact in your phone. The hard part is serving a million people at once.

The idea to keep: a URL shortener is a giant, read-heavy notebook with one question, "what's the long link for this code?" Scaling is making that question cheap.

What's the simplest version?

Start small: one server, one table, two columns.

short_codelong_url
aZ3k9Qhttps://example.com/some/very/long/page?with=tracking&and=more

Two jobs. Create: make a code, save the pair. Redirect: find the pair, send the visitor onward. Click Next and follow one click.

Browser
Server
Table

Browser opens short.ly/aZ3k9Q

Step 1 of 4
The whole redirect: one lookup, one reply.

Notice there's exactly one question being asked. So where do the codes come from?

Where do the codes come from?

First bottleneck: making a code that nobody else has. Three common ways.

WayHowCatch
Counter1, 2, 3, ... then shorten the numberOne counter is a traffic jam, and codes are easy to guess
RandomPick random characters, retry if takenNeeds a check on every create
HashScramble the long link, keep the first bitTwo links can collide, so you still need a check

The trick for short codes is base62. That means writing a number using 62 symbols (a-z, A-Z, 0-9) instead of 10 digits, so it comes out much shorter, like writing a big number in a bigger alphabet.

A 7-character base62 code gives 62^7 = 3,521,614,606,208 options (about 3.5 trillion).

A collision is two links getting the same code, like two people given the same locker number. How big does this get?

How big does it get?

A quick estimate (labelled, not measured). Everything below is my assumption, not real data.

  • Assume 100 million new links a month, and 100 times more clicks than creates.
  • A month is about 2.6 million seconds (30 x 86,400). So creates are about 100M / 2.6M = ~40 per second, and clicks are about 10B / 2.6M = ~3,900 per second.
  • Assume 500 bytes per row. Five years of links is 6 billion rows, so about 3 TB.
  • Random 7-character codes at 6 billion rows: 6B / 3.5T is about 0.17% collisions at the very end. Retrying handles that.

Writes are tiny, reads are the whole show. So how should the redirect reply?

Should the redirect be 301 or 302?

Your server answers with a redirect status code. RFC 9110 defines both.

301 Moved Permanently

Permanent, and "heuristically cacheable", so browsers may remember it and skip your server.

302 Found

Temporary, so the client keeps using the original link. Every click comes back to you.

301 means less traffic but fewer click counts. 302 costs traffic but you see every click and can change the target later, so it suits analytics. (307 and 308 are the same ideas, but never change the request method.)

What happens when the database gets tired?

At ~3,900 reads a second (my estimate), the same popular links get asked again and again. Fix: a cache, a small, fast memory shelf in front of the database. Like keeping the books you use most on your desk instead of walking to the shelves each time.

1Click
2Check cache
3Hit: answer instantly
4Miss: ask database, then fill cache
Popular links stay in memory.

Memory is limited, so something must be thrown out when it's full. Redis (a popular cache) lets you set a maxmemory limit and an eviction policy. Its docs suggest allkeys-lru (evict the least recently used keys) as a good default when a small set of items gets most traffic.

A rough estimate: 10 million hot links at 500 bytes is about 5 GB, which fits in one machine's memory (my arithmetic). Still too much traffic for one database?

What if one database still can't answer everyone?

The fix for read-heavy traffic is replicas: extra copies of the database that only answer reads. Creates go to the main copy, and clicks spread over the copies. Drag the slider to 6 and watch the load per server shrink.

3
8
8
8

24 requests ÷ 3 read replicas = 8. Each server handles 8 requests.

A toy: 24 reads shared equally. Real traffic isn't this even.

With 6 replicas each one answers 4 of the 24 reads. Why this matters: the main copy stays free for creates.

Version oneOne database answers every read and write.
→
ScaledOne main copy for writes, replicas for reads.

The catch: a replica can lag a moment behind, so a brand-new link might not be there yet. A retry on the main copy covers it.

What about IDs from many machines?

With many app servers creating links, a single counter becomes the jam. Twitter hit a version of this with tweet IDs. Their announcement states that they needed tens of thousands of IDs per second, roughly time-sortable, in 64 bits, with no coordination. Their answer, Snowflake, builds an ID from a timestamp, a worker number and a sequence number.

The original repo lays out the bits like this:

41 bits: time in milliseconds
10 bits: which machine
12 bits: counter within that millisecond
One 64-bit number, three parts. No chatting between machines.

Two machines can't clash, because the machine number is baked in. Time sits at the front, so newer IDs are bigger. It relies on synced clocks.

Do you need that? Probably not. Base62 of a 63-bit number is about 11 characters (my arithmetic), longer than our 7. A simpler idea of mine: hand each app server a block of counter numbers.

Design it yourself

  1. Clicks jump 10 times. Cache, replica or bigger database first, and why?
  2. One link goes viral and one cache key gets hammered. What now?
  3. Users want to delete a link. Which redirect code makes that easier, 301 or 302?
Key takeaways
  • A shortener is a lookup table: code in, long link out. Reads dwarf writes.
  • Estimate first, then fix the real bottleneck: cache for hot links, replicas for read load.
  • 301 can be cached by browsers; 302 keeps every click coming to you.
  • Snowflake-style IDs (time + machine + sequence) let machines make unique IDs without talking to each other.

Sources

  • RFC 9110: HTTP Semantics, sections 15.4.2, 15.4.3, 15.4.8, 15.4.9
  • Twitter Engineering, Announcing Snowflake, 1 June 2010
  • twitter-archive/snowflake README for the bit layout
  • Redis docs, Key eviction
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
System Design · 4 minHow does video streaming work? (Why movies play on slow Wi-Fi)System Design · 5 minHow does a search engine find one page out of billions so fast?System Design · 5 minHow to scale a website from 1 user to 1 million users
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)