Bird
Raised Fist0

Design Twitter - Tweets, Follow Graph, Timeline Generation at Scale

HLDhardInterview freq: high
Choose your preparation mode4 modes available
🏗️
Design Twitter - Tweets, Follow Graph, Timeline Generation at Scale
Design a scalable system to handle tweets, follow graph, and timeline generation for billions of users
hardSocial Media and Feed SystemsFreq: high
Prerequisites:Fan-OutCachingConsistent Hashing
⚙️
Functional Requirements
  • Users can post tweets with text and media attachments.
  • Users can follow and unfollow other users to build a follow graph.
  • Users can view a personalized timeline showing tweets from followed users in reverse chronological order.
  • Support retweets and likes on tweets.
  • Support real-time timeline updates with low latency (under 100ms P99).
  • Support search and hashtag indexing (optional, out-of-scope focus).
📊
Non-Functional Requirements
  • Support 2 billion daily active users (DAU).
  • Handle 40 tweets per user per day on average.
  • Ensure 99.99% availability for timeline reads and tweet writes.
  • Timeline read latency P99 under 100ms.
  • Durability with multi-region replication and eventual consistency for timeline generation.
❓
Clarifying Questions to Ask
  • What is the expected average and peak tweet write rate per second?
  • How many followers does a typical user have, and what is the max fan-out?
  • Is timeline generation push-based (fan-out on write) or pull-based (fan-out on read)?
  • What is the acceptable staleness for timeline updates?
  • Are retweets and likes required to appear in timelines immediately?
  • What is the expected retention period for tweets and timelines?
Out of Scope
✗ Search and hashtag indexing.
✗ Direct messages and private chats.
✗ User profile management and authentication.
✗ Ad targeting and analytics.
📌
Assumptions
  • 2 billion DAU
  • 40 tweets per user per day
  • Average tweet size 100 bytes
  • Average followers per user 200
⚡
QPS Calculation
write qps2B users × 40 tweets/day ÷ 86400 sec ≈ 925,926 writes/sec
peak write qpsAssuming 3× peak factor: 925,926 × 3 ≈ 2.78M writes/sec
read qps2B users × 200 followers × 40 tweets/day ÷ 86400 sec ≈ 185M reads/sec
💾
Storage Estimate
daily storage2B × 40 × 100 bytes = 8 TB/day
retention days30
total storage8 TB × 30 = 240 TB
with replication240 TB × 3 (replication factor) = 720 TB
🖥️
Server Estimate
tweet write serversAssuming each server handles 10K writes/sec, need 278 servers
timeline read serversAssuming each server handles 50K reads/sec, need 3700 servers
🏗️
Architecture Diagram
Architecture Diagram
          +-------------+
          |  User App   |
          +------+------+
                 |
          +------+-------+
          |  API Gateway |
          +------+-------+
                 |
    +------------+-------------+
    |                          |
+---v---+                  +---v---+
|Tweet  |                  |Follow |
|Service|                  |Graph  |
+---+---+                  |Service|
    |                      +---+---+
    |                          |
    |                   +------+-------+
    |                   | Follow Graph |
    |                   |   Storage    |
    |                   +------+-------+
    |                          |
    |                  +-------v--------+
    |                  | Timeline Gen   |
    |                  |  Service       |
    |                  +-------+--------+
    |                          |
    |                  +-------v--------+
    |                  | Timeline Cache |
    |                  |   (Redis)      |
    |                  +-------+--------+
    |                          |
    +--------------------------+
                 |
          +------+-------+
          |  User Timeline|
          |    Service    |
          +--------------+
🧩
Components
API Gateway
Entry point for all client requests, routes to tweet, follow, and timeline services
Why thisNginx or Envoy provides high throughput, TLS termination, and routing with low latency
Why notDirect client-to-service communication lacks centralized control and complicates rate limiting
Tweet Service
Handles tweet creation, validation, and persistence
Why thisBuilt on Cassandra for write-optimized, horizontally scalable storage with tunable consistency
Why notPostgreSQL requires complex manual sharding at 2.78M writes/sec; B-tree writes don't scale linearly
Follow Graph Service
Manages user follow/unfollow relationships and stores the follow graph
Why thisUses a graph database like Neo4j or a wide-column store like Cassandra optimized for adjacency lists
Why notRelational DBs do not scale well for billions of edges and frequent updates
Timeline Generation Service
Generates user timelines by fan-out on write or fan-out on read strategies
Why thisKafka streams for asynchronous fan-out processing and Redis for caching timelines to meet 100ms P99 latency
Why notSynchronous fan-out on write with relational DB causes high write amplification and latency spikes
Timeline Cache
Caches precomputed timelines for fast retrieval
Why thisRedis with TTL and sorted sets supports efficient timeline queries and automatic expiry
Why notMemcached lacks sorted set support and TTL granularity needed for timeline ordering
User Timeline Service
Serves timeline read requests by fetching from cache or fallback storage
Why thisStateless microservice horizontally scalable behind API Gateway for high read QPS
Why notMonolithic service would not scale to 185M reads/sec and complicate deployment
🔄
Data Flow
1
User App → API Gateway
User posts a tweet or follows/unfollows another user
API Gateway routes request to appropriate service
2
API Gateway → Tweet Service
Persist tweet in Cassandra with user_id and timestamp
Write optimized for high throughput and availability
3
Tweet Service → Kafka Topic
Publish tweet event for asynchronous timeline fan-out
Decouples write path from timeline generation
4
Kafka Topic → Timeline Generation Service
Consume tweet event, fan-out to followers' timelines
Uses follow graph to identify followers
5
Timeline Generation Service → Timeline Cache (Redis)
Update followers' timeline sorted sets with new tweet
Supports fast timeline reads with sorted order

