System requirements
Functional:
- Given a long url, output a shortened url
- shortened url only have 8 bytes for length
- click on shortened url able to redirect to long url
Non-Functional:
- availability
- low latency, within 1second
Capacity estimation
- 200K employees, 10 requests per day, 23 RPS, peak 100RPS
- A simple server can easily handle 10K RPS, CPU wont be an issue
- long url 1K bytes, short url 8 bytes, daily 2M * 1.1K = 2.2G, month 70G, year - 840G ~ 1T
API design
- Get v1/shortenedUrl/longurl=?
- 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
- generate shortened url user -> loadbalancer -> urlshortner -> DB
- resolve the shortened url, user, give out a shortened url -> resolver -> DB, then direct it to long url
Request flows
- generate shortened url user -> loadbalancer -> urlshortner -> DB
- 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
- url shortner can use other ways, like hash code of the long url, which is longer than 8 bytes
- each url shortner should sync on time
Failure scenarios/bottlenecks
- database is down.
- for url generated a year ago, need to go through DB and fetch the original url which can be slow
Future improvements
- improve the resolver time