Load/Bearing
High-level design · 12 of 22

URL shortener and ID generation

A tiny product with a real design question: how do you hand out billions of short, unique keys without a bottleneck?

4 min read

A URL shortener turns https://example.com/some/very/long/path?with=params into sho.rt/aZ3kQ9x and redirects anyone who visits the short link. The redirect has to be fast and almost never fail, which makes it a good exercise in caching and availability. The interesting part is generating the keys.

Requirements and numbers

QuantityMathResult
New links per second100M per month ÷ 2.6M seconds≈ 40/s average, ~120/s peak
Redirects per second100 reads per write≈ 4,000/s average, ~12,000/s peak
Rows after 5 years100M × 12 × 56 billion
Storage6B × ~500 bytes≈ 3 TB
Key space with 7 base62 characters62⁷≈ 3.5 trillion

Writes are tiny. Reads are 100 times larger and very skewed, since a few links go viral. Seven characters of [0-9a-zA-Z] give about 580 keys for every link you'll ever store.

CLIENTLBAPI 1statelessAPI 2statelessREDISkey → URLURL DBkey-valueKEY POOLpre-generated
Writes take a key from the pool and store the mapping. Redirects read Redis first and fall back to the database.

Generating keys

ApproachHowGoodBad
Hash the URLTake the first 7 base62 characters of MD5 or SHA-256 of the long URL.The same URL always gets the same key, so no duplicates.Collisions: check, and on a clash re-hash with a salt and retry.
Counter + base62A global counter; encode each new number in base62.No collisions; keys are as short as possible.One counter is a bottleneck and a single point of failure, and sequential keys can be enumerated.
Counter rangesEach server leases a block (say 1,000 numbers) from a coordinator.No per-request coordination.A crashed server wastes its block, which is fine. Keys are still guessable unless scrambled.
Pre-generated poolA key generation service creates random unused keys offline; servers take batches.No collision check at write time; random, unguessable keys.An extra service, and the pool must not run dry.

Collision odds for the hash approach: with 6 billion keys in a space of 3.5 trillion, each new key has about a 0.17% chance of hitting an existing one. One retry fixes nearly all of them, so hashing works fine. A common interview answer is counter ranges with the numbers scrambled (for example by a reversible bit shuffle) so the keys look random, or a pre-generated pool.

General-purpose IDs

The same problem shows up everywhere you need unique IDs without a central database sequence:

The redirect path

Storage

The data is a simple mapping looked up by primary key, with no joins, so a key-value store (DynamoDB, Cassandra) fits naturally and partitions by key. PostgreSQL works too at this size: 3 TB over five years is manageable with a few shards. Store short_key (primary key), long_url, owner_id, created_at and expires_at. Expired links can be deleted lazily on read and by a periodic cleanup job.

Abuse

Shorteners attract spam and phishing. Rate-limit link creation per user and per IP (see rate limiting, including how to keep counters consistent across N servers), check new URLs against a malware blocklist, and make custom aliases a logged-in feature.

Interview checklist

Practise it

Model it in the sandbox

Set the traffic from your estimate, then build the read and write paths and see which component turns red first.

Open the sandbox
Workshop drill

Do it by hand

Interactive drills for the parts the simulator can't show. Scored, with your best result saved in your browser.

Short keys and IDs

Further reading