#16 Distributed State/Session
Netflix Screen Limiting
Enforce "at most N screens playing at once" per account, worldwide, without blocking playback when a check fails.
What is being asked
A subscription plan allows a fixed number of simultaneous streams. Design the service that counts active streams per account and refuses a new one beyond the limit.
Requirements
Functional
- Start, continue and stop a stream; refuse the start when the plan's limit is reached.
- A stream that ends without notice (the app is killed) must free its slot soon.
Non-functional
- Millions of concurrent streams.
- The check must be fast and must not be a single point of failure for playback.
- Devices and data centres are spread across the world.
Design
- On play, the session service counts the account's active sessions and compares with the plan's limit. If there is room it records a session with a time to live of about a minute.
- The player sends a heartbeat every 30 seconds, which renews the session's expiry.
- On stop the session is deleted. If the app dies, the session simply expires.
- The count and the insert happen as one atomic operation on the account's key, so two devices starting together cannot both take the last slot.
Key decisions and trade-offs
- Expiry instead of explicit stop. Clients disappear without saying goodbye; a short lease that must be renewed is the reliable signal.
- Heartbeat interval. Shorter frees slots sooner but multiplies load; longer leaves a crashed device holding a slot.
- Fail open. If the session store is unreachable, allow playback. A brief period of over-sharing is cheaper than customers who cannot watch.
- Regions. Keep an account's sessions in one home region to get an exact count, or count per region and tolerate a small overshoot during network partitions.
Follow-up questions
- How would you let the user choose which device to stop?
- How do you prevent a modified client from skipping heartbeats to keep playing?
- What changes if downloads for offline viewing also count?
- How do you move an account's sessions when its home region fails?