skip to content
jaeaeichsysdes

Design a URL Shortener

End-to-end architecture for a 1B DAU URL shortener: Snowflake + Base62 codes, DynamoDB conditional writes, CDC-backed Bloom filters, and sub-10ms multi-tier caching.

The whiteboard

Trace the write path, read path, and CDC Bloom sync below — combining Distributed Unique IDs, Bloom Filters, and Caching & Failure Modes.

whiteboard · press 1–4 to trace flows

WRITE PATH (POST /url)READ PATH (GET /:code · <10ms)STORAGE & CDC GUARDPOST /urlGET /:codemint IDconditional putwrite-through1. cache2. bloom guard (on miss)3. missstreamset bitsClientbrowser / app1B DAUCreate ServicePOST /url1.2K–10K RPSSnowflake ID64-bit → Base62stateless genRedirect SvcL1 + singleflight301 / 302Redis Cache1d TTL + jitterwrite-throughDynamoDBPK: codeattr_not_existsCDC StreamDynamoDB Streamsasync fan-outBloom Filterexists(code)?blocks miss storms

Full URL shortener: writes go through Snowflake + conditional DynamoDB put; reads stay sub-10ms via L1, Redis, and a CDC-fed Bloom filter.Availability over strict consistency — pure key-value access lets DynamoDB scale horizontally without manual sharding.

Scale & APIs

1.2K / 10K

avg / peak write RPS (1B DAU)

< 10ms

redirect p99 on cache hits

~100 GB/day

avg storage (~1 KB/row)

Entity: URL { code (PK), longUrl, userId, createdAt }
POST /url
body: { "url": "https://...", "customCode"?: "launch-2026" }
-> 201 Created { "code": "a8F9k2" } | 409 Conflict (alias taken)
GET /url/:code
-> 301 Moved Permanently (browser/CDN caches; lowest load, no click counts)
-> 302 Found (hits origin every time; enables click analytics)
Location: https://...

How it works

1. Code generation (Snowflake → Base62)

  • Stateless ID minting: Create Service encodes a 64-bit Snowflake ID in Base62 ([0-9a-zA-Z]) with no central counter lock.
  • Exact 7-char option: Full 64-bit Snowflake yields up to 11 Base62 chars; if strictly 7 chars (62^7 ≈ 3.5T) are required, use lease-based 43-bit counter blocks or salted hashing with retry.

2. Storage & atomic uniqueness (DynamoDB)

  • Zero-ops horizontal scale: Pure key-value lookups (code -> longUrl) and availability over strict consistency make DynamoDB partitioned on code ideal.
  • Conditional write guard: Every write runs attribute_not_exists(code) so concurrent custom-alias requests serialize atomically in storage instead of overwriting each other.

3. CDC-backed Bloom filter

  • Cache-penetration shield: Redirect Svc checks the Bloom filter on Redis misses to reject random/non-existent code scans before they reach DynamoDB (small false positive rate is harmless; false negatives never happen).
  • Async propagation: DynamoDB Streams updates the Bloom filter asynchronously so writes stay fast, which is why the DB conditional write remains the real uniqueness gate.

4. Sub-10ms reads without stampedes

  • L1 + singleflight: In-process L1 caches viral links locally, and singleflight collapses thousands of concurrent misses per instance into 1 Redis/DB lookup.
  • Write-through + TTL jitter: Create Service populates Redis on creation so fresh links are warm immediately, while randomized 1-day TTL jitter prevents synchronized thundering herd expiries.

lesson

Interview differentiator: Explicitly call out the 301 vs 302 trade-off (CDN offload vs click analytics) and explain why an async CDC Bloom filter stops read-path cache penetration but can never replace a conditional DB put on writes.

Check yourself

interactive · stress-test this architecture against common interview probes

01 · Race condition on custom alias

async CDC lag · custom alias · uniqueness

Two users submit POST /url with customCode='promo' within 2ms of each other. Both pass the CDC-backed Bloom filter because the first write hasn't propagated yet. What prevents overwriting?

02 · Viral link cache expiry

50k read RPS · single hot key · cache miss

A celebrity tweets a short link getting 50,000 clicks/sec. Its 24-hour Redis TTL expires. Which combination keeps DynamoDB from melting?

03 · 301 vs 302 for click analytics

browser caching · click analytics · redirect codes

Product asks for an accurate click-counter dashboard on every shortened link. Your Redirect Service currently returns 301 Moved Permanently. What breaks?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme