Requirements, estimates and the timeline API
The primer’s use cases and estimates: 100M active users, 6,000 tweets and 100,000 reads per second, 150 TB of new content a month, and the REST API.
What you will learn
- State the Twitter timeline and search use cases and assumptions as the primer does.
- Estimate tweet storage, fan-out deliveries and read rates.
- Size the home timeline cache, including its replicas.
The problem
The primer's Design the Twitter timeline and search notes that designing the Facebook feed and Facebook search are similar questions.
Use cases (from the primer)
- A user posts a tweet; the service pushes it to followers, sending push notifications and emails.
- A user views the user timeline (their own activity) and the home timeline (activity from people they follow).
- A user searches keywords.
- The service has high availability.
Out of scope: the firehose and other streams, visibility rules (such as hiding @replies) and analytics.
Assumptions (from the primer)
- Traffic is not evenly distributed. Posting should be fast, and so should fanning out to followers, "unless you have millions of followers".
- Viewing the timeline and searching should be fast. Twitter is read-heavy, so optimize for fast reads; ingesting tweets is write-heavy.
- 100 million active users.
- 500 million tweets per day (15 billion a month), each delivered to 10 followers on average: 5 billion deliveries a day, 150 billion a month.
- 250 billion read requests and 10 billion searches per month.
Back-of-the-envelope estimates
Using the primer's conversion, 400 requests per second ≈ 1 billion requests per month:
| Quantity | Per month | Per second |
|---|---|---|
| Read requests | 250 billion | |
| Tweets | 15 billion | |
| Fan-out deliveries | 150 billion | |
| Searches | 10 billion |
Size per tweet (primer): tweet_id 8 bytes, user_id 32 bytes, text 140 bytes, media 10 KB on average. That is about 10 KB per tweet.
For comparison, Twitter reported 150 million active users and about 5,000 tweets per second on average (7,000 at the daily peak) in 2013, and 300,000 queries per second to generate timelines. So the primer's figures are the right order of magnitude.
Sizing the timeline cache
The primer keeps home timelines in a memory cache such as Redis. Each entry is a tweet ID, a user ID and a metadata byte: bytes. It keeps "only several hundred tweets for each home timeline", and only for active users. Twitter capped home timelines at 800 entries. With the primer's 100 million active users:
Twitter reported "a couple of terabytes of RAM" for this cluster, and said each tweet is replicated on 3 machines. Replication multiplies the RAM you need, so size the cluster for the copies too.
API
The primer's REST API:
POST /api/v1/tweet
{ "user_id": "123", "status": "hello world!", "media_ids": "ABC987" }
→ { "created_at": "Wed Sep 05 00:37:15 +0000 2012", "status": "hello world!",
"tweet_id": "987", "user_id": "123", ... }
GET /api/v1/home_timeline?user_id=123
→ [ { "user_id": "456", "tweet_id": "123", "status": "foo" }, ... ]
The user timeline API is similar, and search is GET /api/v1/search?query=hello+world. The primer's request also carries an authentication credential, omitted here. For internal calls it suggests RPC.
- Web & Mobile Clients: 1 × Clients & Users
- Route53 Anycast Geo-DNS: 2 × Anycast DNS / Route53
- Application Load Balancer: 4 × Application Load Balancer
- Tweet Ingestion & Write API: 8 × App Compute Server
- Timeline & Feed Read API: 14 × App Compute Server
- Fanout Event Queue: 3 × Message Queue / Kafka
- Redis In-Memory Timeline Cluster: 4 × Redis In-Memory Cache
- Primary DB (User & Tweet SQL): 2 × Primary Relational DB
- S3 Media Storage & CDN: 2 × Object Storage (S3)
- Web & Mobile Clients sends requests to Route53 Anycast Geo-DNS
- Route53 Anycast Geo-DNS sends requests to Application Load Balancer
- Application Load Balancer sends requests to Tweet Ingestion & Write API
- Application Load Balancer sends requests to Timeline & Feed Read API
- Tweet Ingestion & Write API sends requests to Fanout Event Queue
- Tweet Ingestion & Write API sends requests to Primary DB (User & Tweet SQL)
- Timeline & Feed Read API sends requests to Redis In-Memory Timeline Cluster
- Fanout Event Queue sends requests to Redis In-Memory Timeline Cluster
- Tweet Ingestion & Write API sends requests to S3 Media Storage & CDN
- Timeline & Feed Read API sends requests to S3 Media Storage & CDN
Key takeaways
- Twitter is read-heavy, so optimize for fast timeline reads; ingesting tweets and fanning them out is write-heavy.
- 500 million tweets a day at about 10 KB each is 150 TB of new content a month, 5.4 PB in three years.
- 800 entries of 17 bytes for 100 million active users is about 1.36 TB of cache, before replication multiplies it.
Sources
- System Design Primer: Design the Twitter timeline and search
- High Scalability: The architecture Twitter uses to deal with 150M active users, 300K QPS (summary of “Timelines at Scale”, 2013)
- Raffi Krikorian: Timelines at Scale (InfoQ, 2013)
Adapted from The System Design Primer by Donne Martin, licensed under CC BY 4.0. Changes were made.