Practical System Design

Real code, real numbers, real architectures

Video Upload Pipeline

How YouTube handles 500 hours of video per minute

TypeScript
// Browser: Split video into 5MB chunks for resumable upload
async function uploadVideo(file: File) {
  const CHUNK_SIZE = 5 * 1024 * 1024; // 5MB
  const totalChunks = Math.ceil(file.size / CHUNK_SIZE);

  // Get upload session from server
  const { uploadId, uploadUrl } = await fetch('/api/upload/init', {
    method: 'POST',
    body: JSON.stringify({
      fileName: file.name,
      fileSize: file.size,
      mimeType: file.type
    })
  }).then(r => r.json());

  // Upload each chunk with retry logic
  for (let i = 0; i < totalChunks; i++) {
    const start = i * CHUNK_SIZE;
    const end = Math.min(start + CHUNK_SIZE, file.size);
    const chunk = file.slice(start, end);

    await uploadChunkWithRetry(uploadUrl, uploadId, i, chunk, {
      maxRetries: 3,
      onProgress: (pct) => updateProgress(i, pct)
    });
  }

  // Signal completion
  return fetch('/api/upload/complete', {
    method: 'POST',
    body: JSON.stringify({ uploadId })
  });
}

Key Insights

Why chunked upload?
• Resume after network failure (don't restart from 0)
• Show real progress (not just spinner)
• Server can process chunks in parallel
• Memory efficient (don't load 4GB into RAM)

Key numbers:
• 5MB chunk = 2-5 seconds on average connection
• Max 3 retries per chunk = handles flaky networks
• Parallel chunk upload = 3-4x faster on good connections

Quick Reference Numbers

Redis GET
~1ms
p99 latency
Postgres query
~5ms
indexed lookup
S3 GET
~50ms
first byte
Cross-region
~100ms
network RTT
1M requests/day
~12 RPS
average load
1GB RAM
~100K entries
~10KB each
H100 GPU
~100 tok/s
GPT-4 class
99.9% uptime
8.7 hrs/year
allowed downtime

Capacity Planning Cheat Sheet

Users → RPS1M DAU × 10 actions/day ÷ 86400 = ~116 RPS
Storage growth1M users × 10 posts/mo × 1KB = ~10 GB/month
Bandwidth1M views × 5MB avg = ~5 TB/day
Server count10K RPS ÷ 500 RPS/server = 20 servers + 50% headroom