Count views for each video accurately, ensuring every legitimate view is counted, even under high traffic.
Display real-time or near-real-time view counts to users and content creators.
Prevent fraudulent or bot views using techniques like watch duration thresholds, rate limiting, IP analysis, CAPTCHA, or device fingerprinting.
Provide content creators with view count trends broken down by time period (daily, weekly, monthly) for analytics and insights.
Non-Functional Requirements:
Scalability
Reliability & Availability
Performance
Security
Fault Tolerance
Consistency
API Design
Assuming each video has a unique ID and a known length in seconds.
videoWatchStarted(videoID, startTimestamp)
videoWatchEnded(videoID, endTimestamp)
videoViewCount(videoID, periodFrom, periodTo): Number
publishNotification(videoID, timestamp)
High-Level Design
The Load Balancer is to ensure Scalability, Reliability & Availability NFRs are guaranteed by distributing load among horizontally scalable application servers.
The Geo DNS is to ensure Fault Tolerance NFR is guaranteed by failing over to a different region in case of failure of one of the available regions.
Application Server
The Authentication Service is to guarantee security to prevent fraudulent or bot views and also to identify the person who watched a specific video for analytics purposes.
View Count Service is the service responsible for counting video views according to specific rules.
Analytics Service is the service that will be used by the client to query analytics data. For example, view count trends for a specific period of time.
Notification Workers is the service responsible for notifying content creators once a video view has been added to the database.
The Redis server is to guarantee consistency NFR across application servers when it comes to counting views.
The Relational Database is to guarantee persistence of the view data. Also it's relational since aggregation queries to count views will be used for analytics.
Message Queue allows the View Count Service to publish video view notifications to a queue/topic and it also allows Notification Workers to get notified once a video view notification is published and consequently notifies content creators either through email or WebSockets.
Detailed Component Design
videoWatchStarted(videoID, startTimestamp)
this function will store the video id and timestamp in a session variable
videoWatchEnded(videoID, endTimestamp)
this function will compare the the length watched by comparing the watched length (endTimestamp - startTimestamp) in seconds to the length of the video.
If the length watched is at least 70% of the video, add a timestamp for this view.
We can also store some extra information like who watched it, where it was watched, and may be collect some information about the browser and operating system used to watch it.
videoViewCount(videoID, periodFrom, periodTo): Number
this function will query the database to obtain the number of view timestamps between periodFrom and periodTo of this videoID and return a number.
publishNotification(videoID, timestamp)
This function will be used by View Count Service to publish a message to the queue/topic once a video view event occurs.