Design a URL Shortener
An educational design exercise, not a description of any company’s real system.
01Problem
Design a public URL shortening service. A user submits a long URL and receives a short link such as https://sho.rt/aZ3kQ9x. Anyone who opens the short link is redirected to the original URL. Signed-in users can choose a custom alias, set an expiry date and see how many times their links were clicked.
The system is extremely read-heavy: a link is created once and may be opened millions of times, often in sudden bursts when it is shared on social media. Redirects must feel instant everywhere, short codes must never point to the wrong destination, and the service must not become a convenient tool for spreading phishing or malware.
02Functional requirements
- Create a short link for a long URL; the service generates a unique 7-character code.
- Redirect `GET /{code}` to the original URL (HTTP 302) or return 404/410 if the code is unknown or expired.
- Optionally choose a custom alias (4–32 characters) and an expiry date when creating a link.
- Signed-in users can list and delete their links and see total and per-day click counts.
- Links without an expiry stay valid for at least 5 years.
03Non-functional requirements
- 100 million new links per month; reads outnumber writes about 100:1.
- Redirect latency p99 under 50 ms server-side (excluding the client network); creation p99 under 300 ms.
- Redirects available 99.99% of the time; creation 99.9% is acceptable.
- A short code, once issued, must always resolve to the same URL (no collisions, no reuse while the link exists).
- Click counts may lag by up to a few minutes and may be approximate (within about 1%).
- Generated codes should not be trivially enumerable in sequence.
04Clarifying questions
Ask these first. Open one to see the answer the interviewer gives.
What is the expected traffic, and how much more reading than writing?
Plan for 100 million new links per month and about 100 redirects per created link over its life, so roughly 100:1 reads to writes. Assume peaks of 5x the average.
Should a redirect be permanent (301) or temporary (302)?
Use 302. We need every click to reach us for analytics, and users can delete or expire links. A 301 would be cached by browsers and keep redirecting after deletion.
If two users shorten the same long URL, should they get the same code?
No. Each creation gets its own code so owners have separate analytics and can delete independently. Deduplication is not required.
How long do links live, and can codes be reused after a link expires?
Links without an expiry live at least 5 years. Expired or deleted codes are never reissued; the keyspace is large enough that reuse is unnecessary and reuse would send old traffic to a new destination.
How detailed must analytics be?
Total clicks and clicks per day per link, visible within a few minutes. Referrer and country breakdowns are a nice-to-have, not required now.
Do anonymous users get to create links?
Yes, but rate limited per IP and checked against a malicious-URL blocklist. Custom aliases and analytics require an account.
- Capacity estimation
- API design
- Data model
- High-level architecture
- Component deep dive
- Scaling strategy
- Failure handling
- Security
- Observability
- Trade-offs
- Alternative designs
- Follow-up questions
- Capacity estimation
- API design
- Data model
- High-level architecture
- Component deep dive
- Scaling strategy
- Failure handling
- Security
- Observability
- Trade-offs
- Alternative designs
- Follow-up questions