System requirements


Functional:

  1. Given a long url, output a shortened url
  2. shortened url only have 8 bytes for length
  3. click on shortened url able to redirect to long url



Non-Functional:

  1. availability
  2. low latency, within 1second



Capacity estimation

  1. 200K employees, 10 requests per day, 23 RPS, peak 100RPS
  2. A simple server can easily handle 10K RPS, CPU wont be an issue
  3. long url 1K bytes, short url 8 bytes, daily 2M * 1.1K = 2.2G, month 70G, year - 840G ~ 1T



API design

  1. Get v1/shortenedUrl/longurl=?
  2. Get v1/shortenedUrl return the webpage points to url



Database design

MySQL

save it within a table

add cache when read from database


High-level design

  1. generate shortened url user -> loadbalancer -> urlshortner -> DB


  1. resolve the shortened url, user, give out a shortened url -> resolver -> DB, then direct it to long url






Request flows

  1. generate shortened url user -> loadbalancer -> urlshortner -> DB


  1. resolve the shortened url, user, give out a shortened url -> resolver -> DB, then direct it to long url





Detailed component design

  • url shortener, 8bytes has 64 bits. Url can be generated based on timestamp 42 bits, region id 5 bit, machine id 5 bits, sequence number 12bits
  • shortened url resolver. Cache over the DB saves over the past 7 days generated urls. Key-value pair, given the shortened url, it maps it to the original url




Trade offs/Tech choices

  1. url shortner can use other ways, like hash code of the long url, which is longer than 8 bytes
  2. each url shortner should sync on time


Failure scenarios/bottlenecks

  1. database is down.
  2. for url generated a year ago, need to go through DB and fetch the original url which can be slow




Future improvements

  1. improve the resolver time