Estimating that the system would be a lot more of read than write. We would only write once to create the URL but mostly read a lot more. so it would be read heavy.
If there are 10 million of shortened URLs created in a month then its about, 10 urls created per second.
Assuming 200:1 read write, that would mean its about 2000 redirections per second.
If we want to store it for a given time (let's say 50 years) then we would need a DB to be long lasting. lets say a URL is about 500bytes, and we are creating 10 million URLs a month, that;s about 120 million URLs a year, and we want it to last for 50 years so that;s about 120*50= 600 million urls
600 million * 500 byte, is somewhere around 30 TB.
We also need to consider caching memory. if we use 80:20 rule for caching, then for 2000 requests/redirects per second, we have that for a day is about 180 million requests a day and if we cache 20% of these then we would need memory of about 0.2*180*500 or so GB of memory for caching.
DB1:
DB2:
on a high level we will have client and database.
the client has two functions:
In order to avoid latency if multiple requests are coming, then we need to add load balancers in front of the webserver and perhaps increase the webservers and also partition the DBs so that balanced requests are managed accordingly. (for example if they are coming from different locations then they go to the server closest to their region and hits the right DB partition).
We could also allow caching so that some of the long URLs can be cached and dont have to hit the DB often to reduce latency.
The main crux would be the shortening algorithm. We would have to use URL encoding to shorten the URL and have some kind of key that would map to the longer URL. Also we need to decide how many characters we want in our short URL. If we have more characters in our shortURL then we can have more variations of different URLs that we can generate for different long URLs (if we use base 62 URLs and want 7 characters then that's 62^7).
For simplicity sake we can have our algorithm just choose 7 characters at random. First we check if the long URL exists in the DB or not. if it does then return the short one. If not then we will generate a short one and see if that exists in the DB or not. but this can take a while if we want to keep retrying. if not then we would need some kind of basic conversion rules such that if the long url is of certain length and has certain characters then the corresponding short URL will be encoded in a certain way. May be some kind of a counter?
Explain any trade offs you have made and why you made certain tech choices...
A bottleneck could be when using RDBMs for bigger number of URLs. it may not be very scalable and we would need a NOSQL type solution for it.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?