Design Twitter

System Design Challenge

hard
45-60 minutes
load-balancermicroservicesmessage-queuecachetimeline-generation

Design Twitter

What is Twitter?

Twitter is a real-time social media platform that allows users to post short messages (tweets), follow other users, and view timelines. It's similar to Facebook, Instagram, or LinkedIn. The service provides real-time messaging, social networking, and content discovery.

Real-time timeline generation with fan-out patterns and social graphs is what makes systems like Twitter unique. By understanding Twitter, you can tackle interview questions for similar social media platforms, since the core design challenges—timeline generation, fan-out patterns, real-time updates, and social graphs—remain the same.


Functional Requirements

Core (Interview Focussed)

  • Tweet Posting: Users can post tweets with text and media.
  • Timeline Generation: Generate personalized timelines for users.
  • Social Features: Follow/unfollow users and see their tweets.
  • Real-time Updates: Show new tweets in real-time.

Out of Scope

  • User authentication and accounts
  • Tweet search and discovery
  • Direct messaging
  • Tweet analytics and engagement
  • Mobile app specific features

Non-Functional Requirements

Core (Interview Focussed)

  • Low latency: Sub-second response time for timeline requests.
  • High availability: 99.9% uptime for tweet posting and viewing.
  • Scalability: Handle millions of tweets and users.
  • Consistency: Ensure timeline consistency across users.

Out of Scope

  • Data retention policies
  • Compliance and privacy regulations

💡 Interview Tip: Focus on low latency, high availability, and scalability. Interviewers care most about timeline generation, fan-out patterns, and real-time updates.


Core Entities

EntityKey AttributesNotes
Tweettweet_id, user_id, content, created_at, media_urlsIndexed by user_id for user tweets
Useruser_id, username, email, follower_count, following_countUser account information
Followfollow_id, follower_id, following_id, created_atSocial graph relationships
Timelinetimeline_id, user_id, tweet_ids, last_updatedPre-computed timelines
Likelike_id, tweet_id, user_id, created_atTweet engagement data

💡 Interview Tip: Focus on Tweet, User, and Follow as they drive timeline generation, social graphs, and content distribution.


Core APIs

Tweet Management

  • POST /tweets { content, media_urls } – Post a new tweet
  • GET /tweets/{tweet_id} – Get tweet details
  • DELETE /tweets/{tweet_id} – Delete a tweet
  • GET /tweets?user_id=&limit= – Get user's tweets

Timeline

  • GET /timeline – Get user's home timeline
  • GET /timeline/user/{user_id} – Get user's profile timeline
  • GET /timeline/mentions – Get tweets mentioning user
  • POST /timeline/refresh – Refresh timeline

Social Features

  • POST /users/{user_id}/follow – Follow a user
  • DELETE /users/{user_id}/follow – Unfollow a user
  • GET /users/{user_id}/followers – Get user's followers
  • GET /users/{user_id}/following – Get users being followed

Real-time Updates

  • GET /stream/timeline – WebSocket stream for timeline updates
  • GET /stream/mentions – WebSocket stream for mentions
  • POST /stream/subscribe { stream_type } – Subscribe to stream
  • POST /stream/unsubscribe { stream_type } – Unsubscribe from stream

High-Level Design

System Architecture Diagram

Key Components

  • Tweet Service: Handle tweet CRUD operations
  • Timeline Service: Generate and serve timelines
  • Social Service: Manage follow relationships and social graphs
  • Fan-out Service: Distribute tweets to followers
  • Real-time Service: Handle WebSocket connections and real-time updates
  • Database: Persistent storage for tweets, users, and relationships

Mapping Core Functional Requirements to Components

Functional RequirementResponsible ComponentsKey Considerations
Tweet PostingTweet Service, Fan-out ServiceTweet storage, fan-out distribution
Timeline GenerationTimeline Service, Fan-out ServiceTimeline computation, caching
Social FeaturesSocial Service, DatabaseFollow relationships, social graphs
Real-time UpdatesReal-time Service, Fan-out ServiceWebSocket connections, update broadcasting

Detailed Design

Fan-out Service

Purpose: Distribute tweets to followers using fan-out patterns.

Key Design Decisions:

  • Fan-out Strategy: Use push model for active users, pull model for inactive users
  • Batch Processing: Process fan-out operations in batches
  • Error Handling: Handle fan-out failures gracefully
  • Performance Optimization: Optimize fan-out for high-volume users

Algorithm: Fan-out distribution

1. Receive new tweet from user
2. Get user's follower list
3. For each follower:
   - Check follower's activity level
   - If active user:
     - Add tweet to follower's timeline
     - Update timeline cache
   - If inactive user:
     - Skip immediate fan-out
     - Use pull model on next access
4. Handle fan-out errors:
   - Retry failed operations
   - Log error details
   - Continue processing
5. Update fan-out statistics

Timeline Service

Purpose: Generate and serve personalized timelines for users.

Key Design Decisions:

  • Timeline Computation: Pre-compute timelines for active users
  • Caching Strategy: Cache timelines for fast access
  • Timeline Merging: Merge tweets from multiple sources
  • Personalization: Personalize timelines based on user preferences

Algorithm: Timeline generation

