URL Shortener (like bit.ly)¶
Quick Reference Guide for System Design Interviews
Problem Statement¶
Design a URL shortening service that creates short aliases for long URLs. Users should be able to create short URLs and be redirected to the original URL when accessing the short link.
Requirements¶
Functional Requirements¶
- Shorten a long URL to a short URL
- Redirect short URL to original URL
- Optional: Custom short URLs
- Optional: Expiration time
- Optional: Analytics (click count, location, referrer)
Non-Functional Requirements¶
- Availability: 99.99% (redirects are critical)
- Latency: < 100ms for redirect
- Scalability: Handle 100M URLs, 10B redirects/month
- Durability: URLs should never be lost
Back of Envelope Estimation¶
High-Level Architecture¶
Short URL Generation¶
Approach 1: Base62 Encoding of Counter¶
Approach 2: Hash-Based¶
Approach 3: Pre-Generated Keys (Recommended)¶
Database Schema¶
-- URL Mapping Table
CREATE TABLE url_mappings (
short_url VARCHAR(10) PRIMARY KEY,
long_url TEXT NOT NULL,
user_id UUID,
created_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP,
click_count BIGINT DEFAULT 0,
INDEX idx_user_id (user_id),
INDEX idx_created_at (created_at)
);
-- For custom URLs
CREATE TABLE custom_urls (
custom_alias VARCHAR(50) PRIMARY KEY,
short_url VARCHAR(10) REFERENCES url_mappings(short_url),
user_id UUID NOT NULL,
created_at TIMESTAMP NOT NULL
);
-- Analytics (separate table or time-series DB)
CREATE TABLE click_events (
id UUID PRIMARY KEY,
short_url VARCHAR(10),
timestamp TIMESTAMP NOT NULL,
ip_address INET,
user_agent TEXT,
referrer TEXT,
country VARCHAR(2),
INDEX idx_short_url_time (short_url, timestamp)
);
Caching Strategy¶
Redirect Types¶
API Design¶
Analytics Pipeline¶
Data Partitioning¶
Security Considerations¶
Interview Discussion Points¶
- How do you ensure unique short URLs?
- Pre-generated key service (recommended)
- Counter with base62 encoding
-
Hash with collision handling
-
How do you handle high read traffic?
- CDN caching for redirects
- Redis cache for hot URLs
-
Read replicas
-
301 vs 302 redirect?
- 302 recommended for analytics
-
301 for permanent, SEO-focused use
-
How do you handle URL expiration?
- expires_at column
- Background job for cleanup
-
Check on redirect
-
How do you scale writes?
- Database sharding
- Pre-generated keys
- Async analytics
Quick Reference for System Design Interviews