VolumeCache, a helper that warms local directories across Serverless workers using an attached network volume. It keeps a browsable mirror of your cache directories on the volume: on cold start it restores previously cached files into place, and after your code runs it copies newly written files back. This turns a repeated multi-GB model download on every cold start into a one-time cost per endpoint, which reduces times.
VolumeCache is best-effort and self-contained, and it never affects the outcome of a job. If any part of the cache fails, the worker falls back to a normal cold start. You add it explicitly by wrapping the code that populates your cache, so nothing runs until you opt in.
Requirements
- A network volume attached to your endpoint, mounted at
/runpod-volume. The mirror is stored on the volume, so every operation is a safe no-op when no volume is mounted. - The Runpod Python SDK installed in your worker image.
- An endpoint namespace to scope the mirror. On Serverless this defaults to
RUNPOD_ENDPOINT_ID, which Runpod sets automatically.
Usage
VolumeCache is a context manager. Wrapping it around your model load hydrates the cache before the block runs and syncs any changes back afterward:
handler.py
hydrate() copies files that are missing or newer on the volume mirror into the container. When you exit, sync() copies files that are missing or newer in the container back onto the mirror. By default the sync runs on a background daemon thread and returns immediately, so the with block doesn’t wait on it. A process-exit hook completes any outstanding syncs, so short-lived processes still finish syncing before they exit.
You can also call the phases directly when they happen at different points in your worker’s lifecycle:
Constructor
VolumeCache accepts the following arguments:
How it works
VolumeCache adapts its transport to the size of your files, which keeps both many-small-file caches and multi-GB weight files fast.
- Size-bucketed mirror: Cached files live at
{volume_path}/.cache/{namespace}on the volume. Files below 256 KiB are packed into a singlesmall.tararchive, which collapses the per-file metadata round-trips that make many small files slow on a network volume. Larger files are copied unpacked into abig/subdirectory, which preserves their original relative paths so the large-file subtree stays browsable. A versionedmanifest.json, written last, records the size and modification time of every cached file and marks the mirror as complete. A mirror without a valid, current manifest is treated as absent, so a sync self-heals. - Incremental large files, whole-archive small files: Large-file transfers are diffed per file against the manifest, so unchanged files are skipped. The
small.tararchive is repacked whole whenever any small file changes, since unpacking and re-diffing many tiny files individually is slower than the volume’s per-file overhead. - Parallel copy: Large-file transfers run across a thread pool sized by
max_workers. The work is I/O-bound, so the default oversubscribes the CPU count. - Safety: Symbolic links are never followed or copied. Every archive member and every large-file destination is checked to resolve inside one of your configured
dirsbefore it is written, so a mirror entry can’t write outside your cached directories.
Limitations
- Concurrent cold-start write amplification: If several workers cold-start at the same time, each may miss the still-empty mirror, download the model, and sync a full copy back. There’s no coordination between concurrent syncs, so the mirror reflects whichever worker synced most recently.
- Background sync on short-lived processes:
sync()schedules the copy on a daemon thread. If the process exits without a normal interpreter shutdown (for example,os._exitorSIGKILL), the exit hook never runs and the sync may not complete. - Orphaned large files aren’t pruned: If you delete or rename a large file locally, its copy under
big/stays on the volume. Hydration is manifest-driven and ignores it, but volume usage grows as you swap model versions.
Next steps
- Optimize your endpoints: Combine the volume cache with other strategies to reduce cold starts.
- Cached models: Use Runpod’s platform-level model cache for Hugging Face models.
- Storage options: Compare container disks, network volumes, and S3-compatible storage.