Scaling a URL Shortener: Caching, Read Replicas and Sharding
Scale a URL shortener to billions of redirects: Redis cache-aside, CDN edge caching, read replicas, consistent-hash sharding and multi-region deployment.
Published November 27, 2026
A single server with a database can run a URL shortener for millions of links. Getting to billions of redirects per day with millisecond latency and no downtime takes a few well-known techniques layered in the right order: cache, replicate, shard, and distribute geographically. This article walks through each layer and when you actually need it.
The concept
Because the workload is overwhelmingly reads of immutable data, caching is the first and biggest lever. Link mappings almost never change, and click popularity follows a power law: a small fraction of links receive most of the traffic. A cache-aside Redis layer in front of the database can serve the vast majority of redirects from memory.
Next, the database itself. Read replicas absorb cache misses and give failover options. When data volume or write throughput outgrows one primary, shard by short code: the code is random-looking, so hashing it spreads data evenly, and every lookup knows exactly which shard to ask. Finally, deploy the stateless redirect service in multiple regions so users are served from nearby, with the cache warmed per region.
The scaled architecture
Cache-aside lookup
Why hit rate dominates
Sharding by short code
Practical examples
Cache-aside in Python with Redis
Also cache negative results briefly so scans for random codes do not hammer the database.
def resolve(code: str) -> str | None:
cached = r.get(f"l:{code}")
if cached == b"__none__":
return None
if cached:
return cached.decode()
url = db.fetch_long_url(code) # routed to the right shard
r.set(f"l:{code}", url or "__none__", ex=86_400 if url else 60)
return urlRedis memory estimate
Size the cache from the number of hot links.
hot_links = 50_000_000 # links clicked in the last day
bytes_each = 200 # key + URL + overhead
print(hot_links * bytes_each / 1e9, "GB") # 10.0 GB: one mid-size Redis node or a small clusterChoosing a shard
Consistent hashing with virtual nodes keeps movement small when adding shards.
import bisect, hashlib
ring = sorted((int(hashlib.md5(f"{s}#{v}".encode()).hexdigest(), 16), s)
for s in ["A", "B", "C"] for v in range(100))
def shard_for(code):
h = int(hashlib.md5(code.encode()).hexdigest(), 16)
i = bisect.bisect(ring, (h,)) % len(ring)
return ring[i][1]Common mistakes
- Sharding before adding a cache; caching alone often removes the need for years.
- Sharding by creation date or user, creating hot shards for today's links.
- No negative caching, letting bots that probe random codes reach the database on every request.
- A cache without TTLs, so disabled or expired links keep redirecting.
- Global strongly consistent writes across regions for data that is written once and read forever.
Key takeaways
- Cache first: redirects are immutable, power-law reads, ideal for Redis.
- Read replicas absorb misses and provide failover.
- Shard by hashed short code with consistent hashing when one primary is not enough.
- Run the stateless redirect tier in multiple regions near users.
FAQ
How do I invalidate a cached link when it is deleted?
Delete the cache key in the same code path that disables the link, and keep a reasonable TTL as a safety net.
Do I need Redis Cluster?
Only when the hot set exceeds one node's memory or throughput. A primary with a replica handles a surprising amount of traffic.
How do multiple regions handle writes?
Writes can go to a single home region (they are rare), or each region can generate codes from its own ID range and replicate asynchronously.