Sub topic 4
How the DVC cache stores your files
This chapter assumes you have run dvc init and dvc add in a Git repository (earlier chapters) and are comfortable with the command line and basic hashing ideas. It explains what dvc add actually does to your data, where the data goes, and how the workspace gets it back. These details explain most of the surprises people hit with DVC, such as disk usage, read-only files, and dvc checkout messages.
What the cache is
The DVC cache is a directory, .dvc/cache by default, that holds the content of every file DVC tracks. Git stores the small .dvc pointer files. The cache stores the data they point to.
The cache is content-addressed. Each file is stored under a name derived from a hash of its bytes (MD5 by default), not under its original path or name. Two consequences follow:
- Identical content is stored once, even if it appears under two names or in two versions of the project.
- A pointer file only needs to hold a hash. Given the hash, DVC knows exactly where to find the bytes.
What dvc add does step by step
When you run dvc add data/raw.csv, DVC does the following:
- Reads the file and computes its MD5 hash.
- Moves the file's content into the cache, under a path built from the hash.
- Puts a file back at
data/raw.csvin the workspace, linked to the cached copy (see the link types below). - Writes
data/raw.csv.dvc, which records the hash, size, and path. - Adds
data/raw.csvtodata/.gitignoreso Git ignores the data itself.
The pointer file is plain YAML:
outs:
- md5: 22a1a2931c8370d3aeedd7183606fd7f
size: 14445097
hash: md5
path: raw.csvThe hash: md5 line appears in DVC 3 and later. In older versions the file has only md5, size, and path.
The cache layout
The first two hex characters of the hash become a subdirectory. The remaining 30 become the file name. In DVC 3 and later the cache has this layout:
.dvc/cache/files/md5/22/a1a2931c8370d3aeedd7183606fd7fDVC 2.x used .dvc/cache/22/a1a2931c8370d3aeedd7183606fd7f, without the files/md5 prefix. Splitting by prefix keeps any single directory from holding millions of entries. The cache file name has no extension, so you cannot tell from the cache what the file was called. Only the pointer file in Git records the path.
You can verify the mapping yourself. This script creates a tiny project, tracks a file, and compares the hash DVC recorded with one computed by Python:
mkdir cache-demo && cd cache-demo
git init
dvc init
mkdir data
printf 'id,value\n1,10\n2,20\n' > data/raw.csv
dvc add data/raw.csv
cat data/raw.csv.dvc
find .dvc/cache -type fimport hashlib
from pathlib import Path
content = Path("data/raw.csv").read_bytes()
digest = hashlib.md5(content).hexdigest()
print(digest)
print(Path(".dvc/cache/files/md5") / digest[:2] / digest[2:])The printed digest matches the md5: line in raw.csv.dvc, and the printed path matches what find listed. If your DVC version is 2.x, drop files/md5 from the path. Use a file with Unix (LF) line endings for this check, because older DVC versions normalized line endings before hashing.
Directories are tracked through a manifest
When you track a directory, for example dvc add data/images, DVC hashes every file inside it and stores each one in the cache. It also builds a manifest, a JSON file listing each relative path with its hash. The manifest is stored in the cache too, and its own hash gets a .dir suffix:
outs:
- md5: 5f6d1a0e8c3b4f27a9d0c1e2b3a49876.dir
size: 52428800
nfiles: 1200
path: imagesThe .dir hash identifies the whole directory state. Adding one image changes the manifest, so the manifest gets a new hash and the pointer file changes. The 1200 existing images are not copied again, because their hashes already exist in the cache. This is why versioning a large directory that changes by a few files stays cheap.
Cache lookups by hash also explain two edge cases:
- An empty file always has the hash
d41d8cd98f00b204e9800998ecf8427e, so all empty files share one cache entry. - Renaming a tracked file without changing its content produces the same hash. Only the pointer file's
pathchanges.
How files get back into the workspace
A cached file lives at a hash-named path, but you want data/raw.csv to exist at its normal location. DVC links the workspace path to the cache entry. The link type is configurable:
| Link type | How it works | Extra disk use | Risk |
|---|---|---|---|
| `reflink` | Copy-on-write clone (needs a filesystem such as APFS, Btrfs, or XFS) | None until you edit the file | None. Edits do not affect the cache. |
| `copy` | Full duplicate of the bytes | Doubles disk use | None, but slow for large data |
| `hardlink` | Second name for the same file on disk | None | Editing the file in place corrupts the cache entry |
| `symlink` | Workspace path points to the cache file | None | Same corruption risk. Also breaks if the cache moves. |
The default is reflink,copy. DVC tries a reflink first and falls back to a full copy if the filesystem does not support it. On a filesystem without reflinks, every tracked file therefore exists twice on disk, once in the cache and once in the workspace.
To save space on large datasets, switch to links:
dvc config cache.type reflink,hardlink,symlink,copy
dvc checkout --relinkdvc checkout --relink recreates workspace files using the new link type. Order matters, because DVC tries the types left to right and uses the first one that works.
Cache integrity and what DVC checks
A cache entry's name is its hash, so DVC can detect a damaged entry by rehashing it. Commands that read from the cache may verify content, and dvc status compares the workspace against the pointer files:
dvc statusTypical output when you have edited a tracked file without re-adding it:
data/raw.csv.dvc:
changed outs:
modified: data/raw.csvdvc add again records the new hash and stores the new content as a second cache entry. The old entry stays in the cache, because an earlier Git commit may still refer to it. This is what makes git checkout plus dvc checkout work: both versions are in the cache. It also means the cache only grows until you clean it, which a later chapter covers with dvc gc.
Moving or sharing the cache
By default every project keeps its own cache in .dvc/cache. Two clones of the same project on one machine therefore store the data twice. You can point several projects at one shared cache:
dvc cache dir /mnt/shared/dvc-cacheWithout --local, dvc cache dir writes the change to .dvc/config, which is committed to Git. The path will then be wrong on a teammate's machine. To keep the setting private, store it locally:
dvc cache dir --local /mnt/shared/dvc-cacheThe --local flag writes to .dvc/config.local, which Git ignores. For a cache shared by several users on one server, also set permissions:
dvc config cache.shared groupThis makes new cache files group-writable so other users can add to the cache. Note that DVC does not apply this setting to files already in the cache.
Common problems
- The workspace file is missing after
git clone. The pointer file arrived with Git, but the cache is empty. Rundvc pull. dvc checkoutreports a missing cache entry. The hash in the pointer file is not in your local cache, for example because the data was added on another machine and never pushed. Rundvc pull. If it still fails, the data was never pushed, and the author must rundvc push.- Disk usage is double the dataset size. You are on the default
reflink,copyon a filesystem without reflinks. Changecache.typeas shown above. - A hardlinked file is read-only and an edit fails. This is the guard working. Delete or
dvc unprotectthe file, make your change, and rundvc addagain. - The same data shows two different hashes on two machines. Check DVC versions and line endings. Different versions can differ in how they hash text files.
Exercise
You run dvc add data/images on a directory of 1000 images, commit, then add one new image and run dvc add data/images again. How many new image files does the cache store, and what changes in the pointer file?
Show answer
The cache stores one new image file, plus a new .dir manifest. The 1000 existing images already exist under their hashes and are not stored again. In images.dvc, the md5 value (ending in .dir), size, and nfiles change, because the manifest now lists 1001 entries.
Exercise
A teammate sets cache.type to symlink and runs a script that overwrites data/raw.csv in place. Why is this dangerous, and what should they do instead?
Show answer
With a symlink, data/raw.csv points to the cache entry, so overwriting it changes the cached bytes under a name that still carries the old hash. The cache no longer matches its own hash, and every version that refers to that hash gets the wrong content. They should write the new data to a new file (or remove the link first), then run dvc add data/raw.csv. DVC then stores the new content under its own hash and updates the pointer file.
Exercise
Two projects on one machine track the same 10 GB dataset. What configuration avoids storing it twice, and why should that setting use --local?
Show answer
Point both projects at one shared cache with dvc cache dir --local /path/to/shared-cache. Identical content has identical hashes, so the shared cache holds one copy. The --local flag keeps the path in .dvc/config.local, which Git ignores, so the machine-specific path is not committed and does not break teammates' setups.