Assume we have 1M monthly active users.
DAU = 1M / 30 = 33k users
Assume that each user shorten 8 links a day (5 links are unique), and for each links, there are 3 clicks
writes per day = 33k * 5 = 165k
Let's say long URL average length is 150 characters, so that's 150 bytes. For shortened URL, let's say there are 8 characters. Each write will take 158 bytes
Bytes per day = 165k * 158 = 26MB / day
storage for 5 years = 26MB * 365 * 5 = 47.45TB
reads per day = 165k * 3 = 495k
read per second = 495k / 24 / 60 / 60 = 5.7 read / s
To account for spike in traffic, we should support 50 read / s.
Shorten URL
Edit URL
Access URL
Delete URL
For delete, we have to ensure that the entry is not owned by other users. Otherwise, we should still keep it.
User
Subscription
UrlMap
UserUrl (composite keys for both IDs)
For analytics
ClickUrl
We want to retain data for only 30 days. Or up to 90 days for higher tier subscription users.
Given the relationships of users and URLs and clicks can be complex, we will use relational database (e.g. PostgreSQL) to support complex queries and joins.
We want to have API routers that route to either the read or write server depending on the request.
Before reaching the write or read server, we also want to have a load balancer, so that we can scale the system to more users (by scaling it horizontally).
We also have a write and read database, where periodically, write DB will update the read DB. Because of this, the data may not be consistent (users may not immediately be able to redirect). So on the webpage, we should warn users that it may take 5 seconds for URL shortening to take into effect.
User shorten a new URL
User goes to a shortened URL
Sharding
Rate limiting
Message queues
Replication
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?