logo
System Design Case Studies

Design Twitter (course)

case study: Twitter

我们来设计一个类似 Twitter 的社交媒体服务,对标 FacebookInstagram

What is Twitter?

Twitter 允许用户发布/阅读短消息(<= 280 chars),支持 web 与移动端。

Requirements

Functional requirements

  • 发布 tweets(文本/图片/视频)
  • Follow 其他用户
  • Newsfeed 展示关注内容
  • 搜索 tweets

Non-Functional requirements

  • 高 availability,低 latency
  • 可扩展、高效

Extended requirements

  • Metrics / analytics
  • Retweet
  • Favorite

Estimation and Constraints

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

Traffic

假设 1B 用户,200M DAU,每人每天发 5 条:

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

10% 含媒体:

$$ 10 \space percent \times 1 \space billion = 100 \space million/day $$

RPS

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

Storage

Tweets 100 bytes:

$$ 1 \space billion \times 100 \space bytes = \sim 100 \space GB/day $$

媒体 50KB:

$$ 100 \space million \times 50 \space KB = 5 \space TB/day $$

10 年 ~19 PB:

$$ (5 \space TB + 0.1 \space TB) \times 365 \space days \times 10 \space years = \sim 19 \space PB $$

Bandwidth

$$ \frac{5.1 \space TB}{(24 \space hrs \times 3600 \space seconds)} = \sim 60 \space MB/second $$

High-level estimate

TypeEstimate
Daily active users (DAU)100 million
Requests per second (RPS)12K/s
Storage (per day)~5.1 TB
Storage (10 years)~19 PB
Bandwidth~60 MB/s

Data model design

twitter-datamodel

users:用户信息

tweets:内容 + userID

favorites:收藏

followers:followers / followees

feeds:用户 feed

feeds_tweets:N:M

选什么 database?

模型关系较强,但可拆分到多个服务,各自拥有表,避免单库瓶颈。可用 PostgreSQLApache Cassandra

API design

Post a tweet

postTweet(userID: UUID, content: string, mediaURL?: string): boolean

Follow / Unfollow

follow(followerID: UUID, followeeID: UUID): boolean
unfollow(followerID: UUID, followeeID: UUID): boolean

Get newsfeed

getNewsfeed(userID: UUID): Tweet[]

High-level design

Architecture

采用 microservices

User Service:auth + user info

Newsfeed Service:生成/发布 feed

Tweet Service:发推/收藏

Search Service:搜索

Media Service:媒体上传

Notification Service:push 通知

Analytics Service:指标与分析

Inter-service communication

用 REST/HTTP 或 gRPC,配合 Service discovery / service mesh。

Newsfeed

Generation

  1. 拉取关注列表
  2. 拉取相关 tweets
  3. Ranking 算法排序
  4. 分页返回

可预生成 feed 存 cache,定期更新。

Publishing

  • Pull Model:用户请求时生成
  • Push Model:发推时推给 followers
  • Hybrid:普通用户 push,明星用户 pull

Ranking Algorithm

示例 EdgeRank:

$$ Rank = Affinity \times Weight \times Decay $$

现代系统多用 ML 模型。

Retweets

创建新 tweet,type 指向原 tweet。也可单独表。

使用 Elasticsearch

缓存热门 queries/hashtags,用 batch job 更新。

Notifications

Kafka + FCM/APNS。

Detailed design

Data Partitioning

Sharding + Consistent hashing

Mutual friends

用社交图 + graph database(Neo4jArangoDB)。

Metrics and Analytics

Apache Spark 处理 Kafka events。

Caching

缓存 top 20% tweets + pagination,LRU eviction。

Media storage

object storage / HDFS

CDN

使用 CDN

Identify and resolve bottlenecks

twitter-advanced-design

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

相关练习题

Design Twitter (course)

暂无相关练习题