LESSON 05 · Retrieval path

Assemble one feed page

Gather candidate IDs, enforce current visibility, hydrate post data, and return a stable page.

SYSTEM VIEW

The feed path

Diagram view
Candidates—
On the page—
Page 2 repeats—

Watch one page of 20 get gathered, checked, hydrated in one batch and paged with a cursor.

  1. The Reader asks the Feed service for a page of 20. It takes 20 post IDs from the Reader’s inbox and 2 posts from the Author index, removes 1 duplicate, and keeps a bounded window of 21 candidates.

    From the inbox
    20 post IDs
    Pulled
    2 posts
    Duplicates
    1 removed

    What changed

    • GET /feed, page of 20
    • 20 inbox IDs, 2 pulled posts
    • 1 duplicate: 21 candidates
  2. An inbox entry is a candidate, not permission. Before fetching any post details, the Access check tests all 21 against current follow, block, mute, deletion and audience rules. One is from an author the Reader blocked, so it is dropped, leaving 20.

    Checked
    21 candidates
    Dropped
    1, author blocked
    Allowed
    20

    What changed

    • 21 candidates checked
    • 1 from a blocked author
    • 20 allowed
  3. Hydrating fetches each allowed post’s text and author details. Fetching 20 posts one at a time at 2 ms each takes 40 ms, so the 20 allowed IDs go to the Post cache as one batched read, and the posts and their authors come back.

    One at a time
    20 × 2 ms = 40 ms
    Batched
    1 round trip

    What changed

    • 20 IDs in one batch
    • 20 posts with authors
  4. The Feed service orders the 20 posts by server timestamp, breaking ties by post ID, and returns them with an opaque cursor: a token the Reader sends back without interpreting. It marks where page one ends, post 4398 at 09:41:07.

    Order
    server time, then ID
    Page one ends
    post 4398, 09:41:07

    What changed

    • Page one: 20 posts
    • Opaque cursor
    • Ends at post 4398
  5. Suppose page two used OFFSET 20 instead. Before the Reader scrolls, 3 new posts arrive at the top of the inbox and push every post down 3 places, so OFFSET 20 returns the last 3 posts of page one again, then 17 unseen ones.

    New on top
    3 posts
    OFFSET 20 returns
    3 repeats + 17 unseen

    What changed

    • 3 new posts on top
    • Page two by OFFSET 20
    • 3 posts repeated
  6. Page two sends the cursor: posts older than post 4398 at 09:41:07, which Postgres can express as (created_at, id) < ($1, $2). The 3 new posts don’t shift it, so nothing repeats. Visibility changes can still remove posts, and there’s no jumping to page 50.

    Postgres
    (created_at, id) < ($1, $2)
    Gives up
    jumping to page 50

    What changed

    • Page two after post 4398
    • Repeated posts
    • Hidden posts can still drop out
    • Jump to page 50
  7. The Post cache holds popular posts and authors; canonical posts stay durable in the Post store. Warm: the cache answers the batch. Cold: the batch misses and goes to the Post store, so the page is slower, but all 20 posts still arrive.

    Canonical posts
    durable in the Post store
    Cold cache
    slower, nothing lost

    What changed

    • Post store
    • Warm: batch from the cache
    • Cold: batch from the Post store
    Post cache

Space plays · ← → step · Home resets

THE IDEA

How it works

A feed request identifies the reader and carries a cursor and page size. The service obtains precomputed inbox IDs, merges any pull-source posts, removes duplicate IDs, and takes a bounded candidate window. It then checks current follow, block, mute, deletion, and audience rules before hydrating: fetching post and author details from caches or durable stores. Fetching 20 posts one at a time at 2 ms each takes 40 ms, so use a batched read.

For a reverse chronological feed, order by a server timestamp plus a unique post ID to break ties. Encode that position in an opaque cursor, a token the client sends back without interpreting. Offset pagination can skip or repeat entries when fresh posts arrive ahead of page two: with 20 posts per page, if 3 new posts arrive, OFFSET 20 returns the last 3 posts of page one again. A cursor asks for posts older than a specific one, which Postgres can express as (created_at, id) < ($1, $2). A cursor makes scrolling more stable, though visibility changes can still remove items between requests, and it gives up jumping straight to page 50.

Cache the hot head of each timeline and popular post or author objects, but keep the canonical post and relationship state durable. If the cache misses, rebuild a bounded page from indexes; a cold cache should degrade latency, not erase posts. Return media references rather than media bytes.

DECISION POINT

A blocked author’s ID remains in the reader’s cached inbox. What should the read service do?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • An inbox contains candidates, not the final authorized response.
  • Use a stable cursor and bounded hydration to keep reads predictable.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What is a follower inbox entry in this design?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.