:fist: A full-stack web application, developed by MEAN Stack, that generates short URL and shows the statistics data of the usage of the short URL; Enhanced independence by deploying Docker and improved QPS by using Redis as cache and Nginx as reverse proxy.
We assume daily active user: 1,000,000
(1) average estimate
for Insert:
for Lookup:
(2) peek estimate:
Therefore, we can deal with it by a single machine
map<longURL, shortURL> LongToShortMap;
map<shortURL, longURL> ShortToLongMap;
shortURL insert(longURL){
if LongToShortMap not containsKey longURL:
generate shortURL;
put<longURL, shortURL> into LongToShortMap;
put<shortURL, longURL> into ShortToLongMap;
return LongToShortMap(longURL);
}
longURL lookup(shortURL){
return ShortToLongMap.get(shortURL);
}
The simplest way: return map.size(),but the size will increase too big, so we need add letters.
Therefore, we need to convert decimal into 62 hex. Here is the code:
shortURL generateShortURL(){
return convertTo62(ShortToLong.size());
}
shortURL convertTo62(mapSize){
char encode[62] = {'a',...'z','A',...'Z','0',...'9'};
shortURL = "";
while(mapSize != 0){
shortURL = encode[mapSize % 62] + shortURL;
mapSize /= 62;
}
return shortURL;
}
According to experience, we can assume:
Average size of long url = 100 byte
Average size of short url = 4 bytes(int)
Daily new URL = 104 * 100,000 = 104,000,000 byte = 104mb
Year new URL = 104mb * 365 = 37960mb < 40GB
Therefore, singe Machine can handle!