System design

Nobody watches the file you uploaded

A cook uploads a 2 GB recipe video shot in 4K on a phone. What viewers receive is hundreds of small pieces, at the quality their connection can take, from a server near them. Four versions of a video platform, each one built because the last could not deliver that video.

Jump to the final architecture

Lena films a twelve-minute video of herself making ramen, in 4K, on her phone. The file is about 2 GB. She taps upload, and forty minutes later someone she has never met presses play on a phone in São Paulo, and the first frame shows up in a second or two.

That viewer never receives Lena's 2 GB file. They receive small pieces of a different file, at a quality their connection can take, from a server near them, and the quality can change halfway through without them noticing. Almost every box in a video platform exists to make one of those differences possible.

This post follows Lena's video from her phone to that viewer's screen. Each section starts from something that goes wrong for that video or its viewer, and changes the design just enough to fix it. If you know this territory, the headings tell the story and the last drawing is the result. Learn has the finished design as a worked example; this post is about why each part of it is there.

Seventy times more bytes out than in

Every number below is an assumption for this exercise, rounded so the arithmetic stays easy. None of them is measured from a real service:

  • 10,000 uploads a day, about 1 GB each as they come off the phone: 10 TB of new originals a day. Lena's 2 GB is on the large side.
  • 5 million plays a day. A play lasts about six minutes on average, at about 3 Mbit/s: 360 seconds × 3 Mbit/s is 1,080 Mbit, about 135 MB per play.
  • That makes about 675 TB of video sent to viewers every day, which averages out to about 60 Gbit/s, around the clock. Evenings are busier; at three times the average, the peak is close to 200 Gbit/s.

The two directions are wildly unequal. Uploads are rare, about one every nine seconds, and big. Plays are constant, and they move almost seventy times as many bytes as come in. The two paths also have different deadlines, and those deserve to be written down before anything is drawn. An upload can take as long as Lena's connection needs. Processing can take minutes: a new video should be watchable soon, not instantly. The first frame for a viewer has to appear within a couple of seconds, and after that the video must not stop to load.

Getting 2 GB into storage without the API touching it

Start with the smallest platform that works end to end. There are four boxes: the creator app on Lena's phone, a Video API, a relational database for video metadata, and object storage for the video files. The player on the viewer's phone talks to the same API.

The upload goes like this:

  1. Lena's app tells the Video API it wants to upload a 2 GB video.
  2. The API inserts a row in the videos table with an ID, Lena as the owner, the title and the state uploading.
  3. The API answers with presigned URLs: addresses in video storage that allow exactly this upload, to exactly one key, originals/v_8f3a, for a limited time.
  4. The app sends the file straight to storage, in parts of, say, 16 MB: about 125 of them.
  5. When the last part is in, the app tells the API. The API asks storage to assemble the parts into one object, checks its size, and moves the row to uploaded.

Playback is just as simple. The viewer's player asks the API for the video; the API reads the row, checks that the video may be shown, and answers with a signed URL to the file in storage, valid for a few hours. The player fetches that file over HTTP and plays it.

Stage 1 of 4: Get the video in, and play it back
Get the video in, and play it backThe creator app asks the Video API for an upload URL and sends the file directly to video storage, an object store. The API records each video in a relational metadata database. The player asks the API for the video and then fetches one file straight from video storage.direct uploadone fileCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESVideo storageS3 / BlobPLATFORM & RUNTIME
Get the video in, and play it backThe creator app asks the Video API for an upload URL and sends the file directly to video storage, an object store. The API records each video in a relational metadata database. The player asks the API for the video and then fetches one file straight from video storage.direct uploadone fileCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESVideo storageS3 / BlobPLATFORM & RUNTIME

The API hands out signed URLs and keeps the row; the bytes go straight between the apps and storage.

Learn moreObject storage & blobsLarge file upload

Notice that the drawing has no edge between the API and storage. The API signs URLs, keeps the metadata and, once per upload, tells storage to finish it; it never carries the video. If it did, the simple version would be one request holding 2 GB. On a phone uploading at 20 Mbit/s, 2 GB (16,000 Mbit) takes about 800 seconds: some 13 minutes during which one API instance is tied to one slow client, relaying bytes it has no use for. A deploy or a crash in those 13 minutes throws the whole upload away. Object storage is built for exactly this traffic, and multipart upload gives Lena something that single request could not: when her connection drops at part 90, the app resends part 90, not the first 89. How presigned URLs pin what was approved, and how an upload resumes, is Large file upload in Learn.

