🎓 A1Learner
🔍
0 of 42 lessons completed

News Feed: Ranking and Trade-Offs

Feed ranking is the ordering of a feed by a score for each post, instead of by time alone. This lesson adds ranking and then wraps up the trade-offs of the whole news feed design.

Newest first, with no score

Chronological order sorts posts only by age. The printout lists the posts from newest to oldest.

posts = [
    ("ana: morning run", 1, 3),
    ("ben: match report", 9, 15),
    ("cy: lunch photo", 4, 5),
]

for text, hours, likes in sorted(posts, key=lambda p: p[1]):
    print(text, "| hours old:", hours)

A score changes the order

Each post earns 10 minus its age in hours, plus its likes. The printout shows the ranked order and each score.

posts = [
    ("ana: morning run", 1, 3),
    ("ben: match report", 9, 15),
    ("cy: lunch photo", 4, 5),
]

scored = [((10 - hours) + likes, text) for text, hours, likes in posts]
for score, text in sorted(scored, reverse=True):
    print(text, "| score:", score)

The cost recap in numbers

The small graph from the earlier lessons posts once per account. The printout compares the work each strategy does.

followers = {"ana": 2, "ben": 1, "cy": 1}
posts_each = {"ana": 1, "ben": 1, "cy": 1}
posts = sum(posts_each.values())

examined = posts_each["ana"] + posts_each["ben"]
print("posts:", posts)
print("fan-out on write, feed writes:", sum(followers.values()))
print("fan-out on read, stored posts:", posts)
print("fan-out on read, one cy read examines:", examined)

Example

posts = [
    ("ana: morning run", 1, 3),
    ("ana: old photo", 6, 2),
    ("ben: match report", 9, 15),
    ("ben: hello all", 2, 0),
]

print("newest first:")
for text, hours, likes in sorted(posts, key=lambda p: p[1]):
    print(text)

print("ranked:")
scored = [((10 - hours) + likes, text) for text, hours, likes in posts]
for score, text in sorted(scored, reverse=True):
    print(text, "| score:", score)

Key points

  • Feed ranking: each post earns a score, and the feed shows the highest scores first.
  • Chronological order: posts are shown newest first, with no score at all.
  • Score: a number built from signals about a post, such as its age and its likes.
  • Recency: a signal that gives newer posts more score than older posts.
  • Likes: a signal that gives well-liked posts more score.
  • Demo rule: in this lesson's demos, a post earns 10 minus its age in hours, plus its likes.
  • Trade-off: ranking can surface better posts, and it costs scoring work on every feed.

In this example, one feed is ordered two ways from the same posts. Ranking lifts the well-liked match report from last to first, while the newest post stays near the top.

Note

ranking sits on top of either fan-out strategy, because it only changes the order of posts.

Try it Yourself

Edit the code, then press Run.

Running is simulated

The three strategies at a glance

StrategyAt post timeAt read time
Fan-out on writeOne write per followerReads a ready list
Fan-out on readOne stored postGathers and sorts posts
HybridWrites for ordinary posts onlyMerges ready list with pulled posts

Exercise

What does feed ranking order posts by?