Fan-Out on Write
Fan-out on write is a strategy that copies each new post into every follower's feed at post time. This lesson deep-dives the push model, the first of the two feed strategies.
One post lands in every follower's feed
Ana posts once, and the post is copied at once. The printout shows the copy sitting in each follower's feed.
followers = {"ana": ["ben", "cy"], "ben": ["cy"], "cy": ["ana"]}
feeds = {"ana": [], "ben": [], "cy": []}
writes = 0
for follower in followers["ana"]:
feeds[follower].append("ana: sunny day")
writes += 1
for user in sorted(feeds):
print(user, "feed:", feeds[user])
print("feed writes:", writes)More posts, more copies
Each new post adds its own copies. The printout shows the feeds after three posts and the total writes.
followers = {"ana": ["ben", "cy"], "ben": ["cy"], "cy": ["ana"]}
feeds = {"ana": [], "ben": [], "cy": []}
posts = [("ana", "ana: sunny day"), ("ben", "ben: game tonight"), ("cy", "cy: new recipe")]
writes = 0
for author, text in posts:
for follower in followers[author]:
feeds[follower].append(text)
writes += 1
for user in sorted(feeds):
print(user, "feed:", feeds[user])
print("feed writes:", writes)Reading a ready feed
Cy opens the feed, and the stored list is already there. The printout shows the feed newest first and the entries read.
followers = {"ana": ["ben", "cy"], "ben": ["cy"], "cy": ["ana"]}
feeds = {"ana": [], "ben": [], "cy": []}
posts = [("ana", "ana: sunny day"), ("ben", "ben: game tonight")]
for author, text in posts:
for follower in followers[author]:
feeds[follower].append(text)
feed = feeds["cy"]
for text in reversed(feed):
print(text)
print("entries read:", len(feed))Example
followers = {"ana": ["ben", "cy"], "ben": ["cy"], "cy": ["ana"]}
feeds = {"ana": [], "ben": [], "cy": []}
posts = [("ana", "ana: sunny day"), ("cy", "cy: new recipe"), ("ana", "ana: new photo"), ("ben", "ben: game tonight")]
writes = 0
for author, text in posts:
for follower in followers[author]:
feeds[follower].append(text)
writes += 1
for user in sorted(feeds):
print(user, "feed:", feeds[user])
print("feed writes:", writes)
print("ben reads:", list(reversed(feeds["ben"])))Key points
- Fan-out on write: a new post is copied into the feed of every follower, at the moment it is posted.
- Push model: the other name for this strategy, because each post is pushed out to feeds.
- Feed list: each user owns a stored list that holds the copies made for that user.
- Posting cost: one post costs one feed write for every follower of its author.
- Reading gain: opening a feed reads a ready list, so a read does almost no work.
- Wasted copies: a copy is written even for a follower who never opens the feed.
In this example, four posts are pushed into followers' feeds as they arrive. The total write count grows with each post, while the final read only takes a ready list.
Note
this strategy trades heavy posting work for very cheap reads.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Under fan-out on write, when is a follower's feed built?