Skip to content
BytePatterns

Feed Fan-Out Cost

MediumSystem Design Cases#fan-out#cost-model~25m

Problem

A news feed can be built three ways, and each costs a different number of storage operations per day. With push, every post is written into each follower's feed, and opening a feed is 1 read. With pull, nothing is fanned out, and opening a feed reads from every account the user follows. The hybrid pushes posts from ordinary accounts but pulls posts from celebrities, accounts with at least threshold followers, so opening a feed costs 1 read plus 1 per celebrity followed. Given the follow edges as (follower, followee), posts per day per author and feed opens per day per user, return each model's daily operations (fan-out writes plus reads) and the cheapest one, preferring push, then pull, then hybrid on a tie.

Examples

Input:  4 fans follow "star" and each other; star posts 20 a day, the others 1;
        each fan opens the feed 10 times a day; threshold = 4
Output: ({'push': 132, 'pull': 160, 'hybrid': 92}, 'hybrid')
Why:    pushing star's 20 posts to 4 feeds is the expensive part, and the hybrid skips exactly that
Input:  the same graph and posts, but only ann opens the feed, once a day
Output: ({'push': 93, 'pull': 4, 'hybrid': 14}, 'pull')
Why:    when feeds are rarely read, writing posts into them ahead of time is wasted work
Input:  follows = [("ann", "bob")], posts = {"bob": 50}, opens = {"ann": 1}, threshold = 100
Output: ({'push': 51, 'pull': 1, 'hybrid': 51}, 'pull')
Why:    edge case, with no celebrity the hybrid is just push

Hints

0 / 3

Stuck on the idea rather than the code? Design a News Feed covers it.