System Design Playbook
09 · System designs100 users → 100M+ users13 min read

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.

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

MetricEstimate
Tweets~500M / day ≈ 6K tweets/s, spikes 10×+ during big events
Timeline reads~300M daily users × 20 opens ≈ 70K reads/s
Fan-out6K tweets/s × ~200 followers ≈ 1.2M timeline inserts/s
Timeline cache300M 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 list

Architecture

Architecture
Scale
Follow a request
◱Appweb, mobile⇄API gateway⚙Tweet serviceSnowflake ids⚙Timeline service⌕Searchreal-time index↻Fan-out workers☰Tweet eventsKafka↻Trendsstream processing⛁Tweet storesharded key-valueϟTimeline cacheRedis

Step by step · Post a tweet

  1. 01Posts "What a goal!! #WorldCup"
  2. 02POST /tweets
  3. 03Assigns a Snowflake id
  4. 04Stored on its shard
  5. 05TweetCreated published
  6. 06Pushes the id into followers' timelines
  7. 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

Architecture
Scale
Follow a request
◱Appweb, mobile⚙App server⛁PostgreSQLtweets, follows

Step by step · Read the home timeline

  1. 01Opens the app
  2. 02SELECT tweets WHERE author IN (follows) ORDER BY id DESC
  3. 03Join over follows and tweets
  4. 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.

UsersWhat you runWhy it changes
100Monolith + PostgreSQLThe timeline query is trivial
10KReplicas + Redis cacheTimeline queries dominate the DB
1MFan-out into Redis timelines, Snowflake ids, event stream, searchRead latency and sharding-friendly ids
100M+Hybrid fan-out, sharded tweet store, real-time search and trendsCelebrities 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?