How a Twitter-style service works

Posting, timelines, search, trends and counters at huge scale: the architecture of a short-message social network.

On the roadmap:Software Architect · step 5: Learn from the giants

Lesson 16 of 18 · 13 min read · quiz · exercise

The problem

A service where people post short messages, follow others, and read a timeline of recent posts. The numbers are what make it hard: hundreds of millions of users, huge read traffic, and a few accounts followed by tens of millions.

Core operations

  • Post a message (with media, mentions, hashtags).
  • Follow and unfollow.
  • Read the home timeline (people I follow), and a user timeline (one person's posts).
  • Search posts and see trends.
  • Likes, replies, reposts and notifications.

Estimates

Say 300 million monthly users, 100 million daily active, each reading their timeline 10 times a day and posting a few times a week.

  • Timeline reads: 1 billion a day, about 12,000 a second on average and several times that at peaks.
  • Posts: about 50 million a day, a few hundred per second on average.
  • Reads outnumber writes by roughly 20 to 1, so we optimise for reading.

High-level architecture

Figure SDA-RWS-16-1 · Short-message service architecture · © forcsguy.com

Posting

  1. The tweet service validates the post and obtains a unique, time-sortable id (Snowflake-style, see the ID generator lesson).
  2. It stores the tweet in a sharded tweet store, partitioned by tweet or user id. Media goes to object storage and is served through a CDN.
  3. It publishes a "tweet created" event to a stream, so the rest of the work happens asynchronously: fan-out, search indexing, notifications, trends and analytics.

Home timeline: fan-out

The home timeline is the hot path. Two strategies (from the news feed lesson):

  • Fan-out on write: when a user posts, workers look up their followers and push the tweet id into each follower's cached timeline list (for example Redis, keeping only the latest few hundred ids). Reading a timeline is then one quick lookup.
  • Fan-out on read: build the timeline on demand by merging recent tweets of everyone you follow. Cheap to post, expensive to read.

The hybrid used at scale: fan out on write for most accounts, but skip accounts with huge follower counts. Their tweets are fetched at read time and merged into the cached list. This avoids one post creating tens of millions of writes.

Reading the timeline

  1. Fetch the user's cached list of tweet ids.
  2. Merge in recent tweets from followed popular accounts, plus promoted content.
  3. Hydrate ids into tweets, authors and counts using caches (tweet cache, user cache, counters).
  4. Rank (recency, engagement, relationship, relevance) and return a page with a cursor.
  5. Filter muted, blocked and deleted items at read time.

User timeline

A user's own posts are a simple range query: tweets for one author in time order. They are stored by author id and cached, and paginated with a cursor.

The social graph

Who follows whom is a huge graph with fast lookups in both directions (followers of A, accounts followed by B). It is stored in a specialised, sharded store, with caches, and updated asynchronously when users follow or unfollow.

Posts are indexed in an inverted index (term to list of tweet ids), usually built on a Lucene-style engine. The indexer consumes the event stream, so new tweets become searchable within seconds. The index is sharded by time (recent tweets are the most searched and kept in memory) and replicated. Queries combine text relevance, recency and engagement.

Trending topics are not the most common words, but the ones that are unusually frequent right now. A stream processor counts hashtags and phrases in sliding windows (say 5 minutes), compares them to their normal rate, filters spam and sensitive terms, and publishes the result per region.

Figure SDA-RWS-16-2 · Search indexing and push notifications from the same event · © forcsguy.com

Counters and notifications

Likes, reposts and view counts are updated very frequently. Use sharded counters and approximate, eventually consistent values, batching updates, since an exact real-time number is rarely required. Notifications are produced from the event stream and delivered through the notification system (see that lesson).

Scale and reliability

  • Cache everything hot: timelines, tweets, users, counters. The tweet store is the source of truth, not the read path.
  • Shard stores by id, and replicate across zones and regions.
  • Rate limit posting, following and API calls to fight spam and abuse.
  • Degrade gracefully: if ranking is slow, return a chronological timeline; if search lags, show fewer results.
  • Moderation and abuse systems run on the same event stream.
  • Real systems evolve, and details in public talks differ from year to year; the principles above (async fan-out, hybrid push and pull, caching, sharding) are the durable part.

Real-world reading

Check your understanding

Question 1 of 3

0 correct so far

Why does a Twitter-style service precompute home timelines for most users instead of building them on every read?

Go deeper

  • Timelines at Scale (talk) and the Twitter engineering blog by Twitter engineers: search for the talk and blog posts; details of the real system have changed over the years

© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.