Timelines at Scale
How Twitter built a scalable timeline service — from fanout on write vs read, to the hybrid approach Raffi Krikorian led that powers hundreds of millions of users.
TimeLine at scale - 2012 - Raffi Krikorian
Alice Follows bob Charlie David Emma
Bob: 9:00 "Good morning" Charlie: 9:02 "Having coffee" Emma: 9:03 "Working on AI" David: 9:05 "Watching football"
9:05 David 9:03 Emma 9:02 Charlie 9:00 Bob
Simple Logic select * from firends where id = ALICE_ID -- [1 , 6 , 7 , 9] select * from tweets where id in (list) order by id/timestamp order by desc select * from tweets where id in (list) order by id/timestamp limit 10 offset 1 order by desc. -----> PAGINATION
I follow 3000 People
I follow 20000 People
+ people are refressing + people are scrlling down =---------------------------------- it is massive load
User Experience: I want timeline to appear in 100ms but. because of massive load it is taking 5 seconds
1. Fan Out on Read
Simple Logic select * from firends where id = ALICE_ID select * from tweets where id in (list) order by id/timestamp select * from tweets where id in (list) order by id/timestamp limit 10 offset 1. -----> PAGINATION
will be a problem if there are 30000 following
fan out service
2. Fan Out on write
Alice - [<tweet1> , <tweet2>,<tweet3>] BoB - [<tweet1> , <tweet2>,<tweet3>] David - [<tweet1> , <tweet2>,<tweet3>]
FIFO - with capacity of 1k
Scale At the time of the talk, Twitter reported roughly: 150M+ active users 300K timeline queries/sec 400M tweets/day ~30B timeline deliveries/day
Production logic PUSH + PULL APPROACH ---- (FAN OUT ON READ + FAN OUT OF WRITE)
LRU - with capacity of 1k
tweet service
1. how many celeberity he follows 2. let say 20 3. so REDIS tweets + 20 Celebreity post [lets say 2 posts] 4. merge 5. return
Celebrity problem
is_celebrity = false
messi_id - [twwt1 , twwet1 ,] ts_id - [tweet3 , tweet3] mod_id - [tweet7,, tweet9]