Case Studies/Social Media

Twitter / X

Real-time global public conversation with fan-out at scale

socialtimelinefan-outreal-timesearchtrending

Case study boundary

Use this as a learning model.

This page explains architectural patterns and constraints inspired by Twitter / X. It should not be read as the exact private production architecture or as a one-size-fits-all recommendation.

Evidence

Public info + inference

Learn first

Concepts behind this architecture

Overview

Twitter (now X) is a real-time microblogging platform where users post short messages (tweets) that are distributed to their followers’ timelines. The core technical challenge is the fan-out problem: when a user with millions of followers tweets, that tweet must appear in millions of timelines within seconds. Twitter processes over 500 million tweets per day and serves billions of timeline requests.

Scale

550M+

Monthly active users

500M+

Tweets per day

~300K

Timeline requests/sec

Petabytes

Data storage

Requirements

Functional

  • Post tweets (text, images, video, polls)
  • Home timeline (algorithmic + chronological)
  • Retweets, likes, replies, and bookmarks
  • Real-time trending topics
  • Full-text tweet search
  • Direct messages
  • Follow/follower social graph

Non-Functional

  • Sub-second timeline delivery
  • Handle viral tweet fan-out (celebrity problem)
  • Real-time search indexing
  • High write throughput for peak events
  • Global availability
  • Support for real-time event spikes

High-Level Architecture

Loading diagram...
Twitter — High-level system architecture

Key Components

Tweet Service

Handles tweet creation, storage, and retrieval. Persists tweets to Manhattan (distributed KV store) and triggers the fan-out pipeline.

Fan-Out Service

When a tweet is published, pushes the tweet ID into Redis timeline caches of all followers. Uses different strategies for high and low follower-count users.

Timeline Service

Assembles a user’s home timeline by reading from pre-computed timeline cache (Redis) and hydrating tweet objects. Applies ranking algorithms for the algorithmic feed.

Social Graph Service

Manages the follow/follower graph using FlockDB. Supports queries like “who follows X” and “does A follow B” at massive scale.

Search (Earlybird)

Real-time search engine that indexes tweets within seconds of posting. Built on a custom inverted index optimized for recency-biased queries.

Trending Service

Analyzes the real-time tweet stream using Heron to detect emerging topics. Distinguishes trends from sustained high-volume topics.

Data Model

Loading diagram...
Twitter — Entity relationship diagram

The Fan-Out Problem

The fan-out problem is Twitter’s defining architectural challenge.

Fan-Out-on-Write (Push):

  • When a tweet is posted, push the tweet ID to every follower’s timeline cache
  • Each user’s timeline is a Redis sorted set of (tweet_id, timestamp)
  • Timeline cache holds ~800 most recent tweet IDs
  • Used for: Users with < ~50K followers

Fan-Out-on-Read (Pull):

  • For celebrity users (130M+ followers), fan-out-on-write is prohibitive
  • Their tweets are fetched at read-time and merged with the pre-computed timeline
  • Used for: Users with > ~50K followers

The Math:

  • Average user: ~200 followers → 200 Redis writes per tweet
  • Celebrity with 50M followers → 50M writes per tweet (200GB at 4KB each!)
  • The hybrid approach is essential

Timeline Assembly:

  1. Read pre-computed timeline from Redis (pushed tweets)
  2. Fetch recent tweets from followed celebrities (pulled tweets)
  3. Merge by timestamp
  4. Apply ML ranking for “For You” tab
  5. Hydrate tweet objects
  6. Return final timeline
Loading diagram...
The Fan-Out Problem

Real-Time Search with Earlybird

Twitter’s search engine, Earlybird, is designed for real-time search — indexing tweets within seconds.

Architecture:

  • Tweets flow through Kafka into Earlybird indexer instances
  • Each instance maintains an in-memory inverted index for recent tweets
  • Older tweets stored in on-disk index segments
  • Queries fanned out to all index partitions and results merged

Index Design:

  • Inverted index: Maps terms to posting lists (tweet IDs)
  • Real-time segment: New tweets in a mutable in-memory segment
  • Optimized segments: Periodically flushed to read-only segments
  • Recency ranking: Results biased toward recent tweets

Query Processing:

  1. Parse query into tokens with operators (AND, OR, from:, filter:)
  2. Fan out to all Earlybird partitions
  3. Each partition returns top-K local results
  4. Merge, deduplicate, and globally rank
  5. Social signals (likes, retweets) boost relevance

Earlybird indexes tweets and makes them searchable in under 10 seconds.

Trending topics are detected in real-time from the global tweet stream.

Pipeline:

  1. All tweets flow through Kafka into the stream processing layer
  2. Heron processes the stream (successor to Storm)
  3. Hashtags, keywords, and phrases are extracted and counted in sliding windows
  4. Algorithm distinguishes trends (sudden spikes) from sustained volume

Trend Detection Algorithm:

  • Maintains baseline expected volume for each term (hourly, daily, weekly patterns)
  • Detects when current volume significantly exceeds the baseline
  • Uses statistical tests (z-score) to determine significance
  • Filters spam, adult content, and manipulation attempts

Personalization:

  • Trends are localized by geography and language
  • “Trends for you” incorporates user interests and social graph
  • Each user may see different trending topics

The system processes ~6K tweets/second average (150K+ during peaks) with sub-minute trend detection latency.

Architectural Tradeoffs

Hybrid fan-out (push + pull)

Pros

  • +Avoids 50M+ writes for celebrity tweets
  • +Fast timeline reads for most users
  • +Balances write amplification

Cons

  • −Complex merge logic
  • −Celebrity tweet latency is higher
  • −Two code paths to maintain

Redis for timeline caching

Pros

  • +Sub-millisecond read latency
  • +Sorted sets are perfect for timelines
  • +Simple and battle-tested

Cons

  • −Memory-intensive at Twitter scale
  • −Data loss risk on failures
  • −Cost of billions of sorted sets

Custom search engine (Earlybird)

Pros

  • +Optimized for real-time indexing
  • +Recency-biased ranking built-in
  • +Fine-grained control over index lifecycle

Cons

  • −Huge engineering investment
  • −Maintenance burden
  • −No community contributions

Learn From Twitter / X

Copy a learning context prompt for an AI assistant. It keeps assumptions visible and asks for tradeoffs, cost drivers, failure modes, and what to measure before adding complexity.

Explore More Case Studies