URL shortener
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Design a URL shortener (and Pastebin): the 60-second story

    Design a URL shortener

    You tap sho.rt/3cVrDte and land on a Diwali sale. Simple?

    It looks like a two-column table. The hard parts hide: codes that never collide, 140,000 redirects a second, phishing.

    140k/s

    redirects a second at peak

    30 million new links a day, 3 billion opens. Creates peak at just 1.4k a second, so the read path is the whole game.

    Ten years of links: 110 billion codes. How many characters?

    Six base62 characters give 56.8 billion, too few. Seven give 3.52 trillion, only 3% full after ten years.

    Where do codes come from?

    Bad: Hash the URL

    50% clash odds by 2.2 million codes, under two hours.

    Good: Counter blocks, then shuffle

    Each pod reserves 1,000 IDs at once. A crash leaves gaps, never duplicates.

    Consecutive IDs give consecutive codes. Anyone can count upward.

    A keyed Feistel shuffle maps each ID to an unrelated code. Still one ID, one code; without the key, no pattern.

    A cacheable 301 would cut your traffic. Why refuse it?

    The trick

    Cached 301s hide repeat clicks and can’t be switched off. With a 302 and s-maxage=60, only the CDN caches it, for a minute.

    140k redirects a second. Under 5k reach the database.

    Assume modest hit ratios at each layer: the edge keeps 302s for 60 s, each pod a 30 MB LRU, then Redis. They multiply.

    Pastes: same codes, but 20 GB of text a day

    1. Body in object storage, a 300-byte row in the DB
    2. Immutable, so the edge caches pages until expiry
    3. Raw text on a separate, cookieless domain

    Remember

    Mint codes from reserved blocks, shuffled, stored with a conditional put. Serve 302s from edge, memory and Redis. Paste bodies go to object storage; clicks are counted from logs.