# Twitter News Feed (Timeline)

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

## Home Timeline

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IofXwo0CdSizHPATT%2F-M5Itgz16Q8Qpn7QDnIB%2Fimage.png?alt=media\&token=31a1cbed-b8c4-4358-9ef6-b8b564898ca1)

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.

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IofXwo0CdSizHPATT%2F-M5IuN5gZRA2SrjVVCBu%2Fimage.png?alt=media\&token=07b0720f-ff52-4b8a-908e-876c00ca6045)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IofXwo0CdSizHPATT%2F-M5IsrTwbgcdYxLna13K%2Fimage.png?alt=media\&token=6168c5d8-2213-427b-b416-4d7c3555ae49)

Every write is consist of:

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

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IuvvEBb8GAxa4-Btr%2F-M5IvCyqZPzjx_S_ba2P%2Fimage.png?alt=media\&token=d0cb63ed-0591-44f2-ba0c-e6cbcc0e1c25)

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.

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IviNhowXf-UR6Qf7R%2F-M5IvlAPNeCEjuwOBMzD%2Fimage.png?alt=media\&token=3cf4bc3b-e546-45b4-8598-33d33213f355)

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

## Search

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5Iw_J_LnpUvKwyZ2Sf%2F-M5IwhvcD8fl-wkXBUQE%2Fimage.png?alt=media\&token=6927f2ef-b6e2-4ddd-a576-bba35c262999)

Earlybird is a modified version of Lucene.

Twitter mainly rank the contents based on:

1. Re-tweet
2. Like
3. Reply

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5Iwq_9lj55U7TiAQ8G%2F-M5IxU7m1C5caY-_WpFi%2Fimage.png?alt=media\&token=aabc87e2-bf11-4fe3-afb6-e9e0d2a116fd)

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

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

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5Iwq_9lj55U7TiAQ8G%2F-M5Ixcs3VFE9eDOHP94F%2Fimage.png?alt=media\&token=5b2669ee-71b3-4692-9541-beee97107e6a)

## 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.

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5IziVj_zyaRu_jYFUE%2Fimage.png?alt=media\&token=7e8237be-a3ee-4c3c-b9ff-cadef65fce7f)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J-UQd1ZtSy2yq2JCF%2Fimage.png?alt=media\&token=5b97035a-d60e-4b83-96cf-c7871ec2fbd5)

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

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J-eF6943t07Q42Xp7%2Fimage.png?alt=media\&token=6a1eac57-602e-4b5e-96a1-b6b7f75860b8)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J-y8upoU027LR6jE-%2Fimage.png?alt=media\&token=62b78ae7-54de-40b5-9f5f-86591596079d)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J-sO53ATe10tSnfP1%2Fimage.png?alt=media\&token=37188704-7e33-4d1f-b4ac-25d2f84e6eb7)

## Stats

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J0QxB5Ko7BcPSyuAn%2Fimage.png?alt=media\&token=6ba6990c-7229-4536-934b-345892dd35a3)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J0T8jn0AtOJd3d9lj%2Fimage.png?alt=media\&token=9ef82ca9-c8a6-4847-afae-a037ea400fce)

![](https://1250864089-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4Bkp-b8HYQgJF1rkOc%2F-M5IzPSfOETj_cyPHeRR%2F-M5J0VC0nJmlJmB5JuoB%2Fimage.png?alt=media\&token=e2bd6f33-6bf2-44af-bba5-29203e68609b)

## Reference

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