The problem is to design a solution for finding the number of users who are watching a particular video eg. on youtube etc.
Here is what i thought about solving it.
If the video is playing than every 5th minute from GMT it will send a play event
I will maintain a table containing (userId, movieId, timeStamp)
So to count how many viewers playing a video i will simply have to count the number of entries in the table with the movieID and timeStamp not later than 5 minutes. Because if a play event is not recieved for 6 minutes than the video must not be playing...
And each time a play event is received the timeStamp will be updated to the current time.
Also i will run a cronJob that will be clearing entries that are not getting updated.
Will maintain a cache containing the count for each movie and will update it at a regular interval from the table.
Each video that is being played will send a https request for the number of current viewers every 6th minute and the number of viewers will be updated.
Please suggest if there is any flaw in this approach. or any better approach ?
Any pros and cons ?