> For the complete documentation index, see [llms.txt](https://liuzhenglaichn.gitbook.io/system-design/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuzhenglaichn.gitbook.io/system-design/news-feed/twitter-news-feed-timeline.md).

# Twitter News Feed (Timeline)

* Home timeline: fan-out write
* Search: fan-out read

## Home Timeline

![](/files/-M5Itgz16Q8Qpn7QDnIB)

Each write is fanned out 3 times for redundancy purpose.

We query the Social Graph Service to fan out the write to all the followers.

![](/files/-M5IuN5gZRA2SrjVVCBu)

![](/files/-M5IsrTwbgcdYxLna13K)

Every write is consist of:

1. tweet ID: the sent tweet
2. user ID: the sender's ID
3. bits: miscellaneous product related bits.

![](/files/-M5IvCyqZPzjx_S_ba2P)

max limit: 800 tweets in your timeline

If you are not a active user (logged in in the last 30 days), we don't fan out write into your timeline.

![](/files/-M5IvlAPNeCEjuwOBMzD)

If your cache is empty, we will reconstruct your timeline.

## Search

![](/files/-M5IwhvcD8fl-wkXBUQE)

Earlybird is a modified version of Lucene.

Twitter mainly rank the contents based on:

1. Re-tweet
2. Like
3. Reply

![](/files/-M5IxU7m1C5caY-_WpFi)

The home timeline requires O(n) write and O(1) read

The search timeline requires O(1) write and O(n) read

![](/files/-M5Ixcs3VFE9eDOHP94F)

## Improve fanout write

Race condition:

1. Lady gaga sends out tweet
2. I retweet or reply
3. My fanout write is done before Lady gaga's is done.

400 million tweets a day, 4600 tweets a second

Merge home timeline and search timeline.

![](/files/-M5IziVj_zyaRu_jYFUE)

![](/files/-M5J-UQd1ZtSy2yq2JCF)

Don't fanout write for users with large number of followers. Only do fan out for users with small number of followers.

![](/files/-M5J-eF6943t07Q42Xp7)

![](/files/-M5J-y8upoU027LR6jE-)

![](/files/-M5J-sO53ATe10tSnfP1)

## Stats

![](/files/-M5J0QxB5Ko7BcPSyuAn)

![](/files/-M5J0T8jn0AtOJd3d9lj)

![](/files/-M5J0VC0nJmlJmB5JuoB)

## Reference

1. Timelines at Scale (APR 03, 2013): <https://www.infoq.com/presentations/Twitter-Timeline-Scalability/>
