Bird
Raised Fist0

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

HLDhardInterview freq: high
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/6
API Gateway

Receives new tweet request

Accepts HTTP POST with tweet content from user

Protocol:HTTP
BeforeUser composes tweet on client device
AfterAPI Gateway has received tweet request and forwards it
Tweet Service

Stores tweet

Persists tweet data and assigns tweet ID

Protocol:HTTP RPC
BeforeTweet request received from API Gateway
AfterTweet stored with unique ID, ready for timeline update
Follow Graph Svc

Fetches followers

Retrieves list of user IDs who follow the tweet author

Protocol:RPC
BeforeTweet stored with author ID
AfterList of follower IDs retrieved for timeline update
Timeline Gen Service

Generates timeline entries

Creates timeline update entries for each follower

Protocol:RPC
BeforeFollower list available
AfterTimeline updates prepared and pushed to cache
Timeline Cache

Updates cached timelines

Inserts new tweet references into followers' cached timelines

Protocol:Redis SET
BeforeTimeline updates generated
AfterFollowers' timeline caches updated with new tweet
User Timeline Svc

Serves updated timeline

Delivers updated timeline to followers on next request

Protocol:HTTP
BeforeTimeline cache updated
AfterFollowers see new tweet in their timelines
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────┐
│  API Gateway  │
└───────┬───────┘
        │ HTTP POST /tweet
        ▼
┌───────────────┐
│ Tweet Service │
└───────┬───────┘
        │ RPC call
        ▼
┌────────────────────┐
│ Follow Graph Svc   │
└─────────┬──────────┘
          │ RPC call
          ▼
┌────────────────────────┐
│ Timeline Gen Service   │
└─────────────┬──────────┘
              │ Redis SET timeline updates
              ▼
┌───────────────┐
│ Timeline Cache│
└───────┬───────┘
        │ Cache update event
        ▼
┌────────────────────┐
│ User Timeline Svc  │
└────────────────────┘

When a user posts a tweet, the API Gateway receives the request and forwards it to the Tweet Service, which stores the tweet. The Follow Graph Service then retrieves the list of followers for the tweet author. The Timeline Generation Service creates timeline update entries for each follower and pushes these updates to the Timeline Cache using Redis. Finally, the User Timeline Service serves the updated timelines from the cache to followers. This vertical flow ensures efficient timeline updates at scale by separating concerns and leveraging caching for fast reads.

Primary Focus

✓

Practice

(1/5)
1. A video calling system must establish peer-to-peer connections between clients behind NATs and firewalls. Which component is primarily responsible for enabling this connectivity?
easy
A. STUN/TURN servers that assist in NAT traversal and relay when direct connection fails
B. Media Server that relays all media streams to bypass NAT restrictions
C. Signaling Server that encodes and decodes media streams for transmission
D. WebRTC client library that manages user authentication and session persistence

Solution

  1. Step 1: Identify NAT traversal role

    STUN/TURN servers help clients discover public IPs and relay media if direct peer-to-peer fails.
  2. Step 2: Eliminate other components

    Media Server relays media but is not primarily for NAT traversal; Signaling Server handles session negotiation, not media encoding; WebRTC client manages connection but relies on STUN/TURN for NAT traversal.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    STUN/TURN enable NAT traversal, essential for peer connectivity [check]
Hint: STUN/TURN servers enable NAT traversal and relay fallback [check]
Common Mistakes:
2. If the cache warming service crashes after partially warming keys but before marking completion, what is the immediate failure observed during launch?
medium
A. The cache contains stale data from previous runs causing incorrect responses
B. The cache warming service automatically retries from the start, causing duplicate warming and wasted resources
C. The system falls back to a read-through cache mode with no performance impact
D. Clients experience cache misses for keys not warmed, causing increased latency and possible DB overload

Solution

  1. Step 1: Trace failure impact of partial warming crash

    Without completion mark, warming restarts causing duplicate warming and resource waste.
  2. Step 2: Eliminate other options

    Stale data unlikely if warming overwrites; cache misses occur but duplicate warming is immediate failure; fallback read-through causes latency, not immediate failure.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Crash without completion causes duplicate warming retries [check]
Hint: Crash without completion mark causes duplicate warming retries [check]
Common Mistakes:
3. If the Kafka cluster buffering RTMP packets crashes and loses the latest messages, what exactly breaks in the live streaming pipeline?
medium
A. The CDN serves stale HLS segments but continues streaming without interruption
B. The RTMP ingest server retries sending lost packets automatically with no impact
C. The transcoder stalls waiting for missing packets, causing segment gaps and viewer buffering
D. The player switches to a backup stream seamlessly without user noticing

Solution

  1. Step 1: Understand Kafka's role

    Kafka buffers RTMP packets to ensure ordered, durable delivery to the transcoder.
  2. Step 2: Trace failure impact

    If Kafka loses messages, the transcoder stalls (A) due to missing packets, causing segment gaps. CDN (B) serves only what it has, so stale segments appear. RTMP ingest server (C) does not retry lost packets automatically; loss is permanent. Player (D) cannot switch streams without explicit failover.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Kafka loss causes permanent packet loss; RTMP ingest lacks automatic retry [check]
Hint: Kafka loss means lost packets; RTMP ingest has no auto-retry [check]
Common Mistakes:
4. At 10x scale, which component in an Active-Active failover system is most likely to become the bottleneck first, and why?
hard
A. Load balancers, because they cannot handle increased cross-datacenter traffic
B. Client applications, due to inability to handle multiple active endpoints
C. Passive nodes, because they become overwhelmed with failover requests
D. Replication protocol, due to increased conflict resolution overhead and network latency

Solution

  1. Step 1: Identify scaling challenges in Active-Active

    Active-Active requires continuous replication and conflict resolution across datacenters.
  2. Step 2: Analyze bottleneck at 10x scale

    Replication protocol overhead and network latency increase exponentially, causing delays and conflicts that bottleneck the system.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Replication overhead -> first bottleneck at 10x scale [check]
Hint: Replication overhead grows fastest at scale [check]
Common Mistakes:
5. At 10x scale, which component in the multi-tenant SaaS platform is most likely to fail first, and why?
hard
A. Feature Flag Service cache, because stale flag data causes widespread inconsistent behavior
B. Billing database, due to write amplification from synchronous updates per tenant request
C. Tenant Metadata Service, as it becomes a centralized bottleneck for tenant context lookups
D. API Gateway, because it maintains stateful WebSocket connections that cannot be easily sharded

Solution

  1. Step 1: Analyze scaling bottlenecks

    Billing DB (A) can be scaled with sharding; Tenant Metadata (C) can be cached; API Gateway (D) handles stateless HTTP mostly.
  2. Step 2: Evaluate Feature Flag Service cache at scale

    Cache invalidation and stale data grow exponentially, causing inconsistent feature exposure and client errors.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Feature flag cache stale -> inconsistent behavior at scale [check]
Hint: Feature flag cache stale -> first scale failure [check]
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