A short link is just a lookup. Build one on a single machine, then watch each bottleneck show up and get fixed.
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.
Start small: one server, one table, two columns.
| short_code | long_url |
|---|---|
| aZ3k9Q | https://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 opens short.ly/aZ3k9Q
Notice there's exactly one question being asked. So where do the codes come from?
First bottleneck: making a code that nobody else has. Three common ways.
| Way | How | Catch |
|---|---|---|
| Counter | 1, 2, 3, ... then shorten the number | One counter is a traffic jam, and codes are easy to guess |
| Random | Pick random characters, retry if taken | Needs a check on every create |
| Hash | Scramble the long link, keep the first bit | Two 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?
A quick estimate (labelled, not measured). Everything below is my assumption, not real data.
Writes are tiny, reads are the whole show. So how should the redirect reply?
Your server answers with a redirect status code. RFC 9110 defines both.
Permanent, and "heuristically cacheable", so browsers may remember it and skip your server.
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.)
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.
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?
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.
24 requests ÷ 3 read replicas = 8. Each server handles 8 requests.
With 6 replicas each one answers 4 of the 24 reads. Why this matters: the main copy stays free for creates.
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.
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:
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.