1. Problem Definition & Scale Estimation
TinyURL receives long URLs and generates compact 7-character aliases (e.g. `https://tinyurl.com/aB3x9Z`). The system processes 100 million new URLs per month (write QPS: ~40) and 10 billion redirections per month (read QPS: ~4,000, peaking at 20,000).
The read-to-write ratio is 100:1. The key non-functional requirements are low-latency redirection (<15ms) and 100% alias uniqueness without collision.
2. Encoding Strategies: MD5 Hash vs Base62 Distributed Counter
Approach 1: Hashing the original URL with MD5/SHA256 and taking the first 7 characters causes hash collisions that require costly database checks.
Approach 2 (Recommended): Assign every URL a unique 64-bit auto-incrementing integer ID, then convert the integer into a Base62 string using alphanumeric characters `[a-z, A-Z, 0-9]`. A 7-character Base62 string yields $62^7 \approx 3.52$ trillion unique URLs—more than enough for decades.
3. Distributed Range Generation with ZooKeeper
To prevent a single database auto-increment counter bottleneck, an Apache ZooKeeper or Etcd cluster acts as a Range Coordinator. Each application server requests a range of 1,000,000 IDs (e.g. Server 1 gets IDs 1–1,000,000; Server 2 gets 1,000,001–2,000,000).
Servers increment IDs in local memory without inter-node locks. If a server crashes, unused IDs in its allocated block are skipped without compromising uniqueness.
4. Caching Hierarchy & HTTP 301 vs 302 Redirection
Hot URL mappings are cached in Redis using an LRU eviction policy. The Pareto 80/20 rule applies: 20% of URLs drive 80% of traffic.
When redirecting, HTTP 301 (Permanent Redirect) instructs browsers to cache the destination locally, reducing server load. HTTP 302 (Temporary Redirect) forces every request through the shortener, enabling accurate click analytics and geo-tracking.
Model this architecture on the HLDForge Canvas
Turn static box diagrams into living, collaborative systems. Drag and drop microservices, Kafka clusters, and cache tiers. Run automated SPOF detection and STRIDE threat modeling in real time.
Written by HLDForge Architecture Team
Staff Systems Architect
The HLDForge Architecture Team publishes practical engineering guides, real-world case studies, and high-concurrency blueprints to help engineers build scalable and fault-tolerant software.