The order of the steps matters too. The row exists before any byte is sent, so a video is never in storage without the platform knowing whose it is. And the row only says uploaded after storage has assembled the object and its size matches, because the app saying “done” is not proof: part 124 may never have arrived. Many object stores can also announce a finished object as an event, so a platform can react to storage directly instead of waiting for the app to report; either way, the state change follows storage, not the client.

This version is worth building, because every part of it survives to the end of the post: the API that never touches the bytes, the metadata row with its state, the storage that holds the files. And it already plays Lena's video. It just plays it badly, for most of the people who try.

The uploaded file is the wrong file to watch

Lena's phone recorded 4K video compressed with HEVC (H.265), a codec that packs good quality into fewer bits. 2 GB over twelve minutes is about 22 Mbit/s. Three things go wrong with that file:

  • Not every device can decode it. HEVC support varies between browsers and devices, and a viewer whose browser cannot decode it gets an error instead of a video.
  • Most connections cannot keep up with it. A phone on a mobile network getting 5 Mbit/s needs more than four seconds to download each second of a 22 Mbit/s video. It stops to load almost at once and keeps stopping.
  • It is far bigger than a phone needs. A phone shows the video a few inches wide. Sending it 4K spends the viewer's data plan on pixels most phone screens cannot show.

So the platform makes new files from the original: renditions, the same video at several sizes and bitrates, in a codec every device plays. Producing them means decoding the original and encoding it again, which is transcoding. A reasonable ladder for this exercise is four renditions in H.264, which almost every browser and phone can decode: 1080p at about 5 Mbit/s, 720p at 3, 480p at 1.5 and 360p at 0.8. For Lena's twelve minutes that is about 450, 270, 135 and 72 MB: under 1 GB for all four, less than half the original. A 4K rendition can come later, for the videos that get watched on big screens.

Transcoding is heavy work. Assume one worker takes about twice the video's length to produce all four renditions: about 24 minutes of one worker's time for Lena's video. That rules out doing it inside the request that confirms the upload, which would make the app wait 24 minutes for an answer. The confirmation has to hand the work to something else, and come back right away.

That something is a queue. When the API confirms the upload, it now sets the row to processing and puts a job on the transcode jobs queue: a small message saying “transcode video v_8f3a”. A pool of transcoder workers takes jobs off the queue, one at a time per worker. A worker reads the original from storage, writes each rendition back to storage at a path built from the video ID and the rendition, such as videos/v_8f3a/720p.mp4, and when all four exist, sets the row to ready. The original stays apart, under originals/. Until then Lena's app shows “processing”, and her video cannot be played.

Stage 2 of 4: Transcode off the request
Transcode off the requestA transcode jobs queue and a transcoder now sit between the API and video storage. When an upload is confirmed, the API puts a job on the queue; that arrow is dashed because the API does not wait for the work. The transcoder takes the job, writes the renditions to video storage and marks the video ready in the metadata database. The player still fetches one file from storage.direct uploadone filetranscode jobrenditionsCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIME
Transcode off the requestA transcode jobs queue and a transcoder now sit between the API and video storage. When an upload is confirmed, the API puts a job on the queue; that arrow is dashed because the API does not wait for the work. The transcoder takes the job, writes the renditions to video storage and marks the video ready in the metadata database. The player still fetches one file from storage.direct uploadone filetranscode jobrenditionsCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIME

The API queues a job; transcoder workers take jobs at their own pace and mark the video ready.

Learn moreMessage queues & async workersMedia processing & transcodingQueue-Based Load Leveling

Say the average upload is about seven minutes of video, so 14 minutes of transcoding at the assumed speed. Ten thousand uploads a day is 140,000 worker-minutes a day, and a day has 1,440 minutes: on average, about 100 workers are busy around the clock. But uploads bunch up in the evening; at three times the average, the evening needs 300 busy workers to keep up. Without a queue, the platform would have to run 300 workers or turn uploads away. With one, it can run fewer, say 150: during the evening rush the queue grows, videos uploaded at the busiest moment wait longer to become watchable, and with arrivals bunched like this the backlog drains in the quieter hours, since over a whole day 150 workers have 216,000 worker-minutes for 140,000 of work. A short pool makes processing wait; it never makes an upload fail, because uploads do not wait on a worker. That second use of the queue is queue-based load leveling: a burst becomes a delay instead of an overload. The pool can also grow and shrink with the length of the queue, which keeps the delay shorter without paying for 300 workers all day.

