logo
System Design Case Studies

Design Netflix

case study: Netflix

我们来设计一个类似 Netflix 的视频流媒体服务,对标 Amazon Prime VideoDisney PlusHuluYouTubeVimeo

What is Netflix?

Netflix 是订阅制流媒体平台,用户可在 web / iOS / Android / TV 等设备观看内容。

Requirements

Functional requirements

  • 用户可观看与分享视频
  • 内容团队(或用户)可上传内容
  • 支持搜索(title / tags)
  • 支持评论

Non-Functional requirements

  • 高 availability,低 latency
  • 高 reliability,上传不丢
  • 可扩展、高效

Extended requirements

Estimation and Constraints

注意:和面试官确认规模假设。

Traffic

假设 1B 用户,200M DAU,每人每天看 5 个视频:

$$ 200 \space million \times 5 \space videos = 1 \space billion/day $$

读写比 200:1,上传 5M/day:

$$ \frac{1}{200} \times 1 \space billion = 5 \space million/day $$

RPS

$$ \frac{1 \space billion}{(24 \space hrs \times 3600 \space seconds)} = \sim 12K \space requests/second $$

Storage

视频平均 100MB:

$$ 5 \space million \times 100 \space MB = 500 \space TB/day $$

10 年约 1,825 PB:

$$ 500 \space TB \times 365 \space days \times 10 \space years = \sim 1,825 \space PB $$

Bandwidth

$$ \frac{500 \space TB}{(24 \space hrs \times 3600 \space seconds)} = \sim 5.8 \space GB/second $$

High-level estimate

TypeEstimate
Daily active users (DAU)200 million
Requests per second (RPS)12K/s
Storage (per day)~500 TB
Storage (10 years)~1,825 PB
Bandwidth~5.8 GB/s

Data model design

netflix-datamodel

users:用户信息

videostitlestreamURLtagsuserID

tags:标签

views:播放记录

comments:评论

选什么 database?

建议拆分为多服务,各自拥有表。可用 PostgreSQLApache Cassandra

API design

Upload a video

uploadVideo(title: string, description: string, data: Stream<byte>, tags?: string[]): boolean

Streaming a video

streamVideo(videoID: UUID, codec: Enum<string>, resolution: Tuple<int>, offset?: int): VideoStream

Search for a video

searchVideo(query: string, nextPage?: string): Video[]

Add a comment

comment(videoID: UUID, comment: string): boolean

High-level design

Architecture

采用 microservices

User Service:auth + user info

Stream Service:视频流播放

Search Service:搜索

Media Service:上传与处理

Analytics Service:指标与分析

Inter-service communication

REST/HTTP 或 gRPC + Service discovery / service mesh。

Video processing

video-processing-pipeline

  • File Chunker:按 scene 或时间切 chunk
  • Content Filter:版权/NSFW 检测,问题放 DLQ
  • TranscoderFFmpeg / AWS MediaConvert
  • Quality Conversion:多分辨率输出,存 HDFS / object storage

message queue 解耦处理流程。

Video streaming

使用 CDN。Netflix 的 Open Connect 本质是自建 CDN。

支持 HLS 等自适应码率。断点续播用 offset

Searching

Elasticsearch

Sharing

可用 URL shortener 服务。

Detailed design

Data Partitioning

Sharding + Consistent hashing

Geo-blocking

用 IP 或地区配置,配合 CloudFront 或 Route53 地理策略。

Recommendations

用 ML / Collaborative Filtering,参考 Netflix research

Metrics and Analytics

Apache Spark

Caching

缓存静态内容,LRU eviction,cache miss 回源。

Media streaming and storage

HDFS / GlusterFS / Amazon S3

CDN

CloudFront / Cloudflare CDN

Identify and resolve bottlenecks

netflix-advanced-design

提升 resilience:多实例、load balancers、DB replicas、分布式 cache 多副本。

相关练习题

Design Netflix

暂无相关练习题