Design a URL Shortener: Code Generation, Fast Redirects, and Scaling
Company: Vercel
Role: Software Engineer
Category: System Design
Difficulty: easy
Interview Round: Onsite
Design a URL shortening service. A user submits a long URL and receives a short link; anyone who opens the short link is redirected to the original URL.
Cover how short codes are generated and kept unique, how links are stored and looked up, how the redirect path stays fast under a read-heavy load, and how the service scales and handles failures.
```hint Separate the two paths
Creating a link and following a link have very different volumes and latency needs. Design each one on its own terms.
```
```hint Where do codes come from?
Decide how a new short code is produced so that two concurrent requests can never receive the same code, and work out how long the code has to be.
```
### Clarifying Questions
- What are the expected rates of link creation and of redirects, and how many links must be kept in total, for how long?
- Do links expire? Can users delete or edit them?
- Can users choose custom aliases?
- If the same long URL is submitted twice, should it get the same short code or a new one?
- Are click analytics required, and how fresh must they be?
- Must short codes be hard to guess, or is a sequential-looking code acceptable?
- Should the redirect be permanent or temporary, given the effect on browser caching and click counting?
- Are there abuse requirements, such as blocking malicious destination URLs or rate-limiting link creation?
### What a Strong Answer Covers
- Functional and non-functional requirements, with rough capacity estimates derived from stated assumptions
- A short-code generation scheme with an argument for uniqueness under concurrency, and the code-length calculation
- A data model and storage choice suited to a read-heavy key-value workload
- The redirect read path: caching, the choice of redirect status code, and latency
- Scaling (partitioning, replication, multiple regions), failure handling, and abuse prevention
- Analytics kept off the redirect's critical path
### Follow-up Questions
- A single short link goes viral and receives a large share of all traffic. What happens in your design, and what would you change?
- How would you support custom aliases without letting them collide with generated codes?
- How do you add expiration, and is it safe to reuse expired codes?
- How would you count clicks accurately if some redirects are served from a CDN or a browser cache?
Overview: Design a URL shortening service that creates short links and redirects visitors to the original URLs. It tests capacity estimation, unique short-code generation under concurrency, data modeling for a read-heavy workload, a fast cached redirect path, redirect status choices, analytics, and scaling and failure handling.