---
title: "Copy-on-write"
url: https://www.daytona.com/compute-glossary/storage/copy-on-write
---

# Copy-on-write

Part of the [Compute Glossary](https://www.daytona.com/compute-glossary.md), section [Storage](https://www.daytona.com/compute-glossary/storage.md).

Also called CoW.

Copy-on-write is a way of sharing data between copies until one of them writes to it. A new copy starts as a set of references to the original rather than a second set of bytes. When either side changes a piece, only that piece is copied, and the writer gets its own version. Everything neither side has changed stays shared.

It exists because many copies are made and then barely changed. A forked [process](https://www.daytona.com/compute-glossary/kernel/process.md) often replaces itself with a new program right away, and a new disk often differs from its image by a few files. Copying everything up front would waste time and space on data that never changes. Copy-on-write makes a copy almost free to create and charges only for the writes.

The idea appears at many layers, from [memory pages](https://www.daytona.com/compute-glossary/kernel/memory-page.md) in the [kernel](https://www.daytona.com/compute-glossary/kernel/kernel.md) to [file systems](https://www.daytona.com/compute-glossary/storage/file-system.md) and [block storage](https://www.daytona.com/compute-glossary/storage/block-storage.md). Its cost moves from the moment of copying to the first write of each piece, which then takes longer. Sandboxes depend on it to start quickly from a shared image and to make [snapshots](https://www.daytona.com/compute-glossary/sandboxes/snapshot.md) cheap. Many sandboxes on one host can share one copy of their image, each keeping only its own changes.

## Memory pages

Copy-on-write memory is the sharing of pages between processes until one of them writes. After fork, parent and child map the same physical pages, and the kernel marks the private writable ones read-only for both. A write to such a page causes a page fault, and the kernel copies the page and maps the copy as writable for the writer. Pages that neither side writes are never copied.

The kernel uses the same trick for memory that a program has reserved but not yet written. Reads of such memory can all map one shared page of zeros, and a real page is allocated only on the first write. Programs that fork to save a copy of their data rely on this, with Redis as a common example. Under heavy writes during the save, its memory use can grow toward double, because most pages end up copied.

Page merging runs copy-on-write the other way, joining copies that happen to hold the same data. On Linux, kernel samepage merging scans memory for identical pages and maps them to one shared page marked read-only. A later write splits them again with a fresh copy. Hosts use it to fit more virtual machines in the same memory, though the timing of those splits can leak information between guests.

## Disk images

Copy-on-write disk images are disks that read unchanged data from a shared base and store only their own changes. A qcow2 image can name a backing file, and reads of any part it has not written fall through to that file. A write stores the new data in the image itself, so the base stays untouched and can serve many images. Each snapshot can add another file, so the images form a chain.

Overlay file systems apply the idea to files rather than blocks. A [container](https://www.daytona.com/compute-glossary/virtualization/container.md) image stacks read-only layers under one writable layer, and a write first copies the whole file up into that layer. Block devices offer the same through thin clones, such as LVM thin volumes or Ceph RBD clones of a snapshot. Some file systems, such as Btrfs and ZFS, use copy-on-write by default and normally write changed data to new blocks rather than in place.

## Snapshots and clones

Copy-on-write snapshots and clones are copies of a disk or a machine that share data with their source instead of duplicating it. A snapshot only marks the current blocks as shared, so it takes about as long for a large disk as for a small one. Later writes go to new blocks, and the snapshot keeps the old ones. A clone made from the snapshot starts by sharing every block and grows only as it writes.

Cloning a running sandbox applies the same idea to memory as well as disk. A saved memory image can be mapped privately, so pages load on first read and writes stay with the clone that made them. Many clones of one prepared sandbox then share one copy of its memory and disk. They start far faster than booting each one and installing its tools again.

## Performance

Copy-on-write performance is the cost that shifts from making a copy to writing to it. The first write to each shared piece must copy it before changing it, so it takes longer than later writes. With qcow2, a small write to an unwritten cluster may copy the whole cluster from the backing file, 64 KiB by default. Overlay file systems copy a whole file up on its first change, so editing one byte of a large file can be slow.

Write amplification is the extra data written beyond what the program asked for. Copy-on-write file systems also rewrite the metadata that points to each changed block, up through the tree to its root. Fragmentation follows, since a file rewritten in small pieces ends up scattered across the disk. Btrfs lets single files opt out of copy-on-write for this reason, which suits virtual machine images and databases.

Long chains of layers slow reads, because a read may check every layer down to the base. Storage systems therefore merge layers from time to time, folding a chain back into one image. Shared pages also make memory use hard to predict, since any write can suddenly need a new page. A host that [overcommits](https://www.daytona.com/compute-glossary/infrastructure/overcommit.md) memory on shared pages has to keep spare room for those copies.

## Use cases

Copy-on-write makes copies cheap to create and charges only for what each copy changes.

- **Process forking.** After fork, parent and child share their private memory pages until one of them writes. Only the pages that change are copied, so a fork costs little at first.

- **Shared sandbox images.** Many sandboxes on one host can start from one base image. Each stores its changes in its own image or writable layer, and the base stays untouched.

- **Fast disk snapshots.** A copy-on-write snapshot only marks the current blocks as shared. It takes about as long for a large disk as for a small one.

- **Cloning sandboxes.** A saved memory image can be mapped so that pages load on first read and writes stay with each clone. Many clones of one prepared sandbox then share one copy of its memory and disk.

## Related terms

- [Memory page](https://www.daytona.com/compute-glossary/kernel/memory-page.md)
- [Process](https://www.daytona.com/compute-glossary/kernel/process.md)
- [Snapshot](https://www.daytona.com/compute-glossary/sandboxes/snapshot.md)
- [File system](https://www.daytona.com/compute-glossary/storage/file-system.md)
- [Block storage](https://www.daytona.com/compute-glossary/storage/block-storage.md)
- [Container](https://www.daytona.com/compute-glossary/virtualization/container.md)
