1 million active users
1 million * 5 URLs per month = 5 million URLs per month
5 million * 200 bytes = 1 GB storage per month
Peak QPS = 280
POST api/v1/shorten-url
Body - Long URL
GET api/v1/short-url
id
long URL
short URL
other metadata
so we will have
1) Load balancer
2) Servers, responsible for generating short URL as well as supporting redirect for input short URL
3) Database
1) Client will send request with long URL,
we will check if it already exits in DB,
if yes then we will return short URL,
if no then we will generate short URL and return short URL
2) If user provide short url then we will try to find the corresponding long URL and we will redirect request to the URL,
In terms of response code , we could return 302 or 302,
both has it;s own advantage and disdvantages
Lets deep dive into generation of short URL
We have 2 options
1) as we technically have 62 letters to choose from we can have URL that is let's say 7 letters this will give us 62^7 URLs
we can use hash function and get the first 7 characters and check if it exists in the DB or not, if it is then we append random character to get new string, this type of short URL doesn't increase in time.
the other option is
generate unique number for each URL we can use time based unique number generator and then use base 62 conversation,
e.g 11057 will be converted to 2TX as short URL
How will deletion or updation will work
1) For deletion we will simply remove that URL for the storage
2) For the updation we will need to depete and follow the same procedure of generating new URLs
Mechanism to choose for short URL,
1) Hash based
2) or unique number based,
Hash based url are not predictable vs other one is predictable so based on security needs we can choose the one that fits us
regardng returning 302 vs 301 for redirectm 301 is permenent redirect vs 301 is temporary redirect, choosing 301 will avoid muliple calls but it lacks the capability to get the right tracking
1) If URL is not found we will return 400
2) THe unique number generation could become bottleneack, we are going to use the distributed time based generation.
3) Database could be the vbottleneack, we can use distribute3d key value based storage
1) MOnitoring
2) alerting