The player changes a little. It now asks the API for the video and gets back a list of renditions, picks one, usually by screen size and a quick guess at the connection, and plays that one file. A viewer at home on Wi-Fi gets 720p on their phone. Hold on to that viewer; they are the problem of the next section.

This stage also brings two costs. The platform now stores the renditions as well as the original. It could delete Lena's original after transcoding, but keeping it is what lets the platform re-encode every video later, with a better codec or a new rendition, without asking creators to upload again. At 10 TB of originals a day, that is about 3.65 PB a year, so originals usually move to a cheaper, slower storage class after a while. The other cost is a gap in the API: it updates the row and then puts the job on the queue, and an API that crashes between the two leaves a video in processing with no job behind it. A periodic sweep that requeues any video still in processing for longer than the evening backlog ever lasts closes that gap; the next section explains why a duplicate job is harmless.

When the transcoder dies at 80%

Nineteen minutes into Lena's 24-minute job, the machine running the worker is reclaimed. What happens to her video?

The queue never handed the job over for good. When a worker takes a message, the queue hides it from other workers for a while, the visibility timeout, and the worker deletes the message only after the work is done. A worker that dies deletes nothing, so the message becomes visible again when the timeout runs out, and another worker takes it and starts over.

The timeout needs care in a pipeline like this. Set it shorter than the job, and a healthy worker still busy at minute 20 sees its job handed to a second worker: two machines transcoding the same video. Set it to an hour, and a job whose worker died at minute 19 sits invisible for another 41 minutes. The usual way out is to keep the timeout short, say five minutes, and have a living worker extend it every few minutes while it works; queues such as Amazon SQS let a consumer do that. A dead worker stops extending, and its job is back on the queue within five minutes. Lena's video, which would have been ready after 24 minutes, is ready after about 48: the 19 lost minutes, up to five waiting for the timeout, then 24 more.

Starting over has to be safe even when the first worker was only slow and is still running, so two attempts can be at work at once. Each attempt writes under a prefix of its own, such as videos/v_8f3a/a2/, and publishes only at the end, with one conditional update: set the row to ready, pointing at its prefix, only if the row still says processing. The first attempt to finish wins; a later one changes nothing, and its files are deleted. No viewer is sent to an unfinished file, a published video's files are never rewritten, and the sweep from the previous section may safely requeue a job that was not really lost.

Some jobs fail no matter how many times they run. If Lena's file was cut short in transit, the decoder throws on it every time, and an unlimited retry would keep a worker busy on it forever. So the worker sorts its errors three ways. The file cannot be decoded: that will never pass, so the video fails at once. Something about this job went wrong, such as the encoder crashing on one rendition: that gets a few attempts, say three, and then the job moves to a separate dead-letter queue where a person can look at it. A dependency is down, storage or the database not answering: that says nothing about the video, so the job goes back on the queue with a growing delay and does not use up an attempt. That means the worker keeps its own count of attempts in the row, because a queue's built-in dead-letter rule, such as SQS's, counts every delivery; otherwise a five-minute outage would fail every video in progress. When a video does fail, the row moves to failed and Lena is told to upload again. A video should end up ready or failed; “processing” is not allowed to be forever.

Note Losing 19 minutes of work is acceptable for a 12-minute recipe. For a two-hour film it would mean hours. Large platforms cut the source into chunks of a few minutes and transcode each chunk of each rendition as its own job, so a crash costs one chunk and the chunks run in parallel. Worked design: video streaming in Learn walks through that pipeline.

The connection changes in the middle of the video

Back to the viewer who got 720p on Wi-Fi. Four minutes in, they leave the house and their phone switches to the mobile network, which on that street gives about 2 Mbit/s. The player keeps fetching the same 720p file, which needs 3 Mbit/s. Each second of video now takes a second and a half to download, so the 30 seconds the player had buffered drain at a third of a second per second. About 90 seconds later the buffer is empty and the video stops, a spinner in the middle of a bowl of ramen. It stops again every few seconds after that, because the file is still 720p and the connection is still 2 Mbit/s.

The player never needed the whole file before playing it (a file prepared for streaming starts before it has fully arrived, and HTTP range requests let the player seek), so download size is not the problem. The quality is: it was fixed when playback started, and the file is 720p from the first second to the last. The fix is to let the player change quality as it goes.

