Design a scalable and accurate system to track and update the view count for YouTube videos**, which:
•Handles millions of concurrent views per second
•Prevents fraudulent/inflated views
•Provides real-time or near real-time view count on the frontend
•Efficiently supports analytics and reporting queries
🎯 Functional Requirements:
1.View Tracking
•Log and process every view of a video
•Define what qualifies as a "valid view" (e.g., at least X seconds watched)
2.Near Real-Time View Count Display
•Display view count with low latency (~seconds to minutes delay)
3.De-duplication and Anti-Fraud
•Prevent multiple rapid views from same user/IP/device from inflating the count
4.Analytics & Reporting
•Support queries like: “Top 10 videos today”, “Views by region/device”, etc.
5.Failure Resilience
•No view should be lost even if some components fail
⚙️ Non-Functional Requirements:
•High throughput (handle 100M+ views/hour)
•Scalability (global user base)
•Consistency (eventual consistency is acceptable for view counts)
•Low latency for both writes (view logs) and reads (display count)
🧱 Core Components:
ComponentRole
Frontend
Sends video view events (after user watches for X seconds)
View Collector API
Receives view events, pushes to a stream
Streaming System
Ingests and buffers view logs (e.g., Kafka, Pub/Sub)
View Processor
De-duplicates and filters valid views
Real-Time Counter
Fast-access counter store (e.g., Redis, Memcached)
Batch Processor
Periodic aggregation for permanent storage
Data Store
Stores long-term, accurate, historical view counts
Dashboard/API
Exposes current view count to frontend users
🔁 View Flow Example:
1.User watches video v123
2.After 30s, frontend sends a POST to /log_view
3.View Collector forwards this to Kafka
4.View Processor consumes from Kafka, filters duplicates and invalid views
5.Valid views are:
•Pushed to Redis counter for near real-time display
•Sent to Data Lake / DB for analytics
6.Frontend queries view count via API backed by Redis (real-time) or DB (accurate but delayed)
🧮 Anti-Fraud/De-duplication Strategies:
•Maintain sliding window of viewer's IP/UserID + VideoID (in Redis)
•Apply rules:
•View from same user/IP within X minutes → discard
•Bot detection heuristics (user agents, click patterns, CAPTCHA)
🗃️ Data Model Example (for batch DB):
1json
2CopyEdit
3{
4 "video_id": "v123",
5 "total_views": 1054321,
6 "unique_views": 945213,
7 "region_views": {
8 "US": 400123,
9 "IN": 350000
10 },
11 "timestamp": "2025-07-23T12:00:00Z"
12}
🧰 Suggested Tech Stack:
LayerTools
Ingest
Kafka, Google Pub/Sub
Real-time Store
Redis, Aerospike
Batch Analytics
Spark, Flink, BigQuery, Redshift
Permanent Store
PostgreSQL, Cassandra, S3
API Layer
Node.js / Golang
Dashboard
React / Angular
📊 Optimization Considerations:
•Sharding view counters by video_id % N to reduce contention
•Compress view logs before long-term storage
•Pre-aggregate counts per minute/hour for analytics
•Use Redis HyperLogLog or Bloom Filters for rough unique count