1. Problem Statement
A URL shortener maps arbitrary long URLs to short, unique 7-character aliases (e.g., `https://tiny.url/s7x8q2z`). When clients invoke the short link, the service redirects them to the original destination via HTTP 301 or 302 with minimal latency.
2. Clarifying Questions to Ask the Interviewer
**Scale & Traffic Ratio:** What is the expected read versus write traffic? *(Assumption: 100:1 read-to-write ratio; 100M URLs created per month, 10B redirects per month).***URL Lifetime & Expiry:** Do links expire automatically or remain permanent? *(Assumption: Default expiration is 5 years; custom TTLs supported).***Key Characteristics:** Are short codes purely alphanumeric, and can users provide custom aliases? *(Assumption: Base62 `[0-9a-zA-Z]`, 7 characters; custom aliases up to 16 characters permitted).***Latency SLA:** What is the target latency for the redirect path? *(Assumption: P99 < 15ms globally).***Analytics:** Do we need real-time click metrics (geo, referrer, timestamps)? *(Assumption: Yes, captured asynchronously without impacting the critical read path).*
3. Functional and Non-Functional Requirements
Functional Requirements
1. Given a long URL, generate a unique 7-character short alias.
2. When accessing a short alias, redirect the user immediately to the long URL.
3. Support optional custom aliases and configurable expiration timestamps.
4. Collect aggregate telemetry (click counts, referrers, geo distribution).
Non-Functional Requirements
1. **High Availability:** 99.99% uptime on the read path; redirects must never fail.
2. **Ultra-Low Latency:** P99 read latency < 15ms via aggressive edge caching.
3. **Zero Collisions:** Hash or ID generation must mathematically eliminate duplicate short keys.
4. **Durability:** Once confirmed, short code mappings cannot be lost.
4. Back-of-the-Envelope Estimates
**Write QPS:** ~40 writes/sec (Peak: 100 writes/sec).**Read QPS (100:1):** ~4,000 reads/sec (Peak: 10,000 reads/sec).**Storage Capacity (5 Years):** 6 Billion records * 500 bytes = 3 TB over 5 years.**Cache Memory (80/20 Rule):** 20% of daily reads (345M) * 500 bytes = ~34.5 GB RAM in Redis.
5. API Design
`POST /api/v1/urls` -> creates short code mapping.`GET /{short_code}` -> returns HTTP 302 Found redirect with `Location` header.
6. Data Model & Database Choice
**Choice:** Distributed NoSQL Key-Value Store (DynamoDB or Cassandra).**Schema:** Partition Key `short_code` (String), `long_url` (String), `user_id` (UUID), `created_at` (Timestamp), `expires_at` (TTL timestamp).
7. High-Level Architecture
Edge CDN offloads hot viral links. API nodes request 1M pre-allocated ID blocks from Key Generation Service (ZooKeeper/Redis), increment locally, and encode to Base62 without database collisions. Redis cluster caches active reads.
8. Deep Dive into Hard Parts
**Zero Collision Key Generation:** Pre-allocated integer range blocks encoded to Base62 ensure zero database lock contention and zero collisions.**Cache Stampede Prevention:** Singleflight mutex locking on cache misses prevents thundering herds from overloading DynamoDB.
9. Trade-offs and Alternatives
HTTP 301 delegates caching to browser but blinds analytics; HTTP 302 enables full telemetry at minimal origin overhead.
10. Key Takeaways
Decouple write-path uniqueness from hashing via range-based Base62 counter generation.Favor HTTP 302 over 301 for analytics fidelity and fraud revocation.Prevent cache stampedes on viral links using Singleflight distributed mutex locks.