Hide sidebar

Design Twitter

Design Twitter
Easy
Design a simplified version of Twitter. The service should allow users to post tweets, follow other users, and view a timeline of tweets from the users they follow. The system must be highly available and scalable to handle millions of users and tweets per day.

Variants:

Facebook News FeedInstagram FeedLinkedIn Feed
Loading diagram...

Twitter System Design Requirements

Functional Requirements

1

Post Tweets

Users should be able to post tweets with a maximum of 280 characters.

2

Follow Users

Users should be able to follow other users to see their tweets.

3

View Timeline

Users should be able to view a timeline of tweets from the users they follow.

Non-Functional Requirements

1

High Availability

The system must be highly available, with minimal downtime.

2

Low Latency

The timeline should load in under 200ms.

3

Scalability

The system must be able to handle millions of users and tweets per day.

4

Durability

Once a tweet is posted, it should not be lost.

CAP Theorem Trade-offs

ConsistencyAvailabilityPartition Tolerance
Trade-off Explanation:

For a social media platform like Twitter, we prioritize Availability and Partition Tolerance (AP). It's more important for users to be able to post and view tweets than to have strict consistency. Eventual consistency is acceptable, as a slight delay in a new tweet appearing on a follower's timeline is not a critical issue.

Scale Estimates

User Base500M DAU (5 * 10^8)
Base assumption for system sizing
Tweets/Day500M tweets (5 * 10^8)
The number of new tweets our system must handle daily.
Timeline Reads/Second600K reads
Data per Tweet1KB
Daily Data Growth500GB
Yearly Data Growth182.5TB

API Design

The API for our Twitter-like service will focus on the core functionalities: posting tweets, following users, and retrieving the user's timeline. Your interviewer will expect you to define clear and concise endpoints for these operations.

The POST /api/v1/tweets endpoint will allow users to create a new tweet. The POST /api/v1/follow endpoint will allow a user to follow another user. The GET /api/v1/timeline endpoint will be a read-heavy endpoint that retrieves the user's personalized timeline.

POST/api/v1/tweets

Post a new tweet

POST/api/v1/follow

Follow another user

GET/api/v1/timeline

Get the user's timeline

Database Schema

The database schema for a Twitter-like service needs to efficiently store users, tweets, and the social graph (follows). Your interviewer will be interested in how you model these relationships to ensure fast reads and writes.

We'll use three main tables: users, tweets, and follows. The users table will store user profile information. The tweets table will store all the tweets, and the follows table will represent the social graph, with a composite primary key of follower_id and followee_id.

users

user_id
stringPartition Key
username
string
display_name
string
created_at
timestamp

tweets

tweet_id
stringPartition Key
user_id
string
content
string
created_at
timestamp

follows

follower_id
stringPartition Key
followee_id
stringSort Key
created_at
timestamp

High-Level Architecture

At a high level, our Twitter-like service will be composed of several microservices, each responsible for a specific part of the system. This approach allows for independent scaling and development of each component. Your interviewer will expect you to be able to break down the system into these logical components.

Core Services

  • Tweet Service: This service is responsible for handling all tweet-related operations. It will have endpoints for creating, retrieving, and deleting tweets. This service will be write-heavy, as users are constantly posting new tweets.

  • Timeline Service: This service is responsible for generating the user's timeline. This is a read-heavy service that needs to be highly optimized for performance. It will query the follows table to get the list of users that the current user follows, and then retrieve the latest tweets from those users.

  • User Service: This service is responsible for managing user accounts and the social graph. It will have endpoints for creating users, retrieving user profiles, and managing follows.

Deep Dive: Timeline Generation

Generating the user's timeline is one of the most critical and challenging parts of a Twitter-like system. Your interviewer will want to know how you would approach this problem, as it involves a trade-off between real-time updates and system performance.

1

Fan-out on Write (Push Model)

Why: In a fan-out on write approach, we pre-compute the user's timeline whenever a new tweet is posted. This makes timeline retrieval extremely fast, as we are simply reading from a pre-computed list.

How it works: When a user posts a tweet, we retrieve the list of their followers and insert the new tweet into the timeline of each follower. This is typically done using a message queue to handle the fan-out process asynchronously. The user's timeline can be stored in a key-value store like Redis, with the user_id as the key and a list of tweet_ids as the value.

Trade-offs:

  • Pros: Fast timeline reads, which is the most common operation.
  • Cons: Can be slow for users with a large number of followers (the "celebrity problem"). Wastes resources if many followers are inactive.
2

Fan-out on Read (Pull Model)

Why: In a fan-out on read approach, we generate the user's timeline on demand when they request it. This is simpler to implement than the fan-out on write approach and avoids the "celebrity problem."

How it works: When a user requests their timeline, we first retrieve the list of users they follow. Then, we query the tweets table to get the latest tweets from those users, sort them by created_at, and return the result.

Trade-offs:

  • Pros: Simpler to implement, no "celebrity problem."
  • Cons: Slower timeline reads, as we have to perform multiple queries and a sort on every request.

Hybrid Approach

A hybrid approach is often the best solution for a system like Twitter. We can use a fan-out on write approach for most users, and a fan-out on read approach for celebrities with a large number of followers. This gives us the best of both worlds: fast timeline reads for the majority of users, and a scalable solution for celebrities.

Deep Dive: Scaling the Database

Your interviewer will expect you to discuss how you would scale the database to handle the massive amount of data and traffic that a Twitter-like service would generate.

1

Database Sharding

Why: Sharding is a technique for horizontally partitioning a database. It allows us to distribute the data and query load across multiple servers, which is essential for a system at this scale.

How it works: We can shard our database by user_id. This means that all the data for a particular user (their profile, tweets, and follows) will be stored on the same shard. This makes it easy to retrieve all the data for a user in a single query.

Trade-offs:

  • Pros: Highly scalable, allows for even distribution of data and load.
  • Cons: Can be complex to implement and maintain. Requires a sharding key that will evenly distribute the data.

Complete Design

Now that we've covered all the major components individually, let's look at how everything fits together in our complete Twitter system design. This diagram shows the end-to-end flow from a user posting a tweet to it appearing in their followers' timelines.

Loading diagram...

The complete architecture demonstrates how the Tweet Service, Timeline Service, and User Service work together to provide a scalable and reliable Twitter-like experience. The use of a hybrid timeline generation model and database sharding ensures that the system can handle millions of users and tweets per day.