Users can create short urls of given long urls
Users can redirect to long urls from given short urls
System should be highly available, low latency.
There will be 2 urls:
1) /create POST API, it takes in the original long url and returns a short url
2) /redirect/url_key GET API, it redirects to the original url
We have the client, then comes GeoDNS which will route the request to the appropriate cluster. Then we got load balancers which route the request to appropriate app instance.
Then sits our cache, which caches 1000 urls and stores according to LRU policy. We then have a separate Key Generation Service (KGS) which is responsible for asynchronously creating 6 character strings called keys. We have a coordinator service which assigns a range of keys to each instance, that way we prevent race condition.
We then have a cleanup service that is responsible of cleaning the expired short urls so they can be reassigned.
KGS has sharded DBs to store keys. The DBs have table short_urls having 3 columns id, url_key(string), active(boolean).
Our app server has DB with tables users and user_urls. user_urls has columns id, user_id, long_url, url_key, created_at, expiration_date. We have multiple replicas of this DB to support high read frequencies.
The final piece is our cleanup service. Cleanup service is responsible for recycling the urls and it works with the main app's DBs and the DBs of KGS.