LESSON 02 · Publish path

Commit the post before fanout

Keep one canonical post, then distribute lightweight references asynchronously.

SYSTEM VIEW

The feed path

Diagram view
Posts—
Inbox entries—
Feed delay—

Watch a post commit once despite a retry and fan out after the reply, then see an outbox carry the next post through two crashes.

  1. The Author sends a post with idempotency key 7f3a, chosen by the client. The Post API checks auth, the rate limit, the audience and media readiness, then stores one canonical post, 4417, with a server ID and creation time, and answers 201.

    Key
    7f3a, from the client
    Post ID
    4417, from the server

    What changed

    • Post 4417 stored
    • Auth, limit, audience checked
    • 201 to the Author
  2. Say the reply is slow and the Author retries with the same key, 7f3a. The Post API finds post 4417 already stored under that key and returns it again rather than publishing twice. The Author still has one post.

    Retry key
    7f3a
    Returns
    post 4417 again

    What changed

    • Retry with key 7f3a
    • Same post 4417 returned
    • Still one post
  3. After the commit, the Post API puts post 4417 on the durable Fanout queue. Fanout workers read a versioned follow graph and write its ID into 5,000 Inboxes. The response doesn’t wait: at, say, 1 ms a write, that would take 5 seconds.

    Per write
    1 ms
    Followers
    5,000
    If it waited
    5 s

    What changed

    • Fanout queue, workers, Inboxes
    • 4417 in 5,000 Inboxes
    • The response doesn’t wait
  4. Post 4418 commits, but the enqueue that follows fails. The post exists, yet no inbox gets it: a valid post is hidden from feeds. The reverse order is no better: an event sent before the commit can point at a post that doesn’t exist.

    Commit, then enqueue
    post hidden
    Enqueue, then commit
    may point at nothing

    What changed

    • Post 4418 stored
    • Its enqueue is lost
    • 4418 reaches no inbox
  5. Replay post 4418 with an outbox: the post and an outbox row for its fanout commit in one transaction at 10:00:00. A Relay moves unsent rows to the Fanout queue, starting at row 90. The cost: a table to purge, a relay to run.

    Commit
    10:00:00
    Relay tools
    e.g. Debezium to Kafka

    What changed

    • Outbox row 90: post 4418
    • Relay, cursor at row 90
    • Post and row in one commit
  6. The Relay dies before sending row 90. The post and its outbox row stay committed, so nothing is lost. At 10:02:00 the Relay restarts, resumes at row 90 and publishes post 4418 to the Fanout queue, two minutes late.

    Committed
    10:00:00
    Relay back
    10:02:00
    Delay
    2 min

    What changed

    • Relay crashes
    • Row 90 waits, kept
    • Post 4418 on the queue
  7. A Fanout worker writes post 4418 into 2,500 of the 5,000 inboxes, then crashes, so another worker repeats the job. Inserts keyed by post and recipient make the repeats harmless: 5,000 entries, not 7,500. Followers see the post two minutes late; nothing is lost.

    Insert key
    post + recipient
    Repeated
    2,500 inserts
    Result
    5,000 entries

    What changed

    • A worker crashes at 2,500
    • Another worker repeats the job
    • 5,000 entries, not 7,500

Space plays · ← → step · Home resets

THE IDEA

How it works

The publisher sends text, media references, and an idempotency key, a client-chosen ID for this submission. The API authenticates the account, checks the rate limit and audience, validates media readiness, and stores one canonical post with a server ID and creation time. A retry using the same key returns the same post rather than publishing twice.

After commit, an event enters a durable queue. Fanout workers read a versioned follow graph and write eligible post IDs into timeline stores, per-reader lists called inboxes. The publish API does not wait for those writes: at an example 1 ms each, 5,000 followers would hold the response for 5 seconds. If the queue stalls, the canonical post survives; workers can catch up from a durable event stream or outbox.

An event emitted before the database commit can point to a post that does not exist. A commit followed by a failed enqueue can hide a valid post from feeds. A transactional outbox, a row committed in the post’s own transaction that records the fanout work, closes that gap; tools such as Debezium can relay those rows from a database change log to Kafka. It adds a table to purge and a relay to run.

Follow one crash. The post and outbox row commit at 10:00:00. The relay dies, restarts at 10:02:00, and publishes the row. A worker fills half the inboxes and crashes, so another worker repeats the job. Inserts keyed by post and recipient make the repeats harmless. Followers see the post two minutes late, and nothing is lost.

DECISION POINT

The post row is saved, but the fanout process crashes before enqueueing work. What prevents a permanently missing feed entry?

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

Keep in mind

  • A canonical post is the source of truth; feed entries are derived references.
  • Use a reliable post-to-queue handoff and idempotent fanout.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Why store post IDs in follower inboxes rather than full post copies?

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.