Twitter System Design
- Users should be able to create tweets.
- Users should be able to search for tweets by query.
- Tweet content will be restricted to 280 characters of text.
- The system needs to be highly available.
- The system should be highly reliable. Tweets created by users should never be lost.
- The accepatble latency for search results is 200ms.
We have 500M new tweets each day. Given that the average tweet is 33 characters long, we can estimate a conservative average tweet size of 134 bytes. We calculated that by multiplying the character length by the character encoding's byte size per encoded character. We need to use the character encoding utf8mb4 to support the emoji's used in tweets. utf8mb4 uses up to 4 bytes to encode each character. Lastly we add two bytes to account of the overhead of storing text in mysql.
Character Length | Character Encoding | Average Tweet Size |
---|
33 | utf8mb4 | 134 |
Average Tweet Size | Total Tweets | Total Bytes | Per Year Storage |
---|
134 | 500M | 134_33_4 ~ 67 GBs | 67GB * 365 days = 24.445 TB |
Description | Value |
---|
Write | 600 per second. |
Read | 600,000 per second. |
Total number of Users | 330 million Monthly Active Users. |
Twitter is a Read Heavy System. So the system that you make should be Read Heavy, Eventual Consistency and Storage Efficient.
πRead Heavy: Something
πEventual Consistency: Something
πStorage Efficient: Something
Best choice for the database is Redis and along with it have to use some DB to store the data.
π Redis drives Timeline, Twitter's most important service. Timeline is an index of tweets indexed by an id. Chaining tweets together in a list produces the Home Timeline. The User Timeline, which consists of tweets the user has tweeted, is just another list.
β
User Table
Description | Value |
---|
User ID | Unique ID of the User. |
Followers | Number of Followers of the User. |
Tweets | Number of Tweets of the User. |
β
Tweet Table
Description | Value |
---|
Tweet ID | Unique ID of the Tweet. |
User ID | User ID of the User. |
Text | Text of the Tweet. |
Retweet Count | Number of Retweets of the Tweet. |
β
Follower Table
Description | Value |
---|
User ID | User ID of the User. |
Follower ID | User ID of the Follower. |
API | Description |
---|
/ | Get the Home Timeline of the User. |
/user/:user_id | Get the User Timeline of the User. |
/tweet/:tweet_id | Get the Tweet. |
/user/:user_id/followers | Get the Followers of the User. |
POST Requests.β
API | Description |
---|
/user/:user_id/follow | Follow the User. |
/user/:user_id/unfollow | Unfollow the User. |
/tweet | Post a Tweet. |
API | Description |
---|
/user/:user_id/unfollow | Unfollow the User. |
/tweet/:tweet_id | Delete the Tweet. |
API | Description |
---|
/login | Login the User. |
/logout | Logout the User. |
API | Description |
---|
/error | Get the Error. |
API | Description |
---|
/cache | Get the Cache. |
/cache/clear | Clear the Cache. |
/cache/clear/:key | Clear the Cache. |
/cache/put | Put the Cache. |
/cache/put/:key | Put the Cache. |
API | Description |
---|
/metrics | Get the Metrics. |
/metrics/clear | Clear the Metrics. |
/metrics/clear/:key | Clear the Metrics. |
/metrics/put | Put the Metrics. |
/metrics/put/:key | Put the Metrics. |