Skip to content
SD-26 · Back to the write-up

Design a Concurrent Stream Limits

Every playing device holds a 3-minute lease, renewed every minute; one atomic script per account decides if a new screen fits.

Follow a request

Devices
Edge
Services
Data
PlayerTV, phone, browser
Device pushStop now
Auth servicePlan in the token
Load balancerSpreads requests
License server5-minute keys
Stream-limit serviceStateless
Lease store32 shards by account
Accounts DBPlans, devices
Session eventsAnalytics
1player → lb · POST /streamsA third TV presses play at the same moment as another.1 / 6

The numbers, at a glance

For every start……there are about 17 heartbeats
1 start ≈ 40,000/s17 heartbeats ≈ 667,000/s
The heartbeat interval sets the loadHeartbeats per second at 40M streams
Heartbeats are the loadShare of 707,000 requests a second
So the heartbeat interval, not the start path, decides the size of the fleet.

A start, in order

The whole check is one script on one shard, so two starts on one account never interleave.

Player
Load balancer
Stream-limit service
Lease store
License server
1 POST /streams + token
2 route
3 prune, count, add
4 added, 2 of 2
5 201 session
6 201 session
7 license request
8 lease live?
9 yes
10 5-minute key
RequestResponseAsync, does not block

How the count is kept

The decision this question is really about.

Rows in SQL
Too heavy
COUNT(*)
INSERT row
667K writes/s
+ Familiar.− A write per heartbeat; races.
Counter
Drifts
INCR on start
TV unplugged
stuck at 2 of 2
+ Tiny and fast.− Crashes never decrement.
Leases with TTL
Our pick
prune expired
countadd if room
one shard, one step
+ Heals itself; atomic.− Crash frees in 3 minutes.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 50,000 streamsMake it work
Player
App server
SQL database
One server, a sessions table, heartbeats every 10 s.
Stage 2 · ≈ 5M streamsLeases in memory
Player
Load balancer
Service × N
Lease storeAccounts DB
Sorted set per account, TTL, atomic script, heartbeats every 60 s.
Stage 3 · ≈ 40M streamsShard it
PlayerAuth service
Load balancer
Service × N
Lease store × 32Accounts DB
32 shards by account; the plan travels in the token.
Stage 4 · 40M streams, enforcedEnforce it
PlayerAuth serviceDevice push
Load balancerLicense server
Service × N
Lease store × 32Accounts DBSession events
Video keys need a live lease; revokes push a stop; events go to a log.
Was this walkthrough useful?Your vote tells us what to improve next.