CAPSTONE: CACHE A FEED WITH READ-YOUR-WRITES

Everything in this path, in one timeline

L320 MINCACHING
PREDICT

The app sends Alice to the primary while the replica is behind her last write. Otherwise it reads the cache, then the replica. Alice posts and the cache key is deleted. Can Alice still miss her own post?

This capstone puts the path together. Posts go to the primary. Feeds are read from an async replica (replication lag) through a cache (cache-aside, delete on post). A celebrity’s feed is one hot key (stampede).

The author must see her own post. A follower may see an older feed, and the fix below does not bound how long: if Bob fills the cache from the lagging replica, that entry stays until the author reads again or a TTL expires it. The session remembers the LSN of the author’s last write. A cache entry records the replica LSN it was filled at. The author accepts the cache only if that entry is at or past her write. Otherwise she reads the replica once it has caught up, or the primary until then.

▶ SIMULATION
ALICE (author)
session: last write lsn —
saw: —
mode: routed
CACHE feed:alice
posts: 1
filled at lsn: 1
PRIMARY → REPLICA
primary lsn 1 · replica lsn 1
primary feed reads 0
lag: 0 write(s)
Bob saw (may lag)—
feed:celeb stormnot yet
last event—
✓ Alice always sees her own posts✓ DB loads per hot key per expiry ≤ 1
SCHEDULE
01[APP]config: read cache, else replica
02[ALICE]Alice: INSERT post "launch day" on the primary
03[ALICE]Alice: DEL cache feed:alice
04[ALICE]Alice: read feed:alice
05[REPLICA]replica applies the primary's log
06[BOB]Bob: read feed:alice
07[FANS]feed:celeb expires; 40 fans read it 5 ms apart
08[FANS]report
Start of naive scenario. 8 steps.
function post(user, text) {
  db.primary.insert(user, text);
  cache.del(`feed:${user}`);
}
function readFeed(viewer, owner) {
  const hit = cache.get(`feed:${owner}`);
  if (hit) return hit.posts;                 // may predate viewer's write
  const posts = db.replica.feed(owner);      // may lag viewer's write
  cache.set(`feed:${owner}`, { posts });
  return posts;
}

Watch the ALICE box and the invariant strip. NAIVE misses the post straight away. ROUTE TO PRIMARY misses it later, through Bob’s fill. Both send 24 loads to the DB for feed:celeb. The fix runs the same steps as ROUTE TO PRIMARY, plus one extra Alice read while the replica still lags. That read goes to the primary. After the replica catches up, she rejects Bob’s old entry and refills from the replica. The celebrity’s feed costs 1 load.

BREAK IT NOT YET

The app already sends Alice to the primary while the replica is behind her post. It still trusts any cache hit. Run every step so that Alice reads her feed and her post is not there.

GOAL: Alice reads her feed and her own post is missing

FIX IT NOT YET

Make readFeed read-your-writes. The author must always see her own post, even when the replica lags and even when a follower filled the cache from the lagging replica. Followers must never read the primary, the author only while the replica is behind her, and repeat reads must still hit the cache.

Mental model:

every copy of the data is at some point in the log: the primary, the replica, each cache entry. A read is fresh enough for a reader if that copy is at or past the reader’s own last write. So give every copy a position and give every session a minimum. The rest of the path still applies. Fills race deletes, hot keys need one loader, and expiries need jitter.

IN PRODUCTION

Social feeds, comment threads, profile pages, shopping carts: anything a user edits and immediately reads back through replicas and caches. Real systems carry a write position in a cookie or session (MySQL GTIDs, Postgres LSNs, a write timestamp) and pin the user to the primary for a short window or until a replica has caught up.

IN INTERVIEWS

“Design a cached timeline where users always see their own posts.” Walk the write path (primary, invalidate), the read path (cache, replica), then name the two ways the author misses her post and the fix: session write position plus versioned cache entries. Add singleflight or leases for hot keys, jittered TTLs, and say what consistency followers get: eventual, bounded only by the cache entry's TTL unless fills refuse to go backwards (a versioned set or a lease).

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.