📝 Problem Description
Design a system to enforce concurrent stream limits for Netflix-like services. Basic plan allows 1 stream, Standard 2, Premium 4. Handle real-time enforcement, device management, and edge cases like network issues.
👤 Use Cases
1.
User wants to starts streaming so that stream allowed if under limit
2.
User wants to exceeds stream limit so that new stream blocked, shown active devices
3.
User wants to stops watching so that slot freed for new stream
4.
User wants to kicks device remotely so that selected device stopped
✅ Functional Requirements
- •Track active streams per account
- •Enforce concurrent stream limits by plan
- •Allow user to see and kick active devices
- •Handle stream start/stop/heartbeat
- •Grace period for network issues
⚡ Non-Functional Requirements
- •Stream start decision < 100ms
- •Support 100M concurrent streams
- •No false blocks (user within limit)
- •99.99% availability
⚠️ Constraints & Assumptions
- •Must handle network failures gracefully
- •Cannot rely on stream end signal (user may close browser)
- •Global distributed users
📊 Capacity Estimation
👥 Users
200M accounts, 100M concurrent streams
💾 Storage
10GB (active sessions)
⚡ QPS
Heartbeats: 10M/sec, Stream starts: 100K/sec
📐 Assumptions
- • 200M accounts
- • 50M concurrent active accounts
- • Average 2 concurrent streams per active account
- • Heartbeat every 30 seconds
- • Average stream duration: 1 hour
- • Grace period: 90 seconds (3 missed heartbeats)
💡 Key Concepts
CRITICAL
Heartbeat-based Presence
Devices send heartbeat every 30s. Missing heartbeats = session expired.
CRITICAL
Atomic Limit Check
Check-and-increment must be atomic to prevent race conditions.
HIGH
Grace Period
Allow brief network issues (2 missed heartbeats) before killing stream.
HIGH
Session Tokens
Each stream gets unique token; required for heartbeats and license refresh.
💡 Interview Tips
- 💡Start with the atomic check-and-increment pattern
- 💡Discuss the Redis Lua script approach
- 💡Emphasize the heartbeat mechanism
- 💡Be prepared to discuss edge cases
- 💡Know the tradeoffs between strict and lenient
- 💡Understand the session cleanup strategy