1. Receive timeline request
2. Check cache for existing timeline
3. If not cached:
   - Get user's following list
   - Fetch recent tweets from followed users
   - Merge tweets by timestamp
   - Apply personalization:
     - User's interests
     - Tweet engagement
     - Recency factor
   - Cache generated timeline
4. Return timeline to user
5. Update timeline statistics

Real-time Service

Purpose: Handle WebSocket connections and broadcast real-time updates.

Key Design Decisions:

  • WebSocket Connections: Maintain persistent connections for real-time updates
  • Message Broadcasting: Broadcast updates to relevant users
  • Connection Management: Handle connection drops and reconnections
  • Update Filtering: Send relevant updates to each user

Algorithm: Real-time update broadcasting

1. User connects to timeline stream
2. Send current timeline to user
3. When new tweet arrives:
   - Check if user follows tweet author
   - If follows:
     - Add tweet to user's timeline
     - Broadcast update to user
   - If mentions user:
     - Send mention notification
     - Update mentions timeline
4. Handle connection drops gracefully
5. Reconnect users with missed updates

Database Design

Tweets Table

FieldTypeDescription
tweet_idVARCHAR(36)Primary key
user_idVARCHAR(36)Tweet author
contentTEXTTweet content
media_urlsJSONMedia attachments
created_atTIMESTAMPCreation timestamp

Indexes:

  • idx_user_created on (user_id, created_at) - User tweets
  • idx_created_at on (created_at) - Recent tweets

Users Table

FieldTypeDescription
user_idVARCHAR(36)Primary key
usernameVARCHAR(100)Username
emailVARCHAR(255)Email address
follower_countINTNumber of followers
following_countINTNumber following
created_atTIMESTAMPAccount creation

Indexes:

  • idx_username on (username) - Username lookup
  • idx_follower_count on (follower_count) - Popular users

Follows Table

FieldTypeDescription
follow_idVARCHAR(36)Primary key
follower_idVARCHAR(36)Follower user
following_idVARCHAR(36)Following user
created_atTIMESTAMPFollow timestamp

Indexes:

  • idx_follower on (follower_id) - User's following
  • idx_following on (following_id) - User's followers
  • unique_follow on (follower_id, following_id) - Prevent duplicate follows

Timelines Table

FieldTypeDescription
timeline_idVARCHAR(36)Primary key
user_idVARCHAR(36)Timeline owner
tweet_idsJSONTweet IDs in timeline
last_updatedTIMESTAMPLast update

Indexes:

  • idx_user_id on (user_id) - User timelines
  • idx_last_updated on (last_updated) - Recent timelines

Scalability Considerations

Horizontal Scaling

  • Tweet Service: Scale horizontally with load balancers
  • Timeline Service: Use consistent hashing for timeline partitioning
  • Fan-out Service: Scale fan-out processing with distributed systems
  • Database: Shard tweets and users by user_id

Caching Strategy

  • Redis: Cache timelines and recent tweets
  • CDN: Cache static content and media
  • Application Cache: Cache frequently accessed data

Performance Optimization

  • Connection Pooling: Efficient database connections
  • Batch Processing: Batch fan-out operations for efficiency
  • Async Processing: Non-blocking tweet processing
  • Resource Monitoring: Monitor CPU, memory, and network usage

Monitoring and Observability

Key Metrics

  • Tweet Latency: Average tweet posting time
  • Timeline Latency: Average timeline generation time
  • Fan-out Rate: Tweets distributed per second
  • System Health: CPU, memory, and disk usage

Alerting

  • High Latency: Alert when tweet or timeline time exceeds threshold
  • Fan-out Failures: Alert when fan-out processing fails
  • Connection Drops: Alert when WebSocket connections drop frequently
  • System Errors: Alert on tweet processing failures

Trade-offs and Considerations

Consistency vs. Availability

  • Choice: Eventual consistency for timelines, strong consistency for tweets
  • Reasoning: Timelines can tolerate slight delays, tweets need immediate accuracy

Latency vs. Throughput

  • Choice: Optimize for latency with timeline caching
  • Reasoning: Real-time social media requires fast timeline updates

Storage vs. Performance

  • Choice: Use timeline pre-computation for better performance
  • Reasoning: Balance between storage costs and query performance

Common Interview Questions

Q: How would you handle high-volume users?

A: Use fan-out optimization, timeline pre-computation, and caching to handle high-volume users efficiently.

Q: How do you ensure timeline consistency?

A: Use fan-out patterns, timeline caching, and real-time updates to ensure timeline consistency.

Q: How would you scale this system globally?

A: Deploy regional tweet servers, use geo-distributed databases, and implement data replication strategies.

Q: How do you handle real-time updates at scale?

A: Use WebSocket connections, message broadcasting, and efficient fan-out patterns to handle real-time updates at scale.


Key Takeaways

  1. Fan-out Patterns: Essential for distributing tweets to followers efficiently
  2. Timeline Generation: Pre-computation and caching provide fast timeline access
  3. Real-time Updates: WebSocket connections and message broadcasting enable real-time social media
  4. Scalability: Horizontal scaling and partitioning are crucial for handling large-scale social media
  5. Monitoring: Comprehensive monitoring ensures system reliability and performance