Skip to content

Proposal: cross-image layer deduplication for the overlay snapshotter #13307

Open

Description

@klueska

What is the problem you're trying to solve

AI/ML container images are large — 20–30 GB is common — and much of that is packages shared across images: CUDA, Python, glibc, etc. On a GPU node running ten images that all include CUDA 12.4, images must be carefully crafted to avoid downloading and extracting the same 8 GB toolkit ten times into ten separate snapshot directories.

When different images share identical layers, their downloads are deduplicated by containerd's content store (i.e. a layer with a given digest is fetched once regardless of how many images reference it).

However, the same guarantee is not made for extracted layer content. Committed snapshots are represented as the chain of all layers from the current layer to the base, so the same layer at different positions in two image stacks produces different chains and its content may be extracted and stored on disk multiple times.

Historically this has not been a problem. Identical layers typically appear as shared base layers, meaning they occupy the same position in every image that uses them — the chain IDs match and containerd naturally deduplicates the extraction.

A different model is possible though.

Package managers like Nix organize software into content-addressable packages, which Flox is able to compose into full execution environments. These environments could be directly translated into container images where each dependency is a discrete OCI layer with a stable digest. Such images would end up with many more layers than traditional images, with identical layers interleaved at different positions across different image stacks.

With overlayfs there is no fundamental limitation preventing multiple snapshots from sharing a single extracted copy of a layer, even when that layer appears at different positions in each image's stack. It just hasn't been done this way until now, and this proposal aims to provide that support.

Describe the solution you'd like

When the overlayfs snapshotter unpacks an image, each layer becomes a committed snapshot stored under <overlayfs-root>/snapshots/<id>/, where <overlayfs-root> is the overlayfs snapshotter's data directory and <id> is the chain ID — a rolling hash over the diffID of each layer from the base up to and including the current layer (where the diffID is the SHA-256 of the layer's uncompressed content). Each snapshot has an fs/ subdirectory containing the extracted layer content, and each snapshot records the one below it as its parent, forming a chain that mirrors the image's layer order from base to the current layer.

At container start, the snapshotter walks that chain and passes each fs/ directory to the kernel as a colon-separated list of overlayfs lowerdirs. Overlayfs stacks these directories as read-only layers into a single merged view, with files from later layers taking precedence over the same path in earlier ones.

Since the fs/ directory of each snapshot is mounted read-only, nothing requires it to privately own its content. Two snapshots containing a layer with the same diffID will have identical extracted content — even when that layer appears at different positions in different image stacks, producing different chain IDs and different snapshot keys. They could share a single copy on disk and produce an identical mount either way.

The question is how to make two snapshot directories share content without changing anything about how the snapshotter tracks them. The answer turns out to be simple: overlayfs follows symlinks when resolving lowerdir paths, so fs/ does not need to be a real directory. If we extract a layer once to a stable shared path and make each snapshot's fs/ a symlink to that path, the kernel reads the shared content transparently.

Chain IDs, BoltDB parent links, snapshot keys, mount assembly, and GC are all unchanged. The deduplication lives entirely in how fs/ is created at Prepare time and how the shared directory is cleaned up at Cleanup time.

At a high level, the proposal consists of:

  • Introducing a new layer_content_cache boolean option in the overlayfs snapshotter plugin configuration; defaults to false but is a candidate for becoming the default once the feature has baked

  • Introducing a shared cache directory at <overlayfs-root>/cache/ where extracted layer content is stored by diffID and uid/gid ownership

  • Using the existing LabelSnapshotDiffID label set by the unpacker on every active layer-unpack snapshot to key into the cache when necessary

  • Introducing a new unexported LabelSkipApply label set internally by Prepare on the active snapshot when cached content exists, signaling the unpacker to skip extraction entirely

  • Handling concurrent extraction of the same uncached layer safely: the first extractor to finish wins and the others discard their copy and symlink to the winner's content

  • Garbage-collecting cache entries when the last snapshot referencing them is removed

On the first unpack of a layer, its extracted content is moved into the cache at <overlayfs-root>/cache/<algorithm>.<hex>.<uid>.<gid>/ and the snapshot's fs/ directory is replaced with a symlink to that entry. The uid/gid component ensures that ID-mapped mounts each get their own cache entry with correct ownership.

On subsequent unpacks of the same layer — at any position in any image stack — Prepare uses LabelSnapshotDiffID to locate the cached entry, sets LabelSkipApply signaling the unpacker to skip extraction, and creates the snapshot's fs/ directory as a symlink to the same cached entry. No re-extraction, no additional disk usage.

If two images race to unpack the same uncached layer simultaneously, the first extractor to finish wins; the others discard their copy and symlink to the winner's content. When the last snapshot referencing a cache entry is removed, the entry is garbage-collected.

Chain IDs, snapshot keys, and all other snapshotter semantics are completely unchanged.

Additional context

A working implementation is available at #13308.

  • While the problem statement focuses on images built from content-addressable packages, the mechanism applies to any OCI image. Any two images that share a common layer at different stack positions — something that becomes more common as images are composed from shared base components — will see the same deduplication benefit without any image modification.

  • The layer_content_cache option is opt-in today to avoid disturbing existing code paths and to allow the feature to gain confidence in production. Enabling it globally as the default is a natural next step once it has baked.

  • The LabelSnapshotDiffID and LabelSkipApply labels are now part of the exported core/snapshots API. Other snapshotters could adopt the same skip-apply protocol to implement equivalent caching strategies suited to their own storage backends.

Activity

  1. ningmingxiao commented on Apr 28, 2026

    @ningmingxiao
    Contributor

    Is there an image we can use to check how much space is saved? @klueska

  2. klueska commented on Apr 28, 2026

    @klueska
    Author

    Is there an image we can use to check how much space is saved? @klueska

    With a single image you won't see any savings from this feature — deduplication only kicks in when multiple images share layers, so there's nothing to deduplicate across a single image.

    Moreover, the savings only become significant when images are organized so that each layer corresponds to a single "package" with non-overlapping files. This is exactly how Nix naturally structures its packages — each package and its dependencies are stored as isolated, content-addressed paths in the Nix store. Flox builds on top of Nix to let you compose execution environments from these packages (much like a container image does).

    To exercise this feature, I wrote a tool to convert Flox environments into OCI images organized with one package per layer. It borrows heavily from the code for the standard flox containerize command, with the exception of how it stacks its layers. The code for my tool is embedded in the integration/lcc package of my linked PR as pkg2oci.

    With images built this way, the savings compound in two ways:

    1. Storage: layer content shared across image snapshots are stored only once on disk
    2. Pull time: layer content already present locally are recognized by their content digest and the extraction step is skipped

    As a quick demonstration of this feature, I converted all 104 of the environments in the Flox catalog into container images using pkg2oci and pulled them one after another with the LCC feature enabled vs. disabled:

    Image Image

    The results show:

    • Storage saved: 84 GB (47%) — 181 GB without LCC vs. 97 GB with LCC
    • Pull time saved: 39% — 6.3 min without LCC vs. 3.8 min with LCC

    This benchmark represents a semi-realistic workload for anyone adopting Nix/Flox-based images in a shared environment (e.g., a GPU node pulling many images that all include CUDA and common ML packages).

    For traditional Dockerfile-based images the improvement will be more modest and image-specific — it depends entirely on how much layer content overlaps across the images on a given host and how those layers have been built up.

  3. samuelkarp commented on Apr 28, 2026

    @samuelkarp
    Member

    Moreover, the savings only become significant when images are organized so that each layer corresponds to a single "package" with non-overlapping files. This is exactly how Nix naturally structures its packages

    https://github.com/pdtpartners/nix-snapshotter is a thing (though not one I've actually tried).

    For traditional Dockerfile-based images the improvement will be more modest and image-specific — it depends entirely on how much layer content overlaps across the images on a given host and how those layers have been built up.

    It's unclear to me then if this is super beneficial. How widely would Nix and Flox be used for these kinds of environments, and would they be better served by a Nix-specific snapshotter like the one above?

  4. klueska commented on Apr 28, 2026

    @klueska
    Author

    Nix and Flox are just the concrete example I used to motivate and benchmark this change. The feature itself is intentionally ecosystem-agnostic.

    The implementation is designed to be fully backwards compatible. It deduplicates any two layers with the same diff ID, regardless of which tool produced the image or where in the layer stack the layer appears. Any image ecosystem that produces layers with stable, reusable content would benefit from this change.

    A Nix-specific snapshotter like nix-snapshotter solves a different problem (mounting Nix store paths directly rather than extracting OCI layers). This proposal works at the OCI layer level and requires no changes to how images are built, pushed, or referenced.

  5. ningmingxiao commented on Apr 30, 2026

    @ningmingxiao
    Contributor

    do you have specific images to verify it ? you can add it add ci. @klueska

  6. klueska commented on Apr 30, 2026

    @klueska
    Author

    @ningmingxiao the linked PR includes both unit tests and integration tests that work with on-the-fly generated images.

    Existing unit and integration tests suites now run with LCC off (existing) and on (new), ensuring backwards compatibility:
    09de861

    LCC specific unit tests:
    dbc229b

    LCC specific integration tests:
    36b17b0

    Example integration test results:
    https://github.com/containerd/containerd/actions/runs/25111331329/job/73587147967?pr=13308

  7. klueska commented on May 5, 2026

    @klueska
    Author

    This proposal was discussed in the containerd community meeting on April 30th, 2026.
    Slides here: https://drive.google.com/file/d/1DJQc-tuqRrqVWlQOaOIdmSbgKKzH7Bmc/view?usp=sharing

    One concrete thing that came out of the meeting was that Overlayfs does not support having the same directory listed twice in its chain of lower_dirs. An extra commit was added to the PR to address this.

  8. saolof commented on May 27, 2026

    @saolof

    It may also be worth looking at how podman and cri-o handle the same features in https://github.com/containers/container-libs/blob/main/storage/internal/dedup/dedup_linux.go .

    They support partial pulls of images in zstd:chunked format where they simply never fetch the parts of layers that they don't need and can block copy them from cached images instead. The entire code to support the dedup part is only ~200 lines of Go by using the linux filededuprange ioctl. They also dedup identical files within the same image if they exist.

    The nice thing about relying on unix.FileDedupRange is that upper layers can be completely unaware that the file is deduplicated. If you go with that approach, deduplicating identical file contents once you have identified them is the filesystems job and you only need to keep track of file hashes. The application layer can be completely unaware of it. The main downside is that not all filesystems implement the syscall. xfs and btrfs implement it, while ext4 is the last major filesystem that does not implement it afaik. One alternative is to chattr+i to ensure that the files are immutable, and then hard link.

    The kernel's implementation is atomic so it cannot race with other file operations and will handle TOCTOU edge cases gracefully.

  9. Asthenia0412 commented on Oct 14, 2026

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions