Designing Twitter

1 billion tweets a day · Part of Interview cases

Key idea

Practice

8 questions · about 4 min

Explained step by step

Functional requirement

Functional: post tweets (text, image or video), follow other users, a feed with tweets from people you follow, and search tweets. Non-functional: high availability with minimal latency, and it must scale. Extended: metrics, retweets and favorites. It's like a neighborhood bulletin board: everyone pins notes, you choose whose to read, and you can look up an old note.

Estimation

With 1 billion users, 200 million active per day and 5 tweets each, that's 1 billion tweets per day: about 12,000 per second. If 10% carry media, that's 100 million files per day. At 100 bytes per tweet and 50 KB per file: about 100 GB plus 5 TB per day, 5.1 TB in total. Over 10 years, about 19 PB, with about 60 MB per second of bandwidth. It's like counting how many newspapers a country prints in 1 day.

Newsfeed

To generate user A's feed: fetch the IDs of all users and topics A follows; fetch each one's tweets; sort them with a ranking algorithm by relevance, time and engagement; and return them paginated. It's a heavy process, especially if A follows 2,000 accounts, so the feed is pre-generated, stored in cache and updated every so often. It's like a newsagent building your personal paper: gathering the sections you like and sorting them before you wake up.

Fan-out on write

Publishing the feed has 3 models. Pull (fan-out on load): the feed is built when the follower asks for it; it saves writes but adds reads and doesn't show new posts until reload. Push (fan-out on write): each tweet is pushed to every follower's feed as soon as it's published; it saves reads but adds writes. Hybrid: push for accounts with few followers and pull for celebrities. It's like announcing a party: you call 10 friends one by one; for 1 million fans, better a poster everyone looks at.

Ranking algorithm

A ranking algorithm sorts each post by how relevant it is to each user. Facebook used EdgeRank: Rank = Affinity × Weight × Decay. Affinity: how close the user is to the poster, based on how much they like or message them. Weight: each interaction counts differently, a comment more than a like. Decay: the older the post, the less it counts. Today machine learning models with thousands of factors are used. It's like a newsagent putting on top the news from your friends, the most commented and the newest.

Data model

A retweet can be stored as a new tweet, with the retweeter's ID, the type «tweet» instead of text or video, and the original tweet's ID as content. It's the simplest way; to improve it, a separate retweets table can be used. That way, 1 tweet with 10,000 retweets isn't copied 10,000 times, it's just pointed to. It's like forwarding a link instead of copying the whole article.

Elasticsearch

To search huge volumes in near real time, a traditional database isn't enough. Elasticsearch is a distributed, free and open search and analytics engine, built on Apache Lucene, that answers in milliseconds. For trending topics, the most frequent searches, hashtags and topics of the last N seconds are cached and updated every M seconds with a batch process. It's like the index at the back of a book: you find the word without reading all 500 pages.

Social graph

To suggest mutual friends you build a social graph: each node is a user and each directed arrow is a «follows». Walking a user's followers finds mutual friends to suggest. It needs a graph database like Neo4j. To improve suggestions, a machine learning recommendation model is added. For metrics, events go through Kafka and are analyzed with Apache Spark. It's like a family tree: following the branches you find cousins you didn't know.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

1 billion tweets a day