CoursesBackend engineering from first request to production

Puzzle 11 · 12 of 18

When does a reader see yesterday’s list?

Predict the exact interval during which a saved response can be stale.

All puzzles in this course

The problem

Ada saves a second bookmark. The route still shows her first list. Where could an old answer be hiding?

See it

When does a reader see yesterday’s list? Read at 0 Write hill Read at 9 Read at 10

Scroll sideways to see the full diagram.

  1. Cache the original list.

    Step 1 of 4
  2. Commit the new row.

    Step 2 of 4
  3. The entry is still valid by time.

    Step 3 of 4
  4. Reload after expiry.

    Step 4 of 4

The building block

A cache stores a previous result for reuse. Our key is the verified owner and the entry expires ten seconds after loading. At time zero Ada reads river. A write adds hill in the database. Reading at time nine still uses the cached list. At ten the entry expires and the next read loads both names. Clearing the owner’s entry after a successful write shortens this stale window. With several processes each private cache needs its own invalidation strategy.

Write the owner’s timeline

At tick 0 Ada reads [river] and her cache entry expires at 10. At tick 1 hill commits to storage. At tick 9 the cached copy can still answer [river]. Tick 10 forces a reload, so both names appear. Under immediate invalidation the tick 9 read would also reload. Before choosing either policy, write down the promise the interface makes after a save. If it promises the writer will see hill immediately, a ten-second lifetime alone cannot meet it. Include filters and page boundaries in the key when they change the response.

Try it

Run the browser experiment

Optional local lab

Run the clock-controlled exercise. Invalidate after the write. Predict which assertion changes and why Ben’s list stays empty.

Open the local exercise files

Check yourself

Which cache key keeps two owners separate?

Right. A cache entry needs a suitable key and a freshness policy.

Not quite. Both owners share that route. Re-read that part ↑

Two pages for Ada have different filters. May they share one unqualified owner key?

Right. A key must identify the requested result.

Not quite. A key must identify the requested result. Re-read that part ↑

When is it safe to tell the writer that storage now contains hill?

Not quite. Removing a previous read result does not prove that the write succeeded. Re-read that part ↑

Right. Removing a previous read result does not prove that the write succeeded.

Sources

What you now know

A cache entry needs a suitable key and a freshness policy.