Skip to content

fix(cache): keep the writer's TTL when backfilling tiers - #461

Open
ravi-pplx wants to merge 3 commits into
block:mainfrom
perplexityai:fix/tiered-backfill-expiry
Open

ravi-pplx wants to merge 3 commits into
block:mainfrom
perplexityai:fix/tiered-backfill-expiry

Conversation

@ravi-pplx

@ravi-pplx ravi-pplx commented Nov 22, 2026 •

Copy link
Copy Markdown

When a tiered cache serves an entry from a higher tier, it backfills the lowest tier, and a ranged If-Match read can heal tier 0 from a deeper tier. Both create the new copy with TTL 0, which means the destination's maximum TTL. Some entries are written with an explicit TTL: git bundles (bundle-cache-ttl, 2h by default), apiv1 uploads with Time-To-Live, and handler fills that set a TTL function.

On a replica whose cold disk tier is filled from a shared S3 tier, the copy outlives that TTL, so the replica keeps serving the entry after the writer's tier has expired it. This is the tier-side counterpart of #439, which stopped S3 reads from extending explicit TTLs.

Change

  • Tiered.Create stores the end of an explicit TTL in an internal X-Cachew-Expires-At header, which is written to every tier with the entry. Tiered.UpdateMetadata stamps it again, or clears it when the new TTL is 0.
  • Backfills and heals create the copy with the time left until then. If that time has passed, the copy is skipped and the fill is recorded with the new result expired. The caller still gets the response it was reading.
  • Tiered owns the header: a caller-supplied value is replaced, and Stat, Open and AuthoritativeStat remove it from the headers they return.
  • Memory now caps an explicit TTL at its max-ttl in Create and UpdateMetadata, as Disk and S3 already do. Without that, a backfill into a memory tier carrying the writer's remaining TTL could outlive the memory tier's maximum, which TTL 0 used to enforce.
  • The README covers the backfill expiry and the expired fill result.

Behaviour changes

  • Entries written with TTL 0, and entries written before this change, behave as before, because they carry no header.
  • During a rolling upgrade, an older replica keeps the old backfill behaviour for entries that carry the header. It also includes the header in the responses it serves, because it doesn't know to remove it.
  • A memory cache entry written or refreshed with a TTL above the memory cache's max-ttl now expires at max-ttl.
  • Single-tier deployments don't use Tiered, so apart from the Memory cap they are unaffected.
  • When the deepest tier is a Remote cachewd, the expiry survives only if that server uses a single cache, which stores and returns the header. A tiered server removes the header from the responses it serves, so the client's backfill from it still gets the maximum TTL.

Testing

  • The tests in tiered_expiry_test.go record the TTL each tier's Create receives, so they need no sleeps. All five fail on main.
    • TestTieredBackfillKeepsWriterTTL: a backfill keeps the writer's TTL.
    • TestTieredBackfillWithoutTTLUsesMaxTTL: an entry without a TTL, including one with a caller-supplied header, still gets the maximum.
    • TestTieredBackfillKeepsUpdatedTTL: a TTL that UpdateMetadata refreshes and then clears.
    • TestTieredHealKeepsWriterTTL: a ranged heal.
    • TestTieredSkipsBackfillPastWriterTTL: a backfill is skipped once the TTL has passed.
  • TestMemoryCapsTTLAtMaxTTL checks that Create and UpdateMetadata cap a TTL above max-ttl. It fails without the Memory change.
  • Manual check with two cachewd replicas sharing a MinIO bucket, each with its own disk tier. An entry is PUT on replica A with an 8s TTL and read once on replica B, which backfills B's disk. 10s later on main, A returns 404 but B still returns 200 with the expired body. With this change, both return 404.

Created with Perplexity

Tiered backfills and ranged heals created lower-tier copies with TTL 0,
the destination's maximum. An entry written with an explicit TTL, such
as a git bundle or an apiv1 Time-To-Live upload, outlived that TTL on
any replica whose local tier was filled from the shared tier.

Tiered now records when an explicit TTL ends in an internal header on
Create and UpdateMetadata, and copies into another tier get the time
left. A copy is skipped once that time has passed. Entries written
without a TTL, and entries written before this change, keep the old
behaviour. Tiered removes the header from the headers it returns.

Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx
ravi-pplx requested a review from a team as a code owner November 22, 2026 04:17
@ravi-pplx
ravi-pplx requested review from jrobotham-square and removed request for a team November 22, 2026 04:17
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T18:49:25.871281Z 1a0e166 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx

Copy link
Copy Markdown
Author

@codex review

Created with Perplexity

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2cd033d637

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread internal/cache/tiered.go Resolved
Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx

Copy link
Copy Markdown
Author

@codex review

Created with Perplexity

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 1a0e1667b2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant