========================================================================
SYSTEM DESIGN SPECIFICATION: HIGH-PERFORMANCE URL SHORTENER
========================================================================
1. FUNCTIONAL REQUIREMENTS
------------------------------------------------------------------------
[User & Lifecycle Flows]
* Short Link Generation:
- Users submit a long URL via API or simple UI.
- System generates a short, unique URL alias.
- Support for optional, custom aliases (provided by user).
* Metadata Tracking:
- Every mapping must record: Long URL, Short Code, Expiration Timestamp,
Created Timestamp, and Owner ID (Standard/Premium).
* Status Lifecycle Management:
- ACTIVE: Short URL is live and routing traffic.
- PENDING: Code reserved/in-flight during creation.
- EXPIRED/ERROR: Link is deactivated; routes to a custom 404/expired page.
* Automated Expiration & Cleanup:
- Configurable Time-To-Live (TTL) per link (default 2 years).
- Daily asynchronous background worker deletes or archives expired records
from the database and evicts them from cache.
[Redirection Flow]
* High-Speed Resolution:
- Clicking a short URL resolves the target long URL if active and unexpired.
- Returns HTTP 301 (Permanent Redirect) for standard, unchanging links to
maximize browser caching and reduce system read load.
- Returns HTTP 302 (Temporary Redirect) for dynamic links requiring
real-time analytics/tracking.
2. NON-FUNCTIONAL REQUIREMENTS (NFR)
------------------------------------------------------------------------
[Performance & Latency]
* Read Latency (Redirection): < 20 ms for cached URLs locally; < 100 ms globally
across edge zones.
* Write Latency (Creation):
- Standard User Pool: < 500 ms (throttled/rate-limited via shared request queues).
- Premium User Pool: < 100 ms (dedicated high-priority compute/connection pools).
[Availability & Fault Isolation]
* Target SLA: > 99.99% availability for the redirection path; 99.9% for creation.
* Multi-Region Active-Active:
- Deployed across major global geographical regions.
- Any single regional datacenter failure must trigger seamless automated
Anycast/GeoDNS failover to nearest healthy region.
* Resilience:
- Rate-limiting enforced at the API Gateway using the Token Bucket algorithm.
- Circuit breakers isolate downstream write degradation without crashing the
high-priority read/redirection engine.
[Caching Optimization Strategy]
* Edge caching and centralized Redis cluster utilizing Least Recently Used (LRU)
eviction policy.
* Cache Population: 80/20 rule application (20% of the links generate 80% of traffic).
Hot links are cached indefinitely until explicit expiration.
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...
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.
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.
3. CAPACITY ESTIMATION & TRAFFIC ANALYSIS
------------------------------------------------------------------------
[Core Variables]
* Daily Active Users (DAU): 1,000,000 (1M)
* Total Registered Base: 10,000,000 (10M)
* Average Writes per User/Day: 10 URLs
* Read-to-Write Ratio: 10:1 (100 Reads per 10 Writes per DAU)
[Traffic & QPS Volumes]
* Total Writes per Day: 1M DAU * 10 = 10,000,000 (10M) writes/day
* Total Reads per Day: 10M * 10 = 100,000,000 (100M) reads/day
* Average Write QPS: 10,000,000 / 86,400 seconds = ~116 RPS
* Peak Write QPS (3x Avg): ~350 RPS
* Average Read QPS: 100,000,000 / 86,400 seconds = ~1,157 QPS
* Peak Read QPS (4x Avg): ~4,628 QPS
[Data Size & Storage Footprint]
* User Record Size: 1 KB (ID, Billing Tier, Metadata)
- Registered Base Storage: 10M * 1 KB = 10 GB (Static growth)
* URL Mapping Record Size: 2 KB (Long URL up to 2,048 chars, Short Code, Metadata)
- Daily URL Storage: 10M writes * 2 KB = 20,000,000 KB = 20 GB / day
- Annual URL Storage: 20 GB/day * 365 days = 7,300 GB = ~7.3 TB / year
- 5-Year Storage Target (Retention): 7.3 TB * 5 = 36.5 TB
[Network Bandwidth Calculations]
* Ingress (Write Path):
- Average: 116 RPS * 2 KB = 232 KB/s (~1.85 Mbps)
- Peak: 350 RPS * 2 KB = 700 KB/s (~5.6 Mbps)
* Egress (Read Path - Sending Redirect Headers):
- Expected HTTP Response size (301 Header + Location string) = ~500 Bytes
- Average: 1,157 QPS * 0.5 KB = 578.5 KB/s (~4.63 Mbps)
- Peak: 4,628 QPS * 0.5 KB = 2,314 KB/s (~18.5 Mbps)
[Cache Sizing (Redis RAM)]
* Goal: Cache 20% of daily read traffic volume.
* Daily unique reads needing caching: 100M reads * 20% = 20,000,000 keys.
* Cache Record Size (Only Key -> Long URL Mapping): ~1 KB
* Memory Required: 20,000,000 * 1 KB = 20,000,000 KB = ~20 GB RAM.
* Total Cluster Size (Including master-replica overhead & cushion): ~32 GB RAM.
- ========================================================================