System requirements


Functional:

As a user I would like to provide the original url and get a shortened url

given a url ,I want it to redirect to the website I want

the short url will expire after a certain number of time, but before it expire, it will always be that same link




Non-Functional:

The latency to get the shortened url should me as minimal as possible like 10ms

The system should be highly available. the redirect path should be availble at all times

The system should be highly scalable. The system should be scalible to host millions of urls

The system should be durable with shorten url and it's link stored for a certain number of time



API design

HTTP

  1. POST request, generate shorten url

request body:

long url

response:

short url, createdAt, expiresAt

Authentication: API key in the header, every write request comes with registered account

  1. GET request, get underlying link, and redirect

request body: shortUrl

response: error code, long url. Return a 302 response with the original url in the location.





High-level design

There are 2 path coming for the design: one for read path and one for write path.

read path: client use short url to redirect to the actual url.

write path: client call api and write short url to it


read: client API --> CDN --> API gateway --> mapping service--> redis cache--> database. multiple cache layers. CDN can handle the hottest url request


write: client API --> API gateway--> shorten service --> message queue--> queue worker --> database

shorten service generate the unique code and message queue will decouple the database from service call







Detailed component design

The server can have multiple instances to handle the reuqests coming from the client. And the server will use some algorithm to genereate the short url. The database will consist of 1 table and have reader and writer server.



Database design

Design a key- value store with the short to long url mapping. We can use dynamoDB to do it.

schema:

short url,

long url,

userId

createsAt

expiresAt

We can also have secondary index on long url, when a user submit the same url again, check db and find it