Design a system where Influencers upload videos and send Welcome note to top K Users
We need to provide the influencers(publishers) to track and view top K viewers by watch time of each video. Also the publishers should be able to send "Thank you Note" to these K viewers.
FR
1. Influencer should be able to upload videos
2.Should be able to track top k viewers by watch time of each video
3. Should be able to say Thank You message to these k viewers
4.Videos will be published to youtube/fb/insta
5.Viewer will be notified when influencer uploads video
6.At what frequency do we need to send Thank You Note??
NFR
1.User should be able to upload videos in almost near real time,enforcing low latency
2.System should be highly available
3.Eventual consistent
4.The top K viewer list should be updated and available asap after viewer interactions.
Back of Envelope Calculation
Number of Influencers (Publishers): 100 M
Average Videos per Influencer: 50
Average Views per Video: 10,000 views per year
total videos posted by influencer=100M*50
total views = 100M*50*10k views/video=50 B views per video per year
QPS=50B/10^5*(10^2)=50*10^2=5k qps
Storage
Top K Viewers to Track: 100 per video
Average Watch Time Data per View: 200 bytes (including user ID, timestamp, watch duration)
Storage for Thank You Notes: 200 bytes per note
Retention Period for View Data: 1 year
Data Transfer Overhead for Real-Time Processing: 1.2x
Storage
Each view generates about 200 bytes of watch time data
Total views storage = 50B*200bytes=10TB
total Thank You Note=50B*100(top 100)*200bytes(for thank you) ??
API’s
1. Post api/v1/videos/upload
user id will pass via jwt token
request
{
"title": "string",
"description": "string",
"file": "binary",
"tags": ["string"],
"category": "string"
}
response
201 Created if successful, with video ID and metadata.
2. GET /api/videos/{videoId}
Retrieves details of a specific video by video ID.
{
"videoId": "string",
"title": "string",
"description": "string",
"views": "number",
"topViewers": [
{
"viewerId": "string",
"watchTime": "number"
}
]
}
3. POST /api/videos/{videoId}/view
Records a user's view on a video, including the watch time.
request
{
"viewerId": "string",
"watchTime": "number" // watch time in seconds
}
response
200 OK on successful recording.
GET /api/videos/{videoId}/top-viewers?count={K}
4. GET /api/videos/{videoId}/top-viewers?count={K} reponse { "videoId": "string", "topViewers": [ { "viewerId": "string", "watchTime": "number" } ] }
5.POST /api/videos/{videoId}/thank-you-note
Allows an influencer to send a "Thank You Note" to the top 𝐾 viewers
request { "viewerIds": ["string"], "message": "string" // customizable message }
6. GET /api/influencers/{influencerId}/dashboard
Retrieves analytics data for an influencer's videos, such as total views,
average watch time, and engagement.
response
{
"influencerId": "string",
"videos": [
{
"videoId": "string",
"title": "string",
"views": "number",
"averageWatchTime": "number",
"topViewers": [
{
"viewerId": "string",
"watchTime": "number"
}
]
}
]
}
Data Models
User Table
{
user_id,
user_name,
user_type(wether he is influencer or viewer)
email,
hashed_pass,
created_at,
user_location,
user_rating,
user_engagement
}
Video table
{
video_id,(PK)
title,
influencer_id(FK),
description,
category,
tags,
upload_date,
view_count,
avg_watch_count
video_url
}
Views Table
{
view_id,(PK)
video_id(FK)
viewer_id(FK),
watch_time,
view_date
}
Top Viewers Table
{
id,
video_id,
viewer_id,
watch_time
}
Thank You Table
{
note_id,
viewer_id,(FK)
influencer_id,(FK)
message_content,
sent_at
}
Notifications Table
{
notification_id,
viewer_id,
message,
video_id,
created_at
}
Analytics table
{
video_id,
total_views,
avg_watch_time,
top_viewers_json
}
Relationships between Tables
1. User to Video: 1:N (an influencer can upload multiple videos).
2. Video to View: 1:N (each video can have multiple views).
3. User to View: 1:N (a viewer can watch multiple videos).
4. Video to Top_Viewers: 1:N (each video has multiple top viewers).
5. Video to Thank_You_Note: 1:N (an influencer can send thank-you notes for a video to multiple viewers).
6. User to Notification: 1:N (a viewer can receive multiple notifications)
How would we calculate watch time for each video or number of views for given video if number of users are huge??
HLD