# System Design Interview: Designing a URL Shortener The URL shortener (think bit.ly) is a classic warm-up design question. Here's a structure that fits in 35 to 45 minutes. ## Requirements - Functional: create a short link for a long URL; redirect short to long; optional custom alias and expiry. - Non-functional: very read-heavy (often 100 reads per write), low-latency redirects, links must not collide. - Rough scale: 100M new links a month means about 40 writes a second and 4,000 reads a second. ## Generating the short key 1. **Counter + Base62:** a globally unique ID (database sequence, or a Snowflake-style ID) encoded in Base62. Seven characters give 62^7, about 3.5 trillion keys. Predictable, so add a shuffle if guessing links is a concern. 2. **Hash and truncate:** MD5 or SHA-256 of the URL, then take 7 characters. Needs collision handling (retry with a salt). 3. **Pre-generated key service:** a separate service hands out unused keys in batches; there's no collision check on the hot path. ## Storage and API - `POST /links {long_url}` returns `{short}`; `GET /{short}` returns a 301 or 302 redirect. - 301 is cached by browsers (less load, but you lose click analytics); 302 keeps every hit visible. - A key-value store (DynamoDB, Cassandra) keyed by the short code is a natural fit. ## Scaling reads Put a cache (Redis) in front of the database; a small fraction of links gets most of the traffic. Add a CDN or edge redirects for the hottest links. ## Extras to volunteer Analytics via an async event stream (Kafka), abuse and malware scanning on creation, and a TTL-based cleanup job for expired links. --- *Disclosure: I'm on the team behind [Linkjob AI](https://www.linkjob.ai/), an interview assistant.*