So the transcoder changes what it writes. Instead of one file per rendition, it cuts every rendition into segments a few seconds long; six seconds is a common choice. Every segment starts at an independent access point, an IDR frame: a keyframe from which the decoder can start without any earlier frame, and the cuts fall at the same moments in every rendition: segment 41 of the 720p rendition covers exactly the same six seconds as segment 41 of the 360p one. That alignment is what lets a player take segment 41 in 720p and segment 42 in 360p and play them back to back.

Next to the segments the transcoder writes a manifest: a small text file listing the renditions, their bitrates and resolutions, and where each segment is. Two common formats do this, HLS and MPEG-DASH. In HLS, which this post uses, the manifest is a short playlist of the renditions plus one playlist per rendition listing its segments.

The player now works one segment at a time. It fetches the manifest, then the segments, and times each download. While a segment of 720p arrives faster than it plays, it stays on 720p or moves up. When the viewer walks out of the house, the 720p segments start arriving more slowly than real time, and the player asks for the next segment in 480p, at 1.5 Mbit/s, which a 2 Mbit/s connection can carry. The picture gets softer for a while and does not stop. When the phone is back on Wi-Fi, the player climbs again. This is adaptive bitrate streaming (ABR): the server offers every quality, and the player decides, segment by segment, which one its connection can take right now. Most players weigh how full their buffer is as well as the measured speed. There is still a floor: below 0.8 Mbit/s, even 360p cannot keep up, and the video stops.

Stage 3 of 4: Small pieces, at the quality the connection can take
Small pieces, at the quality the connection can takeThe same boxes as before. The transcoder now writes each rendition as short segments plus a manifest to video storage, and the player's edge to storage changes from one file to the manifest, then segments: the player reads the manifest and fetches segments one by one, choosing a rendition for each.direct uploadtranscode jobrenditionsmanifest, then segmentsCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIME
Small pieces, at the quality the connection can takeThe same boxes as before. The transcoder now writes each rendition as short segments plus a manifest to video storage, and the player's edge to storage changes from one file to the manifest, then segments: the player reads the manifest and fetches segments one by one, choosing a rendition for each.direct uploadtranscode jobrenditionsmanifest, then segmentsCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIME

No new box: the transcoder writes segments and a manifest, and the player chooses a rendition per segment.

Learn moreWorked design: video streaming

The same idea fixes the start of playback. To show the first frame, a typical player fetches the manifest and the whole first segment. A six-second segment of 1080p at 5 Mbit/s is 30 Mbit, about 3.75 MB, which takes 1.5 seconds on a 20 Mbit/s connection; the same six seconds of 360p is 4.8 Mbit, 0.6 MB, about a quarter of a second. So many players start on a low rendition, show the first frame quickly, and climb as soon as the measurements say the connection can take more. Seeking works the same way: jumping to minute 8 means fetching the segment that covers minute 8, in whatever rendition fits.

Nothing new appears in the drawing, but the work changed in three places, and each has a cost:

  • Many small files. Twelve minutes in six-second segments is 120 segments per rendition: 480 segment files for Lena's video, plus five playlists. Every play is now dozens of requests instead of one; the average six-minute play fetches about 60 segments.
  • The manifest is written last. If the playlists went out before every segment existed, a player could ask for segment 97 and get a 404. The transcoder writes all the segments first, then the playlists, then sets the row to ready, the same rule as before, with one more step at the end.
  • Access needs rethinking. A presigned URL opens one object, and a play now reads dozens of them, often more than a hundred. Signing every segment URL into the playlists would work, but it is clumsy; the next stage replaces it with one signature that covers the whole video.

Sixty gigabits a second from one bucket

Lena's video now plays smoothly for one viewer. The platform has five million plays a day, and every segment of every one of them comes out of one storage service in one region. That is about 60 Gbit/s on average and close to 200 Gbit/s in the evening, all of it leaving one place, and object storage bills that traffic out per gigabyte at most providers.

Distance hurts as much as volume. Say the storage is in Europe, and the viewer in São Paulo is about 200 ms away from it for a round trip. Before the first frame, the player opens a connection (one round trip for TCP, one more for TLS 1.3), fetches the list of renditions, then one rendition's playlist, then the first segment: at least five round trips, a full second before any video data arrives, and that is before bandwidth counts at all. The same five round trips to a server 20 ms away take a tenth of a second. Every later segment pays at least one round trip too.

