The application takes input an url and shortens it. The shortened url points to riginal url and user can use this shortened url to access originl url.
The url shortener should haave a minimum length of url such that it generates a unique url for long and there is no collision.
There can be n^26 combination for any number n. So we need to caculate n based on our traffic to make sufficient unique url.
We will have multiple servers behind a load balancer to manage traffic.
Now to avoi collision we will assign a range in whcih each server can make shortened url.
The shortened url will be genrated based on algorithm. There will be a tracker, which will be on incremental basis, to be used by our url generator to make the tring based on the tracker to avid collision.
The url should handle generation of unique url even on simultanious request.
The application should cache the shortened url so it do not regenerates url and there is less read and writes on db.
The application should scale horizontally to handle large number of requests.
Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
POST API
The user sends the originaal url in post request body and gets shortened url in return.
post request firt checks cache, it cache is missed, t checks in db aand if the url is not in db too, it genertes a new shortened url and returns it.
design
post: /shorten-url
body:{ long_url}
GET API
User tries to access the datbase via get api. The body contains shortened url and when made request, user is redirected to original url.
it is done by reading ache, if it missed then by reading database.
user requests the shortened url
the post request is made to our server with long url in its body
first shortened url is checked in cache , if found returns shortened url
if cache is missed, it reads database and if shorned url is found there, it returns the shortened url. Cache is updated accordingly.
if it is also not present in database, it goes to least bust server via load balancer.
Each server is assigned a range in which they can generate url string, so they are independent of each other while generating shortened url.
The server generates the shortened url.
It is wrote down in database and cache is updated.
finally the url is returned to user.
when user clicks on the shortened uurl, cache is checked if it is present there user is redirected to original url. else original url is fetched from db and user is redirected there.
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
Key component:
URL shornner service: Shortens the url and writes in database
URL fetching service: Redirects user to original URL
database: Stores the mapping of shortened url to original url
Cache: Reduces read and writes on database.