設計 URL 短網址 (TinyURL)

Easy★★★★★Studied

FromSystem Design Interview

Concepts practiced📐 回中信封估算 (Back-of-the-Envelope)🧩 一致性雜湊 (Consistent Hashing)⚡ 快取策略 (Caching Strategies)

Convention: read the requirements & constraints first, sketch your own design, then expand the reference to compare.

📚 From the Books

設計 URL 短網址 (TinyURL)ProblemEasySearch & Retrieval

把長網址轉成短碼,存取時導回原網址。看似簡單,卻能考到估算、編碼、快取、讀寫分離。

📋 Requirements

🧠 Core Ideas

TIP

核心是「產生唯一短碼 + 建立 短碼↔長網址 的映射 + 讀多寫少所以重快取」。

先估算(見回中信封估算): 寫 100M/日 ≈ 1.2K QPS,讀 ≈ 120K QPS → 讀路徑必須打 cache。

⚖️ Reference Design

短碼產生 — 三種方案
  • 雜湊 + 取前 7 碼:對 longUrl 做 hash,base62 取 7 碼;碰撞要重試。
  • 發號器 + base62:全域遞增 ID 轉 base62 → 短、無碰撞,但可被列舉(可加擾動)。
  • 預生成池:離線產一批短碼放 KV,寫入時取一個 → 寫路徑最快。
儲存與快取
  • 映射存 KV store;分片用一致性雜湊
  • 讀路徑:cache-aside,熱門短碼幾乎全在 cache。
  • 7 碼 base62 = 62⁷ ≈ 3.5 兆組,10 年內綽綽有餘。

🔑 Takeaways

✍️ My Notes

No notes yet — jot your takeaways or Q&A here.

📖 Further Reading