Requirements
I’ll assume users can optionally provide a custom alias, and if not, the system will generate one
Functional Requirements:
- CRUD Operations:
- Create a short URL for a given long URL.
- Return the long URL associated with a given short URL.
- Users can specify a custom short url (optional and must be unique)
- Redirect traffic to the actual long URL
Non-Functional Requirements:
- Low-Latency (redirects should happen quickly)
- High-Availability (links should remain usable, even if something fails)
- Eventual Consistency (because we want to prioritize low-tatency and high-availability)
- Durability / Fault-tolerance (created short url mappings should not be loss after creation)
- Horizontal Scalability (the system should handle increasing traffic and sudden spikes)
Entities (data models):
UrlMapping
- short_url (unique)
- long_url
- created_at
- updated_at
- user_id (fk links mapping to user it belongs to)
API Design
Define the APIs expected from the system. This is your chance to analyze and define the read and write paths so that you can come up with the high-level design...
Creating short -> long mapping
Create short url
POST /shorten
Body: {
long_url: string,
custom_alias?: string (optional)
}
Response: { short_url: string }
Redirect short url to long url
GET /{short_url: string}
→ 301/302 redirect to long_url
High-Level Design
Describe the overall system architecture. Identify the main components needed to solve the problem end-to-end. Use the diagramming tool to create a block diagram.
Components:
- Client (web / mobile)
- URL Shortener Service (backend app)
- Load Balancer
- Database
- Cache (hot short_url mappings instead of querying every time)
Responsibilities:
- Client
- User interface that communicates with the url shortener service
- Load Balancer
- Distribute traffic across the different instances of the url shortener service
- This is required in order to support horizontal scaling & increasing traffic
- URL Shortener Service
- Generates the short url if user did not provide one
- handles redirects
- Cache
- Store frequently/recently used mappings
- Database
- Persistently stores all mappings
Data Flow:
- For creating short url with or without custom short_url (Post /shorten & Post /create/{custom_short_url}):
- Client -> Load Balancer -> URL Shortener Service -> Database
- For redirecting short url (GET /{short_url}):
- Client -> Load Balancer -> URL Shortener Service -> Cache -> Database (fallback) -> redirect
Scaling:
- Cache -> reduces DB load & Latency
- Stateless Services -> horizontal scaling (the important state is stored in external DB & Cache)
- Load Balancer -> traffic distribution across stateless services
Detailed Component Design
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.