Video Streaming: Scope
Treat this as one more read-heavy web service and the first ten minutes go to the wrong numbers: uploads a day, then storage. What runs this system is the bytes leaving it. By minute ten you want that figure, the decision it forces, and the upload sums too, mostly so that you can say how little they decide. Five questions get you there.
Five Questions, Viewers First
Each answer moves a box or a number, and the one given here is what the rest of this problem uses.
- How many people watch at once at the peak, and at what quality? The answer sets the bytes going out, the number everything else is measured against. Agreed: a peak of a million viewers at once, on HD, twice the average.
- How much video arrives a day, and in what form? The answer sets the storage and the transcoding fleet, transcoding being the work of turning an uploaded file into other encoded versions. Agreed: 100,000 hours a day, mostly 1080p sources.
- Which devices and which networks? A phone on a poor network and a television on fibre cannot share one file, so the answer sets how many versions a video needs. Agreed: phones to televisions, down to about 1 megabit a second.
- How soon after an upload must a video play? The answer sets how much transcoding runs in parallel. Agreed: an hour of video plays about ten minutes after its upload ends.
- On demand only, or live as well? Live takes away the time the pipeline has. Agreed: on demand. Live is a curveball, and so is any check before publishing.
The Agreed Terms
Three things are functional. A creator uploads a video. A viewer watches it on any device. A creator sees whether an upload is ready.
Three are non-functional. Playback starts quickly and does not stall on the viewer's own network. An upload the service has accepted is never lost. A viewer anywhere is served from somewhere nearby.
Then say what stays off the board: recommendations and search, comments and accounts, ads and payments, and DRM, the copy protection on a stream. None of them changes where the bytes go, and that is the reason you give.
The Sum That Places the Edge
Open where Chapter 2's The Edge: CDNs as a Black Box stopped. Assume a million viewers at the peak, at about 7 megabits a second each. That is 7 terabits a second. A 100-gigabit link carries a seventieth of it, so serving it from the origin would take seventy of them. Even a large cloud instance, at 400 gigabits, carries under a seventeenth. The edge already has that capacity, near the viewers, so it goes in the first design and is not a scaling step.
Three short sentences carry all of that, and they are worth having ready: "A million viewers at 7 megabits is 7 terabits a second. That's seventy 100-gigabit links, so the edge goes in the first design, and everything behind it only ever sees the misses."
Now a whole day of it, for the bill. Half a million viewers on average, around the clock, is 12 million viewer-hours a day. At about 3 GB an hour, that is 36 PB a day, about an exabyte in a 30-day month. Nothing gets drawn because of this number, and the cost curve comes back to it in the last lesson.
The Sums on the Way In
Bytes in come next, and they are quick. A 1080p source is about 8 megabits a second, about 3.6 GB an hour. A hundred thousand hours is 360 TB a day. That is about 3.6 GB a second, and about 7 at the peak. Seven gigabytes a second is under one 100-gigabit link, and over a hundred times smaller than the bytes out. It decides nothing, so you say so and drop it.
Storage needs one more assumption. Assume renditions, the encoded versions of a video, at 7, 3 and 1 megabits a second. Together they come to about 5 GB per hour of video. With the source, that is 8.6 GB. At 100,000 hours a day, that makes 860 TB a day and about 310 PB a year. It goes in an object store, so this number sets a bill, not a node count.
Transcoding is the upload sum that does decide something. Assume one machine encodes one rendition in real time, so an hour of video takes three machine-hours. A hundred thousand hours is 300,000 machine-hours a day. Over 24 hours, that is 12,500 machines running all the time, and as an average that is a floor. So transcoding is a fleet, never a step on the upload server.
API and Entities
Two calls carry this for now. A POST on videos takes a title and a size, and returns a video ID and where to send the bytes. A GET on one video returns the title, the status and a playback URL. Scaling It puts that size to work.
POST /videos {"title": "Harbor at dawn", "size": 4000000000}
201 {"id": "v-81c2", "upload_to": "/videos/v-81c2/file"}
GET /videos/{id} 200 {"title": "Harbor at dawn", "status": "ready",
"playback_url": "https://cdn.example.com/v-81c2"}
One entity is enough, a video: its ID, owner, title and status, the key of its file in the object store, and its duration.
- Opening on uploads a day and storage, as for a read-heavy web service. The interviewer has to steer you to the bytes out, and notices that they had to.
- Drawing a CDN with no sum in front of it. It is a slogan until it is seventy links.
- Using the upload bitrate for delivery. At 4K that is about 40 megabits against 16, and the egress line moves by more than double.
- How many watch at once, asked before how much is uploaded, which shows you know the heavy side of this system.
- 7 terabits reached in three short sentences and turned into seventy links, so the number arrives with its decision.
- The upload bandwidth dropped on purpose, with the reason said out loud.
Knowledge Check
The interviewer allows one question before you start drawing. Which answer reshapes this design the most?
- How many hours of video arrive a day, and how long a typical one runs
- How many viewers watch at once at the peak, and at what picture quality
- Which file formats creators may upload, and how large one upload can be
- Whether comments and accounts are needed on the first day of launch
A million viewers at about 7 megabits a second is 7 terabits a second. What does that force into the first design?
- A large tier of app servers, to proxy the video out from the store
- An object store, because one file system cannot hold that much video
- Read replicas of the videos table, placed in every region with viewers
- An edge network near the viewers, in place of seventy links from an origin
Uploads come to about 7 GB a second at the peak. What does that number decide?
- Nothing: it sits under one 100-gigabit link, so you say so and move on
- The size of the app tier, as each server has to carry its share of it
- The size of the CDN, as the edge has to take in what creators send
- The storage bill, as that rate is what fills the object store each year
A hundred thousand uploaded hours a day comes to 12,500 machines. Which assumption does that rest on?
- Every hour is encoded once at one bitrate, then cut up for each device
- One machine encodes all three renditions of a video at three times real time
- One machine encodes one rendition in real time, and there are three renditions
- Each upload server encodes what it receives, and one hour of video keeps it busy
You got correct