Part 5 of 8 · 2 min read
Fanout service
Five steps and a queue.
The book lists the steps of the fanout service.
- Fetch the friend ids from the graph database. A graph database stores relations such as friendships and is suited to friend lists and suggestions.
- Read friend details from the user cache and filter. A muted friend's posts must not appear. A post shared with only some friends must reach only those.
- Send the list of friends and the new post id to a message queue.
- Fanout workers take jobs from the queue and write to feed caches.
- Each cache entry is a pair of post id and user id, appended to the friend's feed list. The list has a configurable size limit.
The limit keeps memory small. Few people scroll through thousands of posts, so old entries can fall out. If a user does scroll past the limit, the service builds the rest from the database, and misses are rare.
The queue is the same idea as in Level 6. It lets the publisher return at once while workers finish the fanout in the background. If workers fall behind, the queue grows, and feeds are a little late. That is the soft guarantee in R4.
A worked example (made up). A post by a user with 600 friends. After filters, 540 remain. The queue holds one job. Workers split it into 540 cache writes of 8 bytes each, about 4 KB of new cache data.
Common mistake
Storing whole posts in every friend's feed cache. A post shared with 500 friends is then stored 500 times, and an edit must change all copies. Store the id once in the post cache, and keep only ids in the feeds.
Check yourself: 1. Why filter friends before fanout? 2. What does the queue buy? 3. What happens when a reader scrolls beyond the cached list?