Case Study: URL Shortener¶
A URL shortener (like bit.ly) maps short URLs to long URLs. Core: hash/encode long URL → short ID, store mapping in DB, redirect on access. Scale: ~100M URLs/month, read-heavy (100:1 read/write). Use Base62 encoding or counter-based ID, cache hot URLs in Redis, replicate DB for reads. Key decisions: custom aliases, analytics, TTL/expiration.
System Design¶
Step 1: Requirements
Functional: - Given a long URL, generate a short URL - Redirect short URL → long URL - Optional: custom aliases, expiration, analytics
Non-Functional: - Low latency redirects (< 50ms) - Highly available (redirects must always work) - Short URLs should be hard to guess
Estimation (100M new URLs/month): - Write: ~40 URLs/sec - Read: ~4000 redirects/sec (100:1 ratio) - Storage: 100M × 500 bytes ≈ 50 GB/year
Step 2: High-Level Design
API:
Step 3: Short URL Generation
Approach 1: Base62 Encoding of Auto-Increment ID
ID: 12345 → Base62: "3dE"
Character set: [a-z, A-Z, 0-9] = 62 chars
6 chars → 62^6 = 56 billion combinations
7 chars → 62^7 = 3.5 trillion combinations
Approach 2: Hash + Truncate
MD5("https://example.com/long/path") → first 7 chars
Problem: collisions. Check DB, retry if collision.
Approach 3: Pre-generated Key Service
Step 4: Database Design
CREATE TABLE url_mappings (
id BIGINT PRIMARY KEY,
short_code VARCHAR(7) UNIQUE NOT NULL,
long_url TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
expires_at TIMESTAMP,
click_count BIGINT DEFAULT 0
);
CREATE INDEX idx_short_code ON url_mappings (short_code);
Read-heavy → cache hot URLs in Redis:
Step 5: Scaling
- Cache: Redis for frequently accessed URLs (80/20 rule)
- DB replication: Read replicas for redirect lookups
- DB sharding: Shard by hash of short code
- CDN: Cache 301 redirects at edge
- Rate limiting: Prevent abuse of URL creation
Common Follow-up Questions
- How do you handle collisions?
- How do you implement analytics (click tracking)?
- How do you handle URL expiration?
- What HTTP status code: 301 (permanent) vs 302 (temporary)?
- How would you prevent abuse?