Design a video streaming service

    A video platform (YouTube/Netflix) ingests uploads, transcodes them into multiple bitrates, and streams adaptive chunks from a CDN to players worldwide. The interview centers on the upload→transcode pipeline and CDN-based delivery.

    Storage & Data, 12 components, about 45 min

    1. Scope

    In scope

    • • Upload video
    • • Transcode to multiple bitrates
    • • Adaptive streaming (HLS/DASH)
    • • Playback metadata + view counts

    Out of scope

    • • Recommendations (separate system)
    • • Live streaming
    • • Monetization/ads

    2. Functional requirements

    Must have

    • • Upload a video
    • • Watch a video at the right quality for the connection
    • • Seek/scrub
    • • Track views

    Nice to have

    • • Captions/subtitles
    • • Thumbnails
    • • Resumable uploads

    3. Non-functional requirements

    PatternWrite-once, read-many— Playback ≫ upload
    Start latency< 2 s to first frame
    Availability99.99% on playback
    StoragePetabytes; tiered hot/cold

    4. Back-of-envelope estimation

    Assumptions

    • • 500 hours uploaded per minute
    • • Each upload transcoded to 5 bitrates
    • • 1B watch hours/day
    • • Chunk = 6 seconds

    Derived numbers

    Uploaded video~720K hours/day— 500 h/min × 1,440
    Raw ingest~1–4 PB/day— At ~1.5–5 GB per hour of source video
    Egress~125 Tbps average— 1B h × 3,600 s × 3 Mbps ÷ 86,400 s; peaks ~2×, served by CDN
    Chunk cache TTLHours— Manifests: seconds

    5. API design

    POST/api/uploads

    Get a signed S3 URL for direct upload

    GET/api/videos/{id}/manifest

    HLS/DASH manifest listing chunk URLs

    GET/chunks/{id}/{bitrate}/{n}.m4s

    Fetch a media chunk (CDN)

    6. High-level design

    What each box in the reference diagram is responsible for:

    Upload API — Issues signed S3 URLs; enqueues a transcode job.
    Raw S3 — Stores the original upload.
    Transcode Queue + Pool — FFmpeg/GPU workers emit 5 bitrates × 6s chunks.
    Encoded S3 — Stores HLS chunks + manifests; CDN origin.
    CDN Edge — Caches chunks near viewers; origin-pulls on miss.
    Metadata DB — PostgreSQL: video info, status.
    View Counter — Cassandra: high-write view counts.
    Recommender — Offline Spark → Redis top-N per user.

    7. Data model

    PostgreSQL: videos(id, owner, title, status, duration)
    S3: {video}/{bitrate}/{n}.m4s + manifest.m3u8

    8. Deep dives

    Why transcode to multiple bitrates

    Networks vary wildly. Adaptive Bitrate Streaming (ABR) encodes each video at several resolutions/bitrates and slices each into short (~6s) chunks. The player measures bandwidth and requests the next chunk at the best bitrate it can sustain — switching seamlessly at chunk boundaries. The manifest (HLS .m3u8 / DASH .mpd) lists all variants.

    Upload path

    Never stream large uploads through app servers. The client gets a signed S3 URL and uploads directly. The upload API enqueues a transcode job; workers read the raw file, transcode, and write chunks to encoded S3. Status flows back to the metadata DB so the UI can show "processing".

    CDN delivery & cache strategy

    Chunks are immutable, so they cache for hours at the edge — a viral video is served almost entirely from CDN. Manifests cache for seconds (so new content/variants appear quickly). Origin shielding ensures only one edge per region pulls a cold chunk from S3.

    View counts at scale

    View counts are extremely write-heavy and don't need strong consistency. Use Cassandra (or a counter service with async aggregation) rather than incrementing a row in the relational DB on every play.

    9. Bottlenecks

    • Transcode is compute-heavy → autoscaling GPU/CPU pool, queue absorbs bursts
    • Egress cost/latency → CDN does the heavy lifting
    • View-count write storms → dedicated counter store

    10. Trade-offs

    Encoding ladder: fixed or per-title

    Choose: Per-title (or per-shot) encoding: analyse complexity and pick bitrates per video, so a cartoon gets lower bitrates than a sports match at the same quality.

    Instead of: One fixed ladder for every video: simpler pipeline, wastes 20–50% of bandwidth on easy content.

    Egress is the dominant cost; spending extra encode CPU once saves bandwidth on every view of popular titles.

    Encode everything or on demand

    Choose: Encode the common renditions at upload; encode rare ones (for example 4K for a video with 10 views) lazily or never.

    Instead of: Full ladder for every upload: simpler, but most uploads are rarely watched.

    View counts follow a power law; most transcode CPU would be wasted on long-tail videos.

    Own CDN or third-party

    Choose: Third-party CDN at first; at very large scale, place caches inside ISPs (as Netflix Open Connect does).

    Instead of: Build edge infrastructure from day one: huge capital cost before traffic justifies it.

    The break-even only arrives at a scale where egress dominates the bill.

    11. Failure modes

    Transcode worker crashes mid-job

    Video stuck in "processing".

    Mitigation: Split into segments transcoded independently; the queue redelivers a segment after its visibility timeout; a job tracker marks the video ready only when all segments exist.

    Viral video cold at the edge

    Thousands of edges miss at once and stampede the origin.

    Mitigation: Origin shield (a mid-tier cache) collapses misses so the origin serves each segment once per region.

    Player picks too high a bitrate

    Rebuffering, the metric viewers notice most.

    Mitigation: Buffer-based ABR (for example BOLA): pick bitrate from buffer level, not just throughput estimates; start low and ramp up.

    12. What interviewers probe

    Answer each one yourself before opening it.

    Estimate egress for 1B watch hours a day.Show a strong answer

    At ~3 Mbps average: 1e9 h × 3600 s × 3 Mb ÷ 86,400 s ≈ 125 Tbps average, with evening peaks around 2×. That is why CDN placement matters.

    Why 2–6 second segments?Show a strong answer

    Shorter segments switch quality faster and start faster but add request overhead and hurt compression; 2–6 s balances both. Low-latency live uses partial segments.

    How would you add live streaming?Show a strong answer

    RTMP/SRT ingest, real-time transcode, short segments (LL-HLS/LL-DASH), manifests updated every segment, and a much shorter cache TTL on manifests.

    How do you resume an upload of a 10 GB file?Show a strong answer

    Multipart or resumable upload directly to object storage with signed part URLs; the client retries failed parts and completes with the part list.

    Sign in to track this guide in your curriculum progress.