Try
↑ ↓ move↵ openesc close
In this lesson · topic 6 of 7
  1. 1. One post, a million timelines
  2. 2. Requirements, numbers, API and data model
  3. 3. Push, pull and hybrid in the lab
  4. 4. The celebrity problem
  5. 5. The hybrid and its threshold
  6. 6. Feed storage and pagination
  7. 7. The news feed in review

Topic 6

Feed storage and pagination

5 min read

Storing the feed

An inbox is a capped list of post ids, newest first, with a score such as the post time. A sorted set in an in-memory store fits well. Cap it at a thousand or so. Almost nobody scrolls further, and the cap bounds memory per user.

The inbox is a cache of work you can redo. If it is lost, rebuild it by pulling from the follow graph. That makes the inbox store cheap to run. It can lose data and recover, so it needs no strong guarantee.

Posts live in the post store, partitioned by post id. A read gets ids from the inbox, then fetches posts in one batch. Batching matters because a page needs 20 posts and 20 separate round trips add up. Tail latency of a fan-out read grows with the number of fetches, which is a reason to keep the count low. See the paper on tail latency in the sources.

Paging with a cursor

Page by number and the feed breaks. A reader on page two loads page three a minute later. Ten new posts have arrived at the front, so page three now repeats posts from page two.

Use a cursor instead. The cursor names the last item the reader saw, such as its time and id. The next request asks for items older than the cursor. New posts at the front do not move it.

  • Put the id in the cursor as well as the time, because two posts can share a time. Sort by time, then by id.
  • Make the cursor opaque to clients so you can change what is inside it.
  • Return no cursor when the end is reached.

The refresh case

When the reader pulls to refresh, ask for items newer than the newest one they hold. This is the cursor in the other direction. If more new items arrived than fit in one page, show a “new posts” marker, and let the reader fetch the gap on demand.

Consistency

A reader should see their own post in their feed at once. Push delay makes that fail. Insert the reader’s own post into their own inbox first, or merge it in at read time. This is the read-your-writes rule, and it is cheaper to special-case than to make the whole fan-out immediate.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms