Low Level Design for Twitter System Design

A low-level design of Twitter dives into the details of individual components and functionalities. Here’s a breakdown of some key aspects:

5.1 Data storage:

  • User accounts: Store user data like username, email, password (hashed), profile picture, bio, etc. in a relational database like MySQL.
  • Tweets: Store tweets in a separate table within the same database, including tweet content, author ID, timestamp, hashtags, mentions, retweets, replies, etc.
  • Follow relationships: Use a separate table to map followers and followees, allowing efficient retrieval of user feeds.
  • Additional data: Store media assets like images or videos in a dedicated storage system like S3 and reference them in the tweet table.

5.2 Core functionalities:

  • Posting a tweet:
    • User submits tweet content through the UI.
    • Server-side validation checks tweet length, media size, and other constraints.
    • Tweet data is stored in the database with relevant associations (hashtags, mentions, replies).
    • Real-time notifications are sent to followers and mentioned users.
  • Timeline generation:
    • Retrieve a list of users and hashtags the current user follows.
    • Fetch recent tweets from those users and matching hashtags from the database.
    • Apply algorithms to rank tweets based on relevance, recency, engagement, etc.
    • Cache frequently accessed timelines in Redis for faster delivery.
  • Search:
    • User enters keywords or hashtags.
    • Server-side search analyzes tweet content, hashtags, and user metadata.
    • Return relevant tweets based on a ranking algorithm.
  • Follow/unfollow:
    • Update the follow relationships table accordingly.
    • Trigger relevant notifications and adjust user timelines dynamically.

5.3 Additional considerations:

  • Caching: Use caching mechanisms like Redis to reduce database load for frequently accessed data like user timelines and trending topics.
  • Load balancing: Distribute workload across multiple servers to handle high traffic and ensure scalability.
  • Database replication: Ensure data redundancy and fault tolerance with database replication techniques.
  • Messaging queues: Leverage asynchronous messaging queues for processing tasks like sending notifications or background indexing.
  • API design: Develop well-defined APIs for internal communication between different components within the system.

Designing Twitter – A System Design Interview Question

Designing Twitter (or Facebook feed or Facebook search..) is a quite common question that interviewers ask candidates. A lot of candidates get afraid of this round more than the coding round because they don’t get an idea of what topics and tradeoffs they should cover within this limited timeframe.

Important Topics for Designing Twitter

  • How Would You Design Twitter?
  • Requirements for Twitter System Design
  • Capacity Estimation for Twitter System Design
  • Use Case Design for Twitter System Design
  • Low Level Design for Twitter System Design
  • High Level Design for Twitter System Design
  • Data Model Design for Twitter System Design
  • API Design for Twitter System Design
  • Microservices Used for Twitter System Design
  • Scalability for Twitter System Design

Similar Reads

1. How Would You Design Twitter?

Don’t jump into the technical details immediately when you are asked this question in your interviews. Do not run in one direction, it will just create confusion between you and the interviewer. Most of the candidates make mistakes here and immediately they start listing out some bunch of tools or frameworks like MongoDB, Bootstrap, MapReduce, etc....

2. Requirements for Twitter System Design

2.1 Functional Requirements:...

3. Capacity Estimation for Twitter System Design

To estimate the system’s capacity, we need to analyze the expected daily click rate....

4. Use Case Design for Twitter System Design

...

5. Low Level Design for Twitter System Design

A low-level design of Twitter dives into the details of individual components and functionalities. Here’s a breakdown of some key aspects:...

6. High Level Design for Twitter System Design

We will discuss about high level design for twitter,...

7. Data Model Design for Twitter System Design

This is the general Dara model which reflects our requirements....

8. API Design for Twitter System Design

A basic API design for our services:...

9. Microservices Used for Twitter System Design

9.1 Data Partitioning...

10. Scalability for Twitter System Design

Let us identify and resolve Scalability such as single points of failure in our design:...

11. Conclusion

Twitter handles thousands of tweets per second so you can’t have just one big system or table to handle all the data so it should be handled through a distributed approach. Twitter uses the strategy of scatter and gather where it set up multiple servers or data centers that allow indexing. When Twitter gets a query (let’s say #geeksforgeeks) it sends the query to all the servers or data centers and it queries every Early Bird shard. All the early bird that matches with the query return the result. The results are returned, sorted, merged, and reranked. The ranking is done based on the number of retweets, replies, and the popularity of the tweets....