MODULO System Design: Comprehensive Architecture MasterclassLAB

Designing Twitter: A Scalable Architecture

Designing Twitter: A Scalable Architecture

=====================================================

Designing a scalable architecture for Twitter requires a deep understanding of system design principles, distributed systems, and database design. In this masterclass, we will delve into the technical details of designing a Twitter-like system that can handle millions of users and tweets.

System Requirements


Before we dive into the design, let's outline the system requirements:

  • Handle 100 million users
  • Support 500 million tweets per day
  • Provide a 99.99% uptime
  • Ensure low latency (< 200ms) for tweet posting and retrieval
  • Support searching, trending topics, and hashtags

System Components


The Twitter system can be broken down into the following components:

1. User Service

  • Responsible for user authentication, registration, and profile management
  • Stores user data in a relational database (e.g., MySQL)

2. Tweet Service

  • Handles tweet posting, deletion, and retrieval
  • Stores tweets in a distributed NoSQL database (e.g., Apache Cassandra)

3. Timeline Service

  • Generates user timelines by aggregating tweets from followed users
  • Uses an in-memory data store (e.g., Redis) for caching

4. Search Service

  • Supports searching for tweets, users, and hashtags
  • Utilizes a search engine (e.g., Apache Solr)

5. Trending Topics Service

  • Calculates trending topics and hashtags
  • Uses a graph database (e.g., Apache Giraph)

Database Design


1. User Database

The user database stores user information, such as username, password, email, and profile data. We can use a relational database like MySQL for this purpose.

User Table Schema:

| Column Name | Data Type | Description | | --- | --- | --- | | id | int | Unique user ID | | username | varchar(255) | Username chosen by the user | | password | varchar(255) | Hashed user password | | email | varchar(255) | User email address | | profile_data | text | User profile data (e.g., bio, location) |

2. Tweet Database

The tweet database stores tweet metadata, such as tweet ID, user ID, text, and timestamp. We can use a distributed NoSQL database like Apache Cassandra for this purpose.

Tweet Table Schema:

| Column Name | Data Type | Description | | --- | --- | --- | | id | uuid | Unique tweet ID | | user_id | int | ID of the user who posted the tweet | | text | text | Tweet text | | timestamp | timestamp | Timestamp when the tweet was posted |

3. Timeline Database

The timeline database stores user timelines, which are aggregated from the tweets of followed users. We can use an in-memory data store like Redis for this purpose.

Timeline Data Structure:

| Key | Value | | --- | --- | | user_id | List of tweet IDs |

4. Search Database

The search database stores tweet metadata and text for searching purposes. We can use a search engine like Apache Solr for this purpose.

Search Index Schema:

| Field Name | Data Type | Description | | --- | --- | --- | | id | int | Unique tweet ID | | text | text | Tweet text | | user_id | int | ID of the user who posted the tweet | | timestamp | timestamp | Timestamp when the tweet was posted |

5. Trending Topics Database

The trending topics database stores graph data representing the relationships between tweets, users, and hashtags. We can use a graph database like Apache Giraph for this purpose.

Graph Data Structure:

| Vertex | Edge | | --- | --- | | Tweet | Hashtag | | User | Follows | | Hashtag | Related |

System Architecture


The Twitter system architecture can be represented as follows:

  • Load Balancer: Distributes incoming traffic across multiple servers
  • Application Server: Handles user requests, communicates with services
  • User Service: Manages user authentication, registration, and profile data
  • Tweet Service: Handles tweet posting, deletion, and retrieval
  • Timeline Service: Generates user timelines by aggregating tweets
  • Search Service: Supports searching for tweets, users, and hashtags
  • Trending Topics Service: Calculates trending topics and hashtags
  • Database: Stores user, tweet, timeline, search, and trending topics data

Scalability Strategies


To ensure scalability, we can employ the following strategies:

  • Horizontal Scaling: Add more servers to handle increased traffic
  • Vertical Scaling: Increase server resources (e.g., CPU, RAM) to handle increased load
  • Caching: Store frequently accessed data in memory to reduce database queries
  • Database Sharding: Split large databases into smaller, more manageable pieces
  • Load Balancing: Distribute traffic across multiple servers to prevent bottlenecks

Conclusion


Designing a scalable architecture for Twitter requires careful consideration of system components, database design, and scalability strategies. By employing a distributed system architecture, using a combination of relational and NoSQL databases, and implementing caching and load balancing, we can build a system that can handle millions of users and tweets.

⚔️ YOUR MISSION

Design a system to handle Twitter's trending topics feature, including hashtag calculation, ranking, and display.

Design response
Read the exercise prompt
SELECT 
  t.hashtag, 
  COUNT(t.id) as tweet_count, 
  SUM(t.engagement) as engagement_score
FROM 
  tweets t
WHERE 
  t.timestamp > NOW() - INTERVAL 1 DAY
GROUP BY 
  t.hashtag
ORDER BY 
  engagement_score DESC
LIMIT 10;
WAITING FOR SIGNAL...
BACKNEXT STAGE