Practice

(1/5)
1. A system design interview requires you to explain how you handle conflicting requirements between latency and consistency. Which component or approach best helps you present this trade-off clearly to the interviewer?
easy
A. Use a decision matrix comparing latency and consistency impacts per component
B. Avoid discussing trade-offs and emphasize only the chosen technology
C. Describe only the eventual consistency model without latency details
D. Focus solely on the fastest technology without discussing consistency implications

Solution

  1. Step 1: Identify the need to clarify trade-offs

    Presenting trade-offs requires explicit comparison of conflicting requirements, such as latency vs consistency.
  2. Step 2: Use a decision matrix for clarity

    A decision matrix systematically compares options, showing pros and cons clearly, which helps interviewers understand your reasoning.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Decision matrix clarifies trade-offs explicitly [check]
Hint: Use decision matrices to clarify conflicting trade-offs [check]
Common Mistakes:
2. A web application requires real-time bidirectional communication with low latency and minimal overhead for interactive gaming. Which technology best fits this requirement?
easy
A. Use WebSockets because they provide full-duplex communication with low latency suitable for interactive use.
B. Use Server-Sent Events because they support unidirectional streaming from server to client efficiently.
C. Use Long Polling because it is simpler to implement and works well for real-time bidirectional communication.
D. Use HTTP/2 multiplexing as it inherently supports bidirectional streaming without additional protocols.

Solution

  1. Step 1: Identify communication direction and latency needs

    Interactive gaming requires low latency and bidirectional communication, which Server-Sent Events and Long Polling do not efficiently provide.
  2. Step 2: Evaluate protocol capabilities

    WebSockets provide full-duplex communication with minimal overhead, ideal for real-time interactive applications. HTTP/2 multiplexing is not widely supported for bidirectional streaming in browsers and adds complexity.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    WebSockets enable low-latency bidirectional streams [check]
Hint: Bidirectional + low latency -> WebSockets [check]
Common Mistakes:
3. In a distributed chat system, at Step 4 the Delivery Worker reads from Kafka and attempts to deliver a message to Bob's device. What happens if Bob's device is offline at this step?
easy
A. The Delivery Worker retries delivery asynchronously and keeps the message in Kafka until acknowledged
B. The Delivery Worker immediately marks the message as delivered and removes it from Kafka
C. The Delivery Worker blocks all other deliveries until Bob's device comes online
D. The Delivery Worker deletes the message permanently to avoid blocking the queue

Solution

  1. Step 1: Trace Delivery Worker decision at Step 4 -- Bob's device offline

    The Delivery Worker reads the message from Kafka but cannot deliver it immediately because Bob is offline.
  2. Step 2: Understand retry and message retention behavior

    The worker retries delivery asynchronously and keeps the message in Kafka until Bob acknowledges receipt. It does not block other deliveries or delete messages prematurely.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Delivery Worker retries and retains message until ack [check]
Hint: Offline devices cause retries, not message loss or blocking [check]
Common Mistakes:
4. At 10x scale, which cache component is most likely to fail first due to stateful eviction metadata, and why?
hard
A. The eviction policy manager, because maintaining frequency counts or recency lists becomes a bottleneck
B. The network layer, because increased traffic causes packet loss
C. The cache storage backend, because disk I/O saturates under high load
D. The TTL timer scheduler, because timers cannot scale linearly with cache size

Solution

  1. Step 1: Identify stateful eviction metadata bottleneck

    Eviction policies like LFU or ARC maintain frequency counts or recency lists, which grow with cache size and cause CPU/memory bottlenecks.
  2. Step 2: Compare other components

    Storage backend scales with hardware; network and TTL timers are less stateful or can be sharded more easily.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Eviction metadata management bottlenecks first at large scale [check]
Hint: Eviction metadata scales poorly, causing bottlenecks first [OK]
Common Mistakes:
5. At 10x scale, which component of the Snowflake ID generation system is most likely to become the bottleneck first, and why?
hard
A. The timestamp source, because clock synchronization across machines becomes impossible at scale.
B. The network bandwidth, because ID generation requires frequent coordination between machines.
C. The sequence number, because generating more than its max IDs per millisecond causes blocking delays.
D. The machine ID allocation, because limited machine ID bits cause ID collisions as machines increase.

Solution

  1. Step 1: Analyze timestamp and network constraints

    Timestamp synchronization (A) is challenging but tolerable with NTP; network coordination (D) is minimal as IDs are generated locally.
  2. Step 2: Evaluate sequence number limits

    Sequence number (B) limits per millisecond can cause blocking but can be mitigated by load balancing or sharding.
  3. Step 3: Identify machine ID allocation bottleneck

    Machine ID bits are fixed (e.g., 10 bits), limiting the number of unique machines. At 10x scale, running out of unique machine IDs causes collisions or requires complex reallocation.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Fixed machine ID bits limit scale -> collisions or reconfiguration needed [check]
Hint: Fixed machine ID bits limit scale before sequence or timestamp issues [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard