Design a URL Shortener
What You Will Learn
Design a simple URL shortener from requirements through API, database, caching, and scaling decisions.
Core Idea
A URL shortener converts a long URL into a short identifier.
Example:
https://example.com/articles/very-long-title
|
v
https://sho.rt/aB92xCore requirements:
- Create a short URL.
- Redirect users to the original URL.
- Optionally support expiration.
- Handle many redirects.
A simple architecture is:
Client
|
Load Balancer
|
API Servers
|
+--> Redis Cache
|
+--> DatabaseThe database stores the mapping. Redis can cache popular short codes.
A Simple Mental Model
Client
|
v
Application
|
+--> Database
|
+--> Cache
|
+--> Background WorkerDo not worry about building every component at once. System design is mainly about understanding why a component exists, what problem it solves, and how components communicate.
Practical Example
Example APIs:
POST /urls
GET /:codeDatabase:
short_code
original_url
created_at
expires_atRedirect flow:
GET /aB92x
|
v
Redis
| hit
v
Original URL
Cache miss -> Database -> Redis -> RedirectA key design issue is generating unique short codes. A counter, random identifier, or encoded ID can be used depending on requirements.
Key Points
['Start with a simple relational database.', 'Cache frequently accessed redirects.', 'Use TTLs for expiring links.', 'Consider collisions and abuse prevention.', 'Measure redirect traffic before adding complex infrastructure.']
Common Beginner Mistakes
- Adding technologies before understanding the problem.
- Designing for millions of users when the requirements do not justify it.
- Ignoring failure cases.
- Treating every system as if it needs the same architecture.
- Focusing on diagrams instead of explaining the decisions behind them.
Quick Summary
The goal of this lesson is to build a simple mental model that you can reuse in future designs.
Next, the examples become more interactive with chat and notification systems.