Design TinyURL — Cache Stampede & Viral Link Failures
One viral link caused 503s when LRU evicted the hot key before a 10x spike.
20+ years shipping production code across the stack, with years spent interviewing engineers. Drawn from code that ran under real load.
- ✓Deep production experience
- ✓Understanding of internals and trade-offs
- ✓Experience debugging complex systems
- TinyURL generates short codes via Base62 encoding of a unique 64-bit ID, guaranteeing no collisions.
- The system is read-heavy (100:1 ratio) — choose NoSQL (Cassandra) with Redis LRU caching.
- Distributed ID generation (Snowflake/ZooKeeper) is the backbone for collision-free scale.
- 301 redirects let browsers cache the mapping, reducing server load; 302 redirects pass through for analytics.
- Biggest mistake: using MD5 hashing for code generation — collisions force retry loops at scale.
Imagine every website address is a long home address like '123 Sunflower Lane, Apartment 4B, Springfield, Illinois, 62701, USA'. TinyURL is like a nickname system — you tell the post office 'call that address #XK9' and now anyone who says '#XK9' gets redirected to the full address instantly. The post office (the server) keeps a giant lookup book that maps short nicknames to long addresses. That's the whole system — a glorified, globally-distributed lookup book that has to handle billions of lookups per day without breaking a sweat.
Every senior engineer has sat across from an interviewer who says 'design a URL shortener' with a calm smile. It sounds trivial — take a long URL, make it short. But behind that smile is a question that probes distributed systems, database design, caching strategy, hash collision handling, rate limiting, analytics, and horizontal scaling simultaneously. Bit.ly processes over 600 million redirects per day. TinyURL has been alive since 2002. These systems are deceptively simple on the surface and genuinely hard to build correctly at scale.
The core problem is a deceptively asymmetric one: writes are rare, reads are overwhelmingly frequent. When you shorten a URL, that's a one-time write. But that short link might be embedded in a viral tweet and hit 10 million times in an hour. Your design has to reflect this read-heavy reality — every architectural choice from your hashing scheme to your cache eviction policy flows from that single insight.
By the end of this article you'll be able to walk into any system design interview and design TinyURL end-to-end: justify your short code generation strategy, design a DB schema that survives traffic spikes, build a caching layer that handles 99% of reads from memory, handle custom aliases and expiration, discuss analytics pipelines, and correctly answer every follow-up an interviewer throws at you. Let's build it.
Why TinyURL Design Tests More Than URL Shortening
The TinyURL design interview asks you to architect a URL shortening service — a system that maps long URLs to short, unique aliases and redirects clients on access. The core mechanic is a key-value lookup: given a short key (e.g., 7 characters from base62), return the original URL and issue an HTTP 302 redirect. This problem is a systems design classic because it forces you to reason about read-heavy workloads, collision-free key generation, and caching under extreme traffic.
In practice, the service must handle billions of writes (new URLs) and tens of billions of reads (redirects). Key properties that matter: key generation must be idempotent and collision-resistant (using distributed counters or pre-generated keys), redirect latency must stay under 10ms at P99, and the system must survive traffic spikes from viral links. A naive cache with a single Redis instance will collapse under a cache stampede when a popular link goes viral — every miss triggers a database read, overwhelming the DB and causing cascading failures.
You use this design pattern when you need a globally unique, short identifier for a resource and expect asymmetric read/write ratios (100:1 or higher). It matters in real systems because the same principles apply to CDN edge caching, distributed ID generation (Snowflake), and rate-limited API gateways. Getting the cache invalidation and key distribution wrong is the #1 cause of production outages in URL shorteners.
The Core Logic: Base62 Encoding vs. Hashing
In a URL shortener, the 'Magic' is how we generate the tiny string. You have two main paths: Hashing (MD5/SHA-256) or Base62 Encoding a unique ID. Hashing often leads to collisions that require complex 'check-and-retry' logic. The industry-standard approach is to use a distributed ID generator (like a Snowflake ID or a centralized Range Manager) and convert that numeric ID into a Base62 string (a-z, A-Z, 0-9).
For example, an ID like 125 converted to Base62 results in a short, predictable, and unique string. To prevent predictability (so people can't guess the 'next' URL), we can add a bit of salt or shuffle our Base62 alphabet.
Data Layer Strategy: Handling Scale and Redirection
Since this is a read-heavy system (100:1 read/write ratio), our database choice and caching strategy are critical. We use a NoSQL database like Cassandra or a sharded MongoDB for the URL mappings because we don't need complex joins—just a simple Key-Value lookup.
To achieve sub-millisecond redirects, we put a Redis cache in front of the database. We use an LRU (Least Recently Used) eviction policy because in the real world, 20% of the links (the viral ones) will generate 80% of the traffic.
Distributed ID Generation: The Backbone of Uniqueness
The unique ID that feeds into Base62 encoding must be globally unique across all servers. A simple auto-increment DB column doesn't scale — you'd have a single point of contention. The standard pattern is to use a distributed ID generator. Two common approaches: Snowflake (Twitter's algorithm) and ZooKeeper-managed ID ranges.
Snowflake generates 64-bit IDs: timestamp (41 bits) + machine ID (10 bits) + sequence (12 bits). This gives 4096 IDs per millisecond per machine, and the IDs are time-sortable. ZooKeeper assigns a range of IDs (e.g., 0-100000) to each app server; when exhausted, the server requests a new range. Both avoid collisions without a central DB write bottleneck.
In production, you'll also want to make the short code appear random. You can shuffle the Base62 alphabet permanently or XOR the ID with a secret before encoding. That prevents users from guessing sequential short codes and scraping all URLs.
- Snowflake: each server gets a unique machine ID and produces tickets from its own counter — no coordination needed.
- ZooKeeper: servers request fresh ticket blocks from a central coordinator. ZooKeeper is the single source of truth for block allocation.
- Both methods guarantee collision-free IDs without a central DB sequence bottleneck.
- Shuffle the Base62 alphabet to obscure sequential IDs from users.
- Clock skew in Snowflake can cause ID collisions or negative timestamps — use NTP and monitor clock drift.
Caching Strategy: Surviving the Viral Spike
We already mentioned a Redis cluster with LRU eviction. But to really survive a viral spike, you need a multi-layer caching strategy. The first layer is an in-memory cache (like Caffeine or Guava on each application server) that holds the hottest entries with a very short TTL (1-2 seconds). The second layer is a Redis cluster, and the third is the database.
When a request arrives, the app server checks its local cache first. On miss, it queries Redis. On Redis miss, it queries the database and then populates both caches. To prevent a stampede (thundering herd) when a cached key expires, use a distributed lock or a get-or-compute pattern. Only one thread should reload a cache entry; others should wait or serve a stale value.
For short codes that go viral, you can proactively pin them to dedicated cache nodes or increase their priority. Use consistent hashing for the Redis cluster so that adding nodes doesn't cause mass cache invalidation.
Analytics and Click Tracking Pipeline
A URL shortener is not just about redirection — it's a data business. Every click is valuable analytical data: geo-location, referrer, user agent, timestamp. You can't afford to write this data synchronously during a redirect (that would add latency). The pattern is asynchronous: the web server publishes a click event to a message queue (Kafka) and returns the 302/301 immediately. A separate consumer processes these events and updates the click_count in the database and aggregates data for dashboards.
Kafka topics can be partitioned by short key to maintain ordering per URL. The consumer can batch updates to the database (e.g., update click_count = click_count + 1 for 100 events at once). For real-time analytics, use a stream processor (Spark Streaming, Flink) to compute counters down to 1-minute granularity.
We also need to handle deduplication: users may refresh or multiple bots may click. Use a combination of IP + user agent + timestamp window to filter duplicates, or accept a small error percentage (most shorteners tolerate 1-2% overcount).
Why Your First Base62 Implementation Will Burn in Production
Every junior engineer starts with the same trap: integer ID → Base62 string → short URL. Simple. Elegant. Wrong for any system that survives more than a single server reboot.
The problem? The conversion is reversible. Anyone who gets a short URL can enumerate your entire ID space. They can scrape every URL you've ever shortened. Your competitor can map your traffic patterns. Your private links become public.
Production systems don't use sequential IDs for exactly this reason. You need unpredictable short codes. The industry standard is a random token (system-generated UUID or Snowflake ID) that has zero correlation to the storage key. Base62 only enters the picture when you need a human-readable representation of that random token.
But wait — random tokens collide. That's fine. You detect the collision, regenerate, and retry. At 62^6 possibilities, collisions are statistically irrelevant at any sane scale. The real cost is the retry overhead in your write path.
How TikTok Handles the Viral Spike That Kills Naive Caches
Your caching strategy looks great on paper. 80% cache hit rate. Redis cluster with replication. Eviction policy set to LRU. Then a celebrity tweets your shortened link and your cache gets eviscerated.
The problem isn't the hot key — it's the thundering herd of cold keys. A viral event means millions of requests for URLs that have never been cached. Every one of those requests hits your database. The database melts. The site goes dark.
The fix is shockingly simple: cache-aside with a distributed mutex. Before hitting the database for a cache miss, acquire a lightweight lock (Redis SETNX) scoped to the short code. Only the first requestor actually queries the database. The rest wait a few milliseconds and retry the cache.
TikTok's approach goes further: they pre-warm the cache for known high-traffic content. For TinyURL, that means tracking URL creation velocity. If a new short URL gets 100 redirects in its first minute, it's categorized as "viral candidate" and all its metadata gets promoted to the L1 cache tier proactively.
The Database Sharding Strategy Nobody Teaches You
Every blog post tells you to shard by user ID. Great for Instagram. Terrible for TinyURL. A single user creating 10,000 URLs per second is a normal day. Sharding by user means one hot shard handles all writes for power users while others sit idle.
The better approach: shard by the short code's first character. With 62 possible first characters, you get automatic load distribution. The write throughput is uniform because short codes are random. Read throughput follows the same pattern — viral URLs spread evenly across shards.
But here's the gotcha: range queries on creation time become impossible. Need to find all URLs created in the last hour? You must query all shards. That's fine for analytics — you batch those queries and accept the latency. The redirect path stays fast because it's a point lookup.
Pro tip: use consistent hashing with virtual nodes on the short code. If you add a shard, only 1/62nd of your data moves. You don't need to rebalance the entire cluster.
Alternative Approaches: Snowflake vs Redis vs Database Sequences
Choosing the right ID generation strategy is critical for TinyURL's scalability. Three common approaches are Snowflake, Redis, and database sequences. Snowflake (used by Twitter) generates 64-bit unique IDs using a timestamp, worker ID, and sequence number. It's decentralized and fast but requires clock synchronization. Redis offers atomic INCR commands with optional persistence, providing low-latency ID generation but introducing a single point of failure unless clustered. Database sequences (e.g., PostgreSQL SERIAL) are simple but become a bottleneck under high write loads. For TinyURL, Snowflake is ideal for distributed systems needing high throughput, while Redis suits moderate scales with caching needs. Database sequences are best for small deployments. Example: Snowflake ID = timestamp (41 bits) + worker ID (10 bits) + sequence (12 bits).
TinyURL with Analytics: Click Tracking and Dashboards
Analytics are essential for understanding link performance. Click tracking involves capturing each redirect event with metadata: timestamp, IP address, user agent, referrer, and geolocation. This data is streamed to a message queue (e.g., Kafka) and processed asynchronously to avoid slowing down redirects. A separate analytics service aggregates data into time-series databases (e.g., InfluxDB) or OLAP stores (e.g., ClickHouse) for dashboard queries. Dashboards display metrics like total clicks, unique visitors, geographic distribution, and click-through rates over time. For real-time updates, use WebSocket connections or periodic polling. Example: A TinyURL click event triggers a POST to /analytics with payload {short_code, timestamp, ip, user_agent}. The analytics service enriches the IP with GeoIP data and writes to Kafka. A consumer updates Redis sorted sets for hourly counts and ClickHouse for long-term storage.
Custom Short URLs: Base62 vs Base64URL Encoding Comparison
Custom short URLs often use encoding schemes to represent numeric IDs as short strings. Base62 uses 62 characters (a-z, A-Z, 0-9) and is case-sensitive, producing strings like 'abc123'. Base64URL is a variant of Base64 that replaces '+' and '/' with '-' and '_' to be URL-safe, using 64 characters. Base62 is more human-readable and avoids ambiguous characters (e.g., 'l' vs '1'), but Base64URL is more compact (shorter strings for the same numeric range). For example, encoding ID 123456789: Base62 yields '8M0kX' (5 chars), while Base64URL yields '7cDf' (4 chars). However, Base64URL may include '-' and '_' which can be less user-friendly. For TinyURL, Base62 is preferred for custom short URLs because it's easier to type and remember. Base64URL is better for machine-generated links where compactness matters. Both require padding removal and careful handling of collisions.
Cache Stampede Took Down Viral Link
- Always design for traffic spikes that are 50x your mean load.
- Cache stampedes are silent until they kill your DB.
- A two-layer cache (local + distributed) with coalescing is necessary for viral scenarios.
redis-cli -p 6379 INFO stats | grep 'keyspace_hits|keyspace_misses'nodetool cfhistograms url_mapping url_mapping| File | Command / Code | Purpose |
|---|---|---|
| io.thecodeforge.shortener.Base62Encoder.java | /** | The Core Logic |
| SchemaDesign.sql | CREATE TABLE io_thecodeforge.url_mapping ( | Data Layer Strategy |
| io.thecodeforge.shortener.SnowflakeIdGenerator.java | /** | Distributed ID Generation |
| io.thecodeforge.shortener.CacheService.java | public class CacheService { | Caching Strategy |
| io.thecodeforge.shortener.ClickEventPublisher.java | public class ClickEventPublisher { | Analytics and Click Tracking Pipeline |
| UnpredictableShortCode.py | from typing import Optional | Why Your First Base62 Implementation Will Burn in Production |
| CacheMutexRedirect.py | from typing import Optional | How TikTok Handles the Viral Spike That Kills Naive Caches |
| ShardResolver.py | from typing import List, Dict | The Database Sharding Strategy Nobody Teaches You |
| snowflake_id_generator.py | class Snowflake: | Alternative Approaches |
| click_tracker.py | from flask import Flask, request, jsonify | TinyURL with Analytics |
| base62_vs_base64url.py | BASE62_ALPHABET = string.ascii_letters + string.digits | Custom Short URLs |
Key takeaways
Interview Questions on This Topic
How would you generate unique short codes in a distributed system?
Frequently Asked Questions
20+ years shipping production code across the stack, with years spent interviewing engineers. Drawn from code that ran under real load.
That's System Design Interview. Mark it forged?
7 min read · try the examples if you haven't