System DesignOct 25, 2026⏱️ 3 min read
Design a URL Shortener Like Bitly & TinyURL: Base62 Hashing & Read-Heavy Caching

Design a URL Shortener Like Bitly & TinyURL: Base62 Hashing & Read-Heavy Caching

The Classic Read-Heavy System Design Problem

A URL shortener (https://rudresh.in/r/zn9ed7) looks simple on the surface, yet tests crucial distributed systems fundamentals: read-to-write asymmetry (100:1), collision-free short code generation, HTTP redirect semantics, and cache penetration protection.
URL Shortener System Architecture

URL Shortener System Architecture


1. Requirements & Capacity Estimation

  • Write Volume: 100M new short URLs/day $\approx 1,160\text{ writes/sec}$.
  • Read Volume (100:1): 10B redirects/day $\approx 116,000\text{ reads/sec}$ (Peak: 250K+ QPS).
  • 10-Year Storage: $100\text{M} \times 365 \times 10 = 365\text{ billion records}$. At ~500 bytes/record = 182 TB.
  • Short Code Length: Using Base62 (0-9, a-z, A-Z), how many characters $n$ do we need?
    • $62^6 \approx 56.8\text{ billion}$ (too small for 365B).
    • $62^7 \approx 3.52\text{ trillion}$ URLs (plenty of headroom!).

2. Core API Endpoints & HTTP 301 vs 302

Endpoints

  • POST /api/v1/data/shorten { "longUrl": "https://..." } → returns { "shortUrl": "https://sho.rt/aB3x9Z" }
  • GET /:shortUrl → HTTP Redirect to Location: longUrl

Crucial Trade-Off: 301 vs 302 Redirect

Status CodeBrowser BehaviorServer LoadAnalytics Tracking
301 Moved PermanentlyBrowser caches redirect aggressively; skips your server on repeat clicksLowest server load❌ Poor (clicks bypass your backend)
302 Found (Temporary)Browser hits your redirect endpoint every timeHigher (handled easily via Redis/CDN)Exact real-time click analytics
Choose 301 to minimize origin cost, or 302 when click-tracking and dynamic expiration matter.

3. Generating Short Slugs: Hash + Collision Check vs Base62

Approach A: Hash Function (MD5 / SHA-256 / CRC32) + Truncate

  • Take the first 7 Base62 characters of sha256(longUrl).
  • Drawback: Truncated hashes collide. Every insert requires checking the database (or a Bloom Filter first to skip DB lookups for non-existent hashes) and appending a salt/predefined string on collision.

Approach B: Distributed Unique ID + Base62 Conversion (Recommended)

  1. Generate a unique 64-bit numeric ID via a Snowflake ID generator (or ticket server range).
  2. Convert that integer deterministically into a Base62 string—zero collisions, zero DB lookups before insert!
python
BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" def encode_base62(num: int) -> str: if num == 0: return BASE62[0] chars = [] while num > 0: num, rem = divmod(num, 62) chars.append(BASE62[rem]) return "".join(reversed(chars))

4. High-Speed Read Architecture & Edge Cases

  • Cache-Aside in Redis: Store hot short_slug -> long_url mappings in Redis (< 1ms p99). A 36 GB cluster caches tens of millions of hot links.
  • Prevent Cache Penetration: Malicious scanners requesting random non-existent slugs (/r/zzzzzz) would miss Redis and slam the DB. Place an in-memory Bloom Filter at the gateway—if the filter returns false, immediately reply 404 Not Found.
  • Database Sharding: Partition (id, short_url, long_url, expires_at) across shards by short_url hash, with secondary expiration cleanup workers.

Share this article

Comments