Design X / Twitter (timelines, search and trends)
Post short messages that appear in followers' timelines within seconds, search everything posted a moment ago and spot what's trending. Builds on the feed problem of Instagram and adds globally unique ids, real-time search and streaming counts.
Builds on these concepts
In one sentence
Precompute home timelines in memory with hybrid fan-out, give every tweet a time-sortable Snowflake id, and run search and trends off a stream of tweet events.
A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.
Requirements
Functional
- Post tweets, follow accounts
- Home timeline (following + recommended)
- Search recent and older tweets
- Trending topics per region
Non-functional
- Timeline in < 200 ms
- A tweet visible to followers within seconds
- Extreme spikes during live events
- Read-heavy, but writes fan out massively
Back-of-the-envelope estimates
| Metric | Estimate |
|---|---|
| Tweets | ~500M / day ≈ 6K tweets/s, spikes 10×+ during big events |
| Timeline reads | ~300M daily users × 20 opens ≈ 70K reads/s |
| Fan-out | 6K tweets/s × ~200 followers ≈ 1.2M timeline inserts/s |
| Timeline cache | 300M users × 800 ids × 8 B ≈ 2 TB of RAM across a Redis cluster |
API and data model
POST /tweets { text, media?, replyTo? } → { id }
GET /timeline/home?cursor=… → [ tweet ]
GET /search?q=…&since=…
GET /trends?region=ES
tweets(id BIGINT, author_id, text, created_at) -- id is a Snowflake: sortable by time
follows(follower_id, followee_id)
timeline:{user_id} → [tweet_id …] -- Redis listArchitecture
Step by step · Post a tweet
- 01Posts "What a goal!! #WorldCup"
- 02POST /tweets
- 03Assigns a Snowflake id
- 04Stored on its shard
- 05TweetCreated published
- 06Pushes the id into followers' timelines
- 07LPUSH timeline:{follower} id; LTRIM to 800
At this scale · 1M users
Timelines in memory
- ▸Every tweet is fanned out into followers' timeline lists in Redis
- ▸Snowflake ids: unique, time-sortable, generated without coordination
- ▸A tweet event stream feeds search and trends
Tap any box to see what it does.
Deep dive: Snowflake ids
With sharded storage you can't use one auto-increment counter. Twitter's Snowflake builds a 64-bit id from:
| 41 bits: milliseconds since epoch | 10 bits: machine id | 12 bits: sequence |- Unique without coordination: each machine generates up to 4,096 ids per millisecond on its own.
- Sortable by time: ordering by id is ordering by creation time, so timelines and pagination are cheap.
- The same idea (time-prefixed ids) is used by Instagram, Discord and many others.
Deep dive: search and trends in real time
Both consume the tweet event stream. Search keeps a small, in-memory index of the last few days that ingests tweets within seconds, plus larger archive indexes for older ones. Trends count hashtags and phrases per region in sliding windows and flag the ones growing much faster than their usual rate, so a daily topic like "good morning" never trends.
How it changes with scale
Step by step · Read the home timeline
- 01Opens the app
- 02SELECT tweets WHERE author IN (follows) ORDER BY id DESC
- 03Join over follows and tweets
- 04Timeline rendered
At this scale · 100 users
Tweets and follows in one database
- ▸Home timeline = SQL over the people you follow
- ▸Auto-increment ids
Tap any box to see what it does.
| Users | What you run | Why it changes |
|---|---|---|
| 100 | Monolith + PostgreSQL | The timeline query is trivial |
| 10K | Replicas + Redis cache | Timeline queries dominate the DB |
| 1M | Fan-out into Redis timelines, Snowflake ids, event stream, search | Read latency and sharding-friendly ids |
| 100M+ | Hybrid fan-out, sharded tweet store, real-time search and trends | Celebrities and live events create huge spikes |
Key takeaways
- 01Home timelines are precomputed lists of ids in memory, hydrated on read.
- 02Hybrid fan-out keeps celebrity posts from exploding write volume.
- 03Snowflake-style ids are unique without coordination and sortable by time.
- 04A tweet event stream feeds search, trends and notifications independently.
Check your understanding
1 of 3
X (Twitter) keeps each user's home timeline in memory. What does it store?