Both problems have the same answer: a CDN (content delivery network) in front of storage. A CDN runs caching servers in many locations close to viewers, points of presence. The player now fetches the manifest and segments from the CDN. The first request for a segment at a location is a miss: the CDN fetches it from storage, keeps a copy and answers. Every later request for it there is served from that copy. Storage becomes the origin, and sees misses only.

Stage 4 of 4: Serve the pieces from near the viewer
Serve the pieces from near the viewerThe final architecture. A CDN now sits between the player and video storage: the player fetches the manifest and then the segments from the CDN, and the CDN goes to video storage only for what it does not hold yet. The upload and transcoding path is unchanged.direct uploadtranscode jobrenditionsmanifest, then segmentsmisses onlyCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIMECDNContent DeliveryPLATFORM & RUNTIME
Serve the pieces from near the viewerThe final architecture. A CDN now sits between the player and video storage: the player fetches the manifest and then the segments from the CDN, and the CDN goes to video storage only for what it does not hold yet. The upload and transcoding path is unchanged.direct uploadtranscode jobrenditionsmanifest, then segmentsmisses onlyCreator AppUploads or broadcasts contentUploads or broadcastscontentCLIENTS & UIVideo APIRuns the business logicSERVICES & APISPlayerSPA / MobileCLIENTS & UIVideo metadataGeneric SQL storeDATA STORESTranscode jobsQueueMESSAGING & WORKFLOWSTranscoderTranscoding / pipelineSERVICES & APISVideo storageS3 / BlobPLATFORM & RUNTIMECDNContent DeliveryPLATFORM & RUNTIME

The player reads from the CDN; storage only answers the CDN's misses.

Learn moreCDN & static deliveryHot keys & cache stampedesHTTP caching: Cache-Control, ETags & revalidation

Video caches unusually well, because of a decision already made. Once Lena's video is ready, its segments and playlists never change: they live at fixed paths, and if the platform ever re-encodes the video, it writes the new version to new paths, such as videos/v_8f3a/r2/…, rather than overwriting the old ones. A file that never changes under its name can be cached for as long as anyone wants, so the transcoder stores it with a caching header that gives the CDN a long lifetime and the viewer's device a short one, for example Cache-Control: s-maxage=31536000, max-age=3600: a year at the edge, an hour in the browser. Nothing goes stale, because nothing at that address is ever rewritten. The exception is taking a video down. The platform deletes the files from storage, so the origin cannot serve them again, and purges the CDN, so the edges drop their copies once the purge has spread. A browser may still reuse a piece it already holds for up to an hour, and nothing takes back what a viewer has already downloaded.

How much reaches storage depends on the share of bytes the CDN serves from its caches. Popular videos are almost all hits; the long tail of rarely watched videos misses more often. If 95% of the bytes come from the edge, storage serves 5% of 60 Gbit/s: about 3 Gbit/s, which one region handles easily.

The access question from the previous stage gets its answer here: the API signs, the CDN checks. When a viewer asks to play Lena's video, the API checks that they may see it and returns a signed cookie, or a token that covers every path under videos/v_8f3a/, valid for a few hours. The original is not under that prefix, so no token reaches it. The CDN verifies the signature and the expiry on every request before serving anything, from its cache or from storage, and storage itself stays private: viewers can reach it only through the CDN. One detail decides whether this works: the signature must not be part of the cache key, the name a copy is stored under. If it were, every viewer's token would make a different key for the same segment, viewers would stop sharing copies, and almost every request would miss.

Lena publishes, and every edge is cold

A few months on, Lena has two million subscribers. She publishes a new video, and her subscribers get a notification. Say 100,000 of them press play in the first few minutes, spread across 200 CDN locations. None of those locations has the new video yet.

Every one of those viewers starts at the beginning, and because this platform's player starts on a low rendition, almost every one of them asks for the same few files: the manifest, the 360p playlist, and the first segments of the 360p rendition. Each location's first request for each of those files misses. And while that first miss is on its way to storage, the next viewers at the same location ask for the same file, and they miss too, because the copy has not arrived yet. Without help, thousands of requests for the same handful of objects reach storage in the same few seconds, each one a full trip to the origin region and a billed request, with a viewer waiting on every one of them. A stage-3 decision, start low so the first frame is fast, is what concentrates the crowd on so few files.

Two CDN features bring that down, and neither is a box you build. Request collapsing: a location that already has a miss in flight for a file holds the next requests for it and answers them all from the one fetch. That leaves at most one fetch per location per file, 200 for each of the hot files. An origin shield takes it further: locations send their misses to one of a few regional shield caches, which collapse them again, and only a shield's miss reaches storage. With four shields, storage sends each hot file about four times. The cost is an extra hop on a miss, and on a release like this, it is the difference between storage seeing thousands of requests for the same few files and seeing a few dozen. This failure has a post of its own on this blog, linked at the end.

What each failure reaches

The finished design has two paths that meet only at the API, storage and the metadata database: the upload path (creator app, API, queue, transcoder) and the playback path (player, API, CDN). Reading the drawing by failure shows which path each one reaches, and who notices:

  • Video storage is down → uploads fail, and Lena's app resumes them later, part by part. Every cache miss fails, for new plays and running ones alike, while whatever the CDN already holds keeps playing, which for a popular video can be almost everything. Transcoding jobs wait and retry without using up their attempts, as the transcoder section set up.
  • The API is down → no new play can start, because nobody can get a token, and no upload can begin. Uploads already in progress keep sending parts to storage, but cannot be confirmed until the API returns. Plays already running continue, until their tokens expire. That makes the token's lifetime a trade-off: hours keep long plays alive through an API outage, minutes let the platform stop a viewer sooner after their access is revoked.
  • The metadata database is down → the API cannot look videos up or record new ones, so for viewers and creators it looks like the API being down. Transcoders cannot mark videos ready, so their jobs wait and retry the same way, without using up attempts.
  • The queue is down → uploads still complete; videos wait in processing and the sweep queues them when it is back. Playback is untouched.
  • The transcoders are down or behind → the queue grows and new videos take longer to become watchable. Playback is untouched.
  • One CDN location fails → viewers there are routed to another nearby location, whose cache may be colder: more misses for a while, through the shield, not straight at storage.
  • The CDN as a whole fails → playback stops, cached or not, because storage was made private behind it. Large platforms pay for a second CDN to cover this; this design accepts the risk.

The pattern is the one the deadlines asked for. Everything slow and heavy sits on the upload path, behind a queue, where failure means waiting. The playback path depends on the CDN, the API to start, and storage only for misses, so most failures on it either stop new plays from starting or reach only the files the CDN does not hold. The exception is the CDN itself: it is the one dependency every running play shares.

The final architecture, box by box

Eight boxes:

  • Creator app: uploads straight to storage, in parts that can be resent one at a time.
  • Video API: signs every upload and every play, and never carries video bytes.
  • Video metadata: one row per video, whose state (uploading, processing, ready, failed) decides what anyone can do with it.
  • Video storage: holds the original and every segment, private; viewers reach it only through the CDN.
  • Transcode jobs: lets the API answer at once, and turns the evening's uploads into a backlog instead of an overload.
  • Transcoder: turns one file nobody should watch into renditions cut into aligned segments, plus the manifest written last, and safely redoes a job that was lost.
  • Player: asks the API once, then chooses a rendition for every segment.
  • CDN: serves the segments from near the viewer, checks the API's signature, and sends storage its misses only, collapsed.

Left out on purpose

  • Live streaming. The same segments and manifests, but the transcoder produces them while the event happens and the playlists keep growing, which changes most of the latency budget.
  • View counts and analytics. Counting five million plays a day must stay off the playback path; Learn's worked design shows one way, and Counting at scale covers the general problem.
  • Recommendations and search. Both are large systems of their own, fed from the metadata, and change nothing about how a video reaches a screen.
  • Checks before publishing. Malware scanning and content moderation belong in the transcoding pipeline, before a video is marked ready; Media processing & transcoding covers them.
  • DRM. Signed URLs decide who may fetch a segment. They do nothing about what a viewer does with it afterwards; encrypting the video for licensed playback is a separate system.
  • More than one storage region. Managed object storage is commonly replicated across the zones of its region, which covers losing a data centre; a second region is for losing the whole region, and this exercise does not need it yet.

Build it yourself

The Video Streaming Platform Challenge asks for this design and more: its brief adds live streaming, recommendations, search, analytics and geo-restrictions, so draw the two paths from this post first and add those around them. Its palette names some boxes differently: a React app plays the video, a Kafka topic holds the transcode jobs, a worker service transcodes, and PostgreSQL keeps the metadata. Diagnose: Streaming Platform With No Edge Delivery starts close to where stage 3 ended: every play is read straight from the origin, which runs in a single zone; put the CDN in front, and then make what still reaches the origin survive losing that zone.