Skip to main content

Twitter: Interview Guide

Editorial

Staff-Level Discussion Topics

These open-ended topics test architectural judgment. Practice explaining your reasoning out loud for each.

Real-Time Timeline Updates: Push vs. Pull Refresh

Currently, timelines are loaded on app open and paginated on scroll. For a more engaging experience, Twitter shows a "New tweets" indicator when fresh tweets arrive. How is this delivered?

Options:

  • WebSocket/SSE: Server pushes "new tweets available" events to connected clients.
  • Short polling: Client polls every 30 seconds for timeline updates.
  • Long polling: Client holds an HTTP connection open; server responds when new data is available.

Search Architecture: Real-Time Tweet Indexing

Twitter search requires indexing 600M tweets/day in near-real-time. A search for "Super Bowl" during the game should show tweets posted seconds ago.

Key challenges:

  • Indexing throughput: 7K tweets/sec must be tokenized, analyzed, and added to the search index.
  • Inverted index storage: 600M tweets/day × 30 tokens/tweet = 18 billion index entries/day.
  • Query latency: Search results must return in < 200ms.
  • Relevance ranking: Balance recency with engagement signals.

Recommendation Algorithm: Beyond Chronological

Twitter's "For You" feed uses a recommendation algorithm that surfaces tweets from accounts the user doesn't follow. This is a fundamentally different pipeline from the follow-based timeline.

The staff challenge: designing a recommendation pipeline that processes billions of candidate tweets across 300M users, scores them in real time, and blends them with the follow-based timeline.

Level Expectations

AreaMid-Level (L4)Senior (L5)Staff (L6)
RequirementsLists FRs (post, follow, timeline) and basic NFRsDerives 7K write QPS, 700K read QPS; articulates 100:1 ratio implicationsFan-out write volume (1.4M cache writes/sec); inactive user optimization
Fan-out strategy"Push tweets to followers" (fan-out-on-write)Compares push vs pull; identifies celebrity problem; proposes hybridCalculates celebrity threshold; designs the merge pipeline; analyzes latency budget
Data modelUsers/Tweets/Follows tablesCassandra partition design; Snowflake IDs; timeline cache as Redis listCompound partition keys for large accounts; tiered storage (hot, warm, analytics copy)
API DesignBasic CRUD endpointsCursor pagination; idempotency keys; async fan-out acknowledgmentOpaque cursor tokens; rate limiting semantics; API versioning
ArchitectureClient → Service → DB3-service split; Kafka event bus; Redis timeline cacheMulti-region deployment; Redis cluster sizing math; Kafka consumer groups
Caching"Use Redis"Timeline cache structure; tweet content cache; TTL strategyRedis cluster sizing (~220 nodes); stale-while-revalidate for timelines
Delivery guarantees"Tweets should be delivered"Retry-safe fan-out (dedupe on read); Redis failover; lazy deletion from cacheConsumer offset management; at-least-once vs exactly-once semantics

Interview Cheatsheet

1. Opening Frame (30 seconds)

"Twitter is a 300M-DAU social platform where users post short messages and see a personalized feed. The core challenge is the timeline: assembling a feed from hundreds of followed accounts at 700K+ QPS. The architecture uses fan-out-on-write for regular users — tweets are pre-distributed to followers' Redis timeline caches via a Kafka-driven fan-out pipeline. Celebrities (>100K followers) use fan-out-on-read to avoid melting the write path. Timeline reads are a Redis LRANGE + hydration in under 200ms."

2. Requirements Scoping

  • FRs: Post tweet, follow/unfollow, home timeline (paginated, ranked), user timeline
  • NFRs: 300M DAU, ~7K write QPS (peak ~21K), ~700K read QPS (peak ~2.1M), <200ms timeline latency
  • Consistency: Eventual for timeline; strong for tweet storage
  • Out of scope: Search, DMs, media, notifications, trending topics

3. Core Architecture Components

  1. Tweet Service — stateless, writes to Cassandra, publishes to Kafka
  2. Follow Service — manages social graph in Cassandra
  3. Timeline Service — reads from Redis cache, hydrates tweets
  4. Fan-out Service — Kafka consumer, distributes tweet IDs to follower caches
  5. Cassandra — tweet storage + social graph, partitioned by author / user (followers bucketed)
  6. Redis — timeline cache (200 tweet IDs per user) + tweet content cache
  7. Kafka — event bus for TweetCreated events → fan-out, search, analytics
  8. ID Generation (Snowflake) — 64-bit, roughly time-sorted unique IDs

4. Key Trade-offs to Mention

  • Fan-out-on-write vs read: Write (push) for regular users, read (pull) for celebrities. Hybrid is the sweet spot.
  • Consistency: Pre-computed timeline is eventually consistent — acceptable for social media.
  • Cassandra vs relational: Cassandra for write throughput and partition-key queries; no JOINs needed for our access patterns.
  • Snowflake IDs vs UUID: Snowflake embeds timestamp → roughly time-sorted without secondary index.
  • Cache size vs freshness: 200 tweet IDs per user balances memory (480 GB) with recency.
  • Inactive user optimization: Skip fan-out for users inactive > 30 days (Twitter's cutoff); rebuild on login.
Three ways a tweet reaches a timeline, one lane each, with who, what happens when a tweet is posted, and what happens when a timeline opens. REGULAR AUTHOR, up to 100K followers: its ID is pushed onto the list of each active follower; when a timeline opens, one list read (LRANGE) returns it, since the timeline is already built. CELEBRITY AUTHOR, over 100K followers: the tweet is stored once in the tweets table and not pushed to any follower; when a follower's timeline opens, the celebrity's recent tweets are fetched and merged into the timeline. INACTIVE FOLLOWER, away 30+ days: the fan-out skips them, so nothing is written while they are away; on return their timeline is rebuilt from the tweets table and cached, and the pushes resume.

5. Numbers to Remember

MetricValue
DAU300M
Tweets per day600M
Read:write ratio100:1
Write QPS (avg / peak)7K / 21K
Read QPS (avg / peak)700K / 2.1M
Fan-out cache writes/sec1.4M
Tweet size (with metadata)~480 bytes
Timeline cache per user~1.6 KB (200 IDs × 8B)
Total timeline cache~480 GB (300M users)
Redis cluster~220 nodes (110 primaries + 110 replicas)
5-year storage~525 TB

6. Possible Follow-up Questions

  • "How would you handle trending topics?" — Real-time aggregation of tweet volume per hashtag using a sliding window counter in Redis. Kafka Streams for windowed counts.
  • "How would you add retweets?" — A retweet is a new tweet with a retweet_of field pointing to the original. Fan-out follows the same pipeline.
  • "How do you handle tweet editing?" — Store edit history in the tweet record. Invalidate the tweet content cache on edit. Timeline cache (which stores only IDs) is unaffected.
  • "How do you prevent vote/like manipulation?" — One like per user per tweet: a likes table keyed by (tweet_id, user_id), so a second like overwrites the first. Rate limit likes per user per minute.
  • "What about media (images/videos)?" — Upload to object storage (S3); a CDN (CloudFront) serves it. The tweet stores media_url. Fan-out and caching logic is identical — only the hydrated response includes media.