URL shortener inspired by Bitly and TinyURL: collision-free base62 codes from pre-allocated id ranges, a key-value link store with conditional writes, cache-first and edge redirects, async click analytics and malicious-link scanning. A teaching design.
See the collision and load problems of the obvious design.
What was missing: Nothing yet.
Why that's risky: Truncated hashes collide (two URLs get the same code) and must be detected and retried, and 50K redirects per second all hit one database.
What gets added: One server that makes codes by truncating a hash of the long URL and resolves every redirect with a database query.
Trade-offs: The same URL always gets the same code, at the price of collisions.
Generate unique codes without collisions or coordination, and store links in a store built for lookups.
What was missing: Codes collided and every redirect queried a relational table.
Why that's risky: Every click still reads the link store, and clicks outnumber creations a hundred to one.
What gets added: An ID range allocator that hands each shorten server a block of a million ids, which it encodes in base62 into 7-character codes; a key-value link store with conditional writes so custom aliases can't clash; and a redirect service.
Trade-offs: Sequential codes are guessable; shuffling ids within each range (or adding a few random bits) avoids that.
Serve almost every click from memory.
What was missing: Every click read the link store.
Why that's risky: A link going viral sends its whole audience to our servers.
What gets added: A link cache in front of the store; since links never change, entries simply expire.
Trade-offs: Using 302 instead of 301 means browsers don't cache redirects themselves, keeping clicks countable at the cost of more requests.
Answer popular links from servers near the visitor.
What was missing: All redirects reached our servers.
Why that's risky: Clicks aren't counted for creators, and nothing checks for malicious destinations.
What gets added: An edge cache that answers redirects for popular codes for a few minutes, logging each click.
Trade-offs: A disabled link can keep redirecting at the edge until its entry expires, so edge TTLs are kept short.
Count every click without slowing redirects, and stop malicious links.
What was missing: No analytics and no safety checks.
Why that's risky: Spammers can create links as fast as they like, and service health isn't watched.
What gets added: A click event stream (including edge-served clicks from edge logs) aggregated into an analytics store by time, country, referrer and device; and a safety scanner that checks new links against malware and phishing lists and disables bad ones.
Trade-offs: Analytics lag by a minute; new links are usable before scanning finishes and disabled if found bad.
Stop mass link creation and watch the service.
What was missing: No creation limits and no monitoring.
Why that's risky: Remaining risk is regional: the link store should be replicated so redirects keep working if a region fails.
What gets added: Rate limits on link creation per account and IP, and monitoring of redirect latency, cache and edge hit rates, id range usage and disabled links.
Trade-offs: Strict creation limits slow legitimate bulk users; they can use an authenticated API with higher limits.
Introduce key salting for popular URLs, cache hot entries aggressively, and shard by hashed key prefix.
Eventual consistency for replicas is acceptable with primary writes and fallback when lag increases.
Use request coalescing, soft TTLs, and background refresh to avoid stampedes.
A single service and database reduce complexity and help validate core functionality quickly.
Rate limiting prevents abusive spikes and preserves capacity for legitimate traffic.
Async analytics decouple heavy writes from the request path and keep redirect latency low.
You're in the middle of an interview session. Leaving now will end your current attempt.
Explore concept overviews, real-system examples, key tradeoffs, and interview talking points for each roadmap section.
You've conquered this phase. These are the skills you now own: