Lesson 10 of 15
37. Design a News Feed

37.9 How do the feed cache and pagination work?

3 min read+10 XP

The feed cache holds, per user, the latest 800 post IDs (newest first), in a Redis list or sorted set by post ID, sharded by user. About 0.64 TB for 100 million users. Trim on every insert; older items come from the posts store if someone scrolls that far.

Pagination with a ranked feed is tricky. Offset pagination ("items 11-20") breaks when new posts arrive and the ranking shifts between pages:

re-ranking and using an offset shows posts twice; a session snapshot with a cursor doesn't.

Figure: rank once per session; page through the snapshot with a cursor.

In the chapter's code, three new high-scoring posts arrive between page 1 and page 2; re-ranking and taking items 11-20 shows 3 posts twice. The fix:

  • Rank once when the session opens (or on pull-to-refresh) and keep the ordered IDs as a snapshot (in a short-lived cache, keyed by session).
  • Page through it with a cursor ("after this post ID"), as in Chapter 24.
  • New posts show as a "3 new posts" banner; tapping it refreshes and re-ranks.

Memory code: "Rank once per visit, scroll with a cursor, announce new posts at the top."