we need to build a system that convert the long url to short url contains alpha numerical character as short as possible.
redirect should be lesser than 100ms to do this i used cdn and caching methods
should be avaialble all the time .
users should visible long url only
scalable both horizontal and vertical
assuming there are 100 millions urls generated per day so every second approx 1160 write so assuming read write ratio is 10:1 then 11600 read per second,
runs for 10 years then 10*100 millions *365 = 365 billions record , each url 100 bytes so 36.5 TB data storgae we required.rs, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
I will create a 2 endpoints first one is used to convert the long url to short url , thirugh post method return type is short url
second is read operations through get method . return longurrl
so when the user start its enter the long url we send to convert this into short url using hash function , hash function may be md5 or sha1 . after the short url created we look in the database if exist or not if exist colllison occures we send this again to hash function for new short url , if not exist we save this to in database . to optimise this we used load balancer in starts that distrubes the long url to differnet server for shortening and add a cache like redis to improve efficeny by directly checking on database first .
for read path when user hit the url , we check in our reddis if this exist , if not look for database .
since read is 10 times more the write we use load balancer and cdn
we dived the databased into more miny database like replicas to avoding load on one database m we can also divide the database on the vbase of country to make for fast read operations
we make read and write database differnet as well
table creation
short url unique index for quick lookup , long url , created at, expire at, url mapping pk ,
for read database i will going to use cassandra or dynamodb that offers great read operations in 0(1) time
for write i will prefere postgresql good for write operations
for cachde i used reddis
table scheme
Table: url_mappings id (BIGINT, PK) short_code (VARCHAR, UNIQUE INDEX) long_url (TEXT, NOT NULL) created_at (TIMESTAMP) expires_at (TIMESTAMP, NULLABLE)
i put a partition key for read operation in cassandra name short code created using base62
So: Writes need guarantees, reads can tolerate a tiny delay for performance.
for hash code generation : i used md5 or sha1
to avoid collisuoon we try 2 3 retry mechanism
can't store more than 37TB
rate limit of 1160 per second
hot spot datavase auto upgrade degrade
cache updation at 5 minutes