Skip to content

fix: validate blob cache hits against backend storage - #4849

Open
Chinzzii wants to merge 1 commit into
distribution:mainfrom
Chinzzii:fix/validate-cache-hits-against-backend
Open

Chinzzii wants to merge 1 commit into
distribution:mainfrom
Chinzzii:fix/validate-cache-hits-against-backend

Conversation

@Chinzzii

Copy link
Copy Markdown

Summary

The in-memory blob descriptor cache can hold stale entries after garbage collection removes blobs from disk. Since GC runs as a separate CLI process (registry garbage-collect), it cannot clear the in-process cache of a running registry. This causes:

  1. docker push to skip uploading layers (reporting "Layer already exists")
  2. docker pull to fail with short read: unexpected EOF

After a cache hit, this PR adds a backend Stat() call to verify the blob still exists on storage. If the backend reports ErrBlobUnknown, the stale cache entry is cleared and the error is returned so the client re-uploads the layer.

Changes

  • registry/storage/cache/cachedblobdescriptorstore.go: After a cache hit in Stat(), validate against the backend. If the blob is missing, clear the stale cache entry and return ErrBlobUnknown. Added cache_stale_clears prometheus counter for observability. Fixed a copy-paste typo in the cacheHitCount comment.
  • registry/storage/cache/cache_test.go: Added TestCacheStaleClearAfterGC that simulates GC removing a blob from the backend while the cache retains a stale entry, and verifies the stale entry is detected and cleared. Fixed testStatter.Clear() to actually delete entries from its map.

Design considerations

  • Performance: Every cache hit now incurs a backend Stat(). This is a metadata-only check (no data read) and is the same call that already happens on a cache miss. For registries where GC is infrequent, the extra call is negligible. For high-throughput registries concerned about the overhead, disabling the blob descriptor cache (blobdescriptor: none) is already the recommended approach.
  • Transient errors: If the backend returns an error other than ErrBlobUnknown (e.g., a transient storage issue), the cached descriptor is still returned. Only confirmed-missing blobs trigger cache invalidation.

Fixes #1803

@milosgajdos

Copy link
Copy Markdown
Member

Sign your DCO

@Chinzzii
Chinzzii force-pushed the fix/validate-cache-hits-against-backend branch from 4babee7 to a241145 Compare April 13, 2026 17:32
@Chinzzii

Copy link
Copy Markdown
Author

Sign your DCO

done, thanks!

The in-memory blob descriptor cache can hold stale entries after
garbage collection removes blobs from disk. Since GC runs as a
separate process, it cannot clear the in-process cache. This causes
docker push to skip uploading layers (reporting "Layer already
exists") and docker pull to fail with "short read: unexpected EOF".

After a cache hit, verify the blob still exists on the backend
storage. If the backend reports ErrBlobUnknown, clear the stale
cache entry and return the error so the client re-uploads the layer.

Fixes distribution#1803

Signed-off-by: Chinzzii <[email protected]>
@Chinzzii
Chinzzii force-pushed the fix/validate-cache-hits-against-backend branch from a241145 to df7deaa Compare April 13, 2026 19:47

@milosgajdos milosgajdos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice Claud-ing, but we need to direct Claude better going forward. I've left some comments in.

Furthermore I wonder if this is the "right" fix 🤔

I wonder if could do invalidation on read failure i.e. clear cache when Get/Open/ServeBlob hits PathNotFoundError. It would fix the pull case cheaply but not the push-corruption case, so that's kinda annoying.

Another option could be some sort of TTL-based validation i.e. only validate entries older than some interval - not sure what that'd be, maybe configuratble (a few minutes could do). That would bound the stale cache window while preserving most cache benefit.

cacheRequestCount = prometheus.StorageNamespace.NewCounter("cache_requests", "The number of cache request received")
// cacheRequestCount is the number of total cache requests received.
// cacheHitCount is the number of total cache requests that were a hit.
cacheHitCount = prometheus.StorageNamespace.NewCounter("cache_hits", "The number of cache request received")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

prometheus description wasn't updated, only the Go comment. NewCounter("cache_hits", "The number of cache request received")

// may have deleted the blob from storage without being able to clear
// the in-process cache. Without this check, clients are told the
// layer already exists and skip uploading, leading to pull failures.
if _, err := cbds.backend.Stat(ctx, dgst); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I get the PR highights the fact that this means remote storage hit, which ironically is the exact I/O the cache was designed to eliminate...I feel like this PR changes defaults to protect operators who don't follow that guidance but taxes everyone else 🤔

@@ -40,6 +42,22 @@ func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (v1
desc, cacheErr := cbds.cache.Stat(ctx, dgst)
if cacheErr == nil {
cacheHitCount.Inc(1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would this increment a stale hit? Should this be moved after successful validation>

// the in-process cache. Without this check, clients are told the
// layer already exists and skip uploading, leading to pull failures.
if _, err := cbds.backend.Stat(ctx, dgst); err != nil {
if err == distribution.ErrBlobUnknown {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I reckon we should use errors.Is() here 🤔

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Registry ignores re-push of GC'd blobs unless restarted

2 participants