Assume 1M DAU, each user creates 1 short url on average per day. Assume read write ratio is 100:1.
TPS = 10M/24/60/60 = 30
QPS = 3000
Assume we store the data for 5 years by default.
5 years * 1M * 30 days * 12 month = 1.8B records
Assume each record is 2KB, the total size is around 3600GB.
We can use NoSQL since we only need to store a key value with some metadata. This also scales better given the high TPS and memory usage.
Write - The client fires a createURL request to the server, the server first checks if a short URL exists in redis, if so, returns it, otherwise creates one and inserts to the database. If a customized short url is provided, the server uses that if no duplicate exists, otherwise returns errors.
Read - The client fires a getURL requests to the server, the server first checks if a short URL exists in redis, if so, returns it, otherwise reads from the database and updates redis.
The server uses MD5 to hash the long URL, if the result already exists, append a 5 digit random string and retry.
Clean up service scan through the database and redis daily to remove records that expire.
As in the capacity calculation section, the total memory we need for 5 years is 3600GB, should fit into one single NoSQL database, thus we can replicate the entire database to scale up.