Design a URL shortener

Lesson 14: Design a URL shortener 1. How would you start designing a URL shortener? First, I'd clarify the requirements before drawing anything. The main features are creating a short link and redirecting users to the original URL. I'd also ask about scale, for example how many new links we expect per day. 2. How would you generate the short codes? I'd give each new URL a unique ID from a counter and encode it in base sixty-two. Seven characters in base sixty-two give us more than three trillion unique codes. Random codes also work, but then we have to check for collisions. 3. How would you store the data? The data model is simple: a short code, the original URL, and a created date. Since we mostly look up by key, a key-value store or a NoSQL database fits well. Reads are much more frequent than writes, so I'd design for fast lookups. 4. How would you handle a lot of traffic? I'd put a cache like Redis in front of the database for popular links. Most traffic goes to a small number of links, so the cache hit rate should be high. Then I'd add more application servers behind a load balancer as traffic grows. 5. What trade-offs would you mention? A 301 redirect is faster because browsers cache it, but we lose click analytics. A 302 redirect costs a bit more, but every click comes back to our servers. I'd ask the interviewer which matters more: speed or tracking.
Giọng đọc: Aria · Guy — đổi

Câu hỏi trong bài này

Design a URL shortener — Tempo