Skip to content

fix: serve cached snapshots immediately, regenerate in background#193

Merged
worstell merged 1 commit intomainfrom
worstell/snapshot-serving-fix
Mar 17, 2026
Merged

fix: serve cached snapshots immediately, regenerate in background#193
worstell merged 1 commit intomainfrom
worstell/snapshot-serving-fix

Conversation

@worstell
Copy link
Contributor

Problem

The snapshot request path was fetching from upstream synchronously, then checking if the cached snapshot's Last-Modified predated lastFetch. Since the fetch just ran, the snapshot was always invalidated, causing a regeneration loop on active repos. Every workstation request triggered a full git clone → tar → zstd cycle instead of serving the cached snapshot.

Fix

  1. Always serve a cached snapshot if one exists. The workstation's git fetch after extracting the snapshot goes through cachew's git http-backend path, which forwards info/refs directly to upstream when refs are stale. The workstation always ends up fully up to date — the snapshot just provides a fast ~95% head start.

  2. Move the upstream fetch to a background goroutine so it keeps the mirror fresh for git fetch/git pull operations without blocking snapshot serving.

  3. Reduce default mirror-snapshot-interval from 6h to 2h so new pods restore a fresher mirror snapshot and the post-restore delta fetch is smaller.

Observed impact on staging

Before: every snapshot request on active repos logged Cached snapshot predates last fetch, regenerating — clone times were 2-10x expected.

After: cached snapshots served immediately, Refs stale for snapshot request, fetching in background logged instead. Clone times back to expected levels.

The snapshot request path was fetching from upstream synchronously, then
invalidating the cached snapshot because its Last-Modified predated the
fetch that just ran. On active repos this created a regeneration loop
where every request rebuilt the snapshot.

Instead, always serve a cached snapshot if one exists and move the
upstream fetch to a background goroutine. The workstation's git-fetch
after extracting the snapshot goes through cachew's git http-backend
path, which forwards to upstream when refs are stale, so the workstation
always ends up fully up to date.

Also reduce the default mirror-snapshot-interval from 6h to 2h so that
new pods restore a fresher mirror snapshot and the post-restore delta
fetch is smaller.

Amp-Thread-ID: https://ampcode.com/threads/T-019cf8fc-dbea-761b-8933-a781adb473bb
Co-authored-by: Amp <amp@ampcode.com>
@worstell worstell requested a review from a team as a code owner March 17, 2026 01:33
@worstell worstell requested review from alecthomas and removed request for a team March 17, 2026 01:33
@worstell worstell merged commit 23b8660 into main Mar 17, 2026
7 checks passed
@worstell worstell deleted the worstell/snapshot-serving-fix branch March 17, 2026 01:47
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.

2 participants