A database to store all counts for all videos on the platform.
Functionality to count views of a video on the site.
Ability to detect fraudulent views.
Display the up-to-date counts of views.
The system should be able to distinguish between repeat viewings and unique viewings.
Advanced analytics to track how views change over time.
Scalability - the system should support thousands of videos and millions of daily users
Performance - the system should work with minimum latency (milliseconds)
Security - the system should be protected from hackers that try to create fake viewings
Reliability - the view counts displayed to users should be fresh
100,000 videos
1,000,000 daily viewers
Average viewer watches 5 videos
Peak period: 10%
5,000,000 views/ day
Views per hour
5M/ 24 = 208,333
Views per peak hour
5M/ 10 = 500,000
Database capacity
60 * 60 = 3600
500,000 / 3600 = 139 writes/sec max
Server capacity
Assuming each server can handle 10,000 requests/ hour
500,000 / 10,000 = 50
208,333/ 10,000 = 20
You would need 50 servers/hour during peak time and 20 servers/hour on average during off-peak.
Latency
We would ideally target response times within 500 ms per request for acceptable user experience.
{"userID": "acbde" }videoID": "12345", "totalViews": 15000, "uniqueViews": 12000 }Scenario 1 - Displaying views
Scenario 2 - Incrementing views
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Trade-off: Choosing between SQL (relational) and NoSQL (non-relational) databases.
Decision: Opted for an SQL database due to the requirement for ACID principles, structure, and fast reads.
Trade-off: Choosing between immediately consistent vs eventually consistent updates.
Decision: Opted to use a message queue to alleviate pressure on the database and server. Having data that is slightly inconsistent is acceptable because the information (video views) isn't critical to user experience.
Trade-off: Choosing between vertical and horizontal scaling.
Decision: The huge volume of users is not practical for a single server: it's unlikely a single server could support the traffic. Even if it could, users expect high availability, so we need to make sure there are multiple failover points. We can also scale up/down during high/low periods to reduce server costs.
Cons: