Design a URL Shortener (TinyURL, bit.ly)
Design a service that turns long URLs into short codes and redirects at very high read volume, covering key generation, storage, caching and analytics.
Intermediate · about 4 hours · difficulty 2/5
Learn first: Unique ID Generation at Scale, Cache Strategies, Eviction and Invalidation, API Design: REST, gRPC and GraphQL
The URL shortener is the most common first HLD question. It is small enough to finish and deep enough to probe estimation, ID generation, caching and data modelling.
Requirements
- Functional: create a short link (optionally a custom alias and expiry), redirect to the original URL, basic click analytics.
- Non-functional: very low redirect latency, high availability, read-heavy (100:1 is a fair assumption), short codes must not collide.
Estimation
100 million new links a month gives about 40 writes per second and about 4,000 redirects per second, with peaks several times higher. 5 years of 500-byte records is roughly 3 TB.
Generating short codes
- Base62 (a to z, A to Z, 0 to 9): 7 characters give 62^7, about 3.5 trillion codes.
- Counter plus Base62: a unique ID (Snowflake, or ranges handed out by a ticket server so each app server encodes its own block) encoded in Base62. No collisions, but codes are guessable; shuffle or add a salt if that matters.
- Hash (MD5/SHA) and take 7 characters: needs collision checks and retries.
- Pre-generated key service: a pool of unused random codes; servers take batches.
Storage and API
POST /urls returns the code; GET /{code} returns 301 (permanent, browsers cache it, fewer hits to you, weaker analytics) or 302 (temporary, every click reaches you, better analytics). A key-value table code to long_url, expiry, owner; a key-value or wide-column store fits naturally, Postgres also works at this scale with sharding by code.
Scaling reads
Cache hot codes in Redis (cache-aside, LRU); a small fraction of links gets most traffic. Put a CDN or edge redirect in front for viral links. Record clicks asynchronously through a queue into an analytics store so redirects stay fast.
Pitfalls
- Synchronous analytics writes on the redirect path.
- Ignoring abuse: phishing links, spam bots creating millions of links (rate limits, URL scanning).
You are ready to move on when you can present this design in 35 minutes including estimates, code generation choice with trade-offs, data model, cache and the 301 vs 302 decision.
Key concepts
- Requirements and read/write ratio
- Base62 encoding
- Counter vs hash vs key pool
- 301 vs 302 redirects
- Caching hot links
- Async click analytics
- Custom aliases and expiry
- Abuse prevention
Mini project
Write a 2-page design doc and build a working prototype: Base62 codes from a range-allocated counter, Postgres storage, Redis cache-aside on redirect, and click events pushed to a queue and aggregated per day. Load test redirects and report p99.
Check yourself
Why would choosing 301 over 302 reduce load on your servers but make your click analytics less accurate?
Free resources
- The System Design Primer (Pastebin and bit.ly solution) (GitHub)
- ByteByteGo System Design 101 (ByteByteGo)
- Awesome System Design Resources (GitHub)