Skip to content

feat(cache): add optional TTL to the inmemory blob descriptor cache - #5008

Open
yelaissaoui wants to merge 1 commit into
distribution:mainfrom
yelaissaoui:feat/inmemory-blobdescriptor-ttl
Open

yelaissaoui wants to merge 1 commit into
distribution:mainfrom
yelaissaoui:feat/inmemory-blobdescriptor-ttl

Conversation

@yelaissaoui

@yelaissaoui yelaissaoui commented Oct 12, 2026 •

Copy link
Copy Markdown

We run distribution as a pull through cache for Docker Hub (S3 backend, inmemory blob descriptor cache). After a GC run, or after objects were removed from the bucket, the proxy kept answering 307 to blobs that no longer existed: the cache still had their descriptors, so serveLocal never fell back to the upstream. Clients got a 404 from S3 and the only fix was restarting the registry. #4868 fixed the same kind of issue for manifest writes, but blobs served through redirects are still affected.

This adds an opt-in storage.cache.blobdescriptorttl option (e.g. 10m). Once a descriptor expires it is looked up again in the backend, so a missing blob gets noticed and re-fetched within the TTL instead of staying broken until a restart. Without the option nothing changes.

A couple of notes:

  • Each entry carries its own expiry on the existing ARC cache. A repository entry never outlives the global entry it was copied from, so a descriptor can't stay cached more than the TTL after storage was last checked.
  • It only applies to inmemory. With redis the option is ignored with a warning, same as blobdescriptorsize.

Added unit tests and a proxy test, go test -short ./... passes.

Related: #1803, #2966, #3390

@github-actions github-actions Bot added area/storage area/cache dependencies Pull requests that update a dependency file area/docs area/api labels Oct 12, 2026
@yelaissaoui yelaissaoui changed the title Add optional TTL to the inmemory blob descriptor cache feat(cache): add optional TTL to the inmemory blob descriptor cache Oct 12, 2026

@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.

I spent a bit of time on this today and Im worried this somehow addressses a symptom instead of underlying issue.

My concern is that this adds a TTL without making the proxy reliably recover from a missing blob...which is what we should focus on.

A repo cache can be populated from the "global cache" i.e. the cache shared by all repos; that gives an old descriptor a fresh TTL without checking storage. I actually reproduced at least one problem, though I think there may be more - I'd have to yell at my agents a bit longer but heres one of the issues:

With the default cache size, a blob shared between repositories returned 500 after the TTL elapsed, without being fetched from upstream - that is not great!

Could we make sure copying a cached descriptor doesn’t restart its lifetime from scratch? I feel like that's one of the issues here. Also cab we add a proxy test that deletes the underlying blob and verifies that it gets fetched again within the configured TTL?

Entries in the inmemory blob descriptor cache never expire, so when blobs
are removed from the storage backend while the registry is running (garbage
collection, manual cleanup), the cache keeps reporting them as present until
the registry is restarted. In pull through cache mode this means clients are
redirected to missing objects and the blobs are never fetched again from
the upstream.

Add a storage.cache.blobdescriptorttl option. When set, descriptors expire
after the given duration and are looked up again in the backend, which
bounds how long the cache can be wrong. It defaults to no expiration, so
the current behaviour is unchanged.

Signed-off-by: Yassine EL AISSAOUI <[email protected]>
@yelaissaoui
yelaissaoui force-pushed the feat/inmemory-blobdescriptor-ttl branch from 2a39f48 to cb22e22 Compare November 1, 2026 19:50
@github-actions github-actions Bot added the area/proxy Related to registry as a pull-through cache label Nov 1, 2026
@yelaissaoui

Copy link
Copy Markdown
Author

Thanks, you were right: a repo entry copied from the global one got a fresh TTL. Fixed, entries now keep their own expiry, and I added the proxy test you asked for (fails before the fix). Dropped the golang-lru bump too.

On recovery: with S3 redirects the proxy never reads the blob, so catching a missing one means a stat on every request (~180ms per blob for us). Bounding staleness looks like the practical fix to me. I can do an upstream fallback in a separate PR if you want, what do you think ?

jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller than the size in the blob
descriptor, the transfer now completes. Only a client that checks size
or digest detects the mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052
reduces CPU time no further.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller than the size in the blob
descriptor, the transfer now completes. Only a client that checks size
or digest detects the mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052
reduces CPU time no further.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to http.ServeContent.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an *os.File to
http.ServeContent.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, fileReader allocated a buffer of up to 4 MiB per GET
and copied blobs larger than 32 KiB a second time. Now ServeBlob passes
the *os.File to http.ServeContent.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning. Does removing this
buffer hurt? Would a smaller buffer be better? With a warm page cache,
no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or
more registry total CPU time than this patch. With a cold page cache,
removing the buffer has a cost that is small compared to the 13% to 42%
gain with a warm page cache: registry total CPU time was 2% to 7% higher
(1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to http.ServeContent requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is less than the size in the blob descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo logged "unexpected EOF" and retried in both
cases.

Remarks
-------

This patch applies only to the filesystem driver, when its Reader
returns an *os.File. For S3, Azure, GCS and all other storage drivers,
the registry still uses fileReader and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the size of the blob file differs from the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

net/http can use sendfile(2) for plain HTTP only. For TLS, distribution#5052 reduced
registry total CPU time by at most 7% more (measured for range GETs).

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 12, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to
`http.ServeContent`.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an `*os.File` to
`http.ServeContent`.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, `fileReader` allocated a buffer of up to 4 MiB per
GET and copied blobs larger than 32 KiB a second time. Now `ServeBlob`
passes `*os.File` directly to `http.ServeContent`.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning.

Does removing this buffer hurt? Would a smaller buffer be better? With a
warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f)
used the same or more registry total CPU time than this patch. With a
cold page cache, removing the buffer has a cost that is small compared
to the aforementioned gains (registry total CPU time was 2% to 7% higher
for 1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to `http.ServeContent` requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is smaller than the size stored in the blob
  descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo for example would log "unexpected EOF" and
retry in both cases.

Remarks
-------

This patch applies only to the filesystem driver, when its `Reader`
returns an `*os.File`. For S3, Azure, GCS and all other storage drivers,
the registry still uses `fileReader` and its buffer (capped at blob size
since distribution#5070). HEAD responses are unchanged.

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller or larger than the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

Tests
-----

The new tests fail without the changes to `blobserver.go` and `blob.go`,
except `TestBlobServerOtherDriverReaderCalls`, which covers what this
patch does not change: one `Reader` call per GET on other drivers.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
jgehrcke added a commit to jgehrcke/distribution that referenced this pull request Nov 17, 2026
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to
`http.ServeContent`.

This patch was extracted from distribution#5052. That PR builds on this patch to
enable sendfile(2), which requires passing an `*os.File` to
`http.ServeContent`.

Performance impact
------------------

With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.

Before this patch, `fileReader` allocated a buffer of up to 4 MiB per
GET and copied blobs larger than 32 KiB a second time. Now `ServeBlob`
passes `*os.File` directly to `http.ServeContent`.

Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:

    Transport   Blob    Before (n=15)      Now (n=15)         Change
    plain HTTP   4 MiB   1.994 +/- 0.026    1.160 +/- 0.009   -41.8%
    plain HTTP  32 MiB   9.423 +/- 0.061    6.851 +/- 0.021   -27.3%
    TLS          4 MiB   3.401 +/- 0.027    2.404 +/- 0.018   -29.3%
    TLS         32 MiB  20.159 +/- 0.063   17.528 +/- 0.113   -13.0%

The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning.

Does removing this buffer hurt? Would a smaller buffer be better? With a
warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f)
used the same or more registry total CPU time than this patch. With a
cold page cache, removing the buffer has a cost that is small compared
to the aforementioned gains (registry total CPU time was 2% to 7% higher
for 1 GiB blobs, local NVMe drive).

Fixed
-----

The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to `http.ServeContent` requires both
changes. Each change fixes one defect (distribution#5080):

- The blob file is missing or cannot be opened.
  Before: The registry sent status 200, a Content-Length header and no
  body (missing file: issues distribution#5010 and distribution#2966).
  Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
  missing file. It sends status 500 for other open errors.
- The blob file size is smaller than the size stored in the blob
  descriptor.
  Before: The registry set Content-Length from the size in the blob
  descriptor and sent a truncated body (curl, for example, reports
  error 18).
  Now: The registry sets Content-Length from the file size.

Before this patch, skopeo for example would log "unexpected EOF" and
retry in both cases.

Remarks
-------

For blob GETs, the filesystem driver opts in through `ReadSeeker`
and returns the original `*os.File`. Drivers and middleware without
`ReadSeeker` use `fileReader` and its buffer (capped at blob size
since distribution#5070).

If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.

In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what distribution#5010 prefers, and is left for a follow-up.

The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller or larger than the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.

Tests
-----

The stale-size and missing-file tests cover the two GET fixes.
`TestBlobServerDriverWithoutReadSeeker` checks that a range GET through
a filesystem wrapper makes one `Reader` call at the requested offset.

Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008.

Fixes distribution#5080

Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>

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

Labels

area/api area/cache area/docs area/proxy Related to registry as a pull-through cache area/storage dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants