Copy-on-Write
Subject: Computer and Technology noun /ˈkɑː.pi ɑːn ˌraɪt/ 1 definition
- operating-systems
- memory-management
- file-systems
- data-structures
-
Definition (noun)
Entry #202 · UpdatedCopy-on-Write (COW) is a resource-management technique in which multiple consumers share references to the same underlying data instead of receiving immediate duplicates when a copy is requested. Read operations access the shared instance with zero duplication overhead, and only when a consumer attempts a modification does the system intercept the write, allocate a private copy of the affected block or object, and apply the change there so other consumers keep seeing the original state. By deferring duplication until an actual write occurs, COW reduces memory usage and upfront copy latency while preserving data isolation, though it requires reference tracking and shifts allocation cost to the first write.
Common Forms
Virtual Memory Paging: Operating systems mark shared memory pages read-only in page tables after operations like
fork(), triggering a page fault on write that allocates and maps a private writable frame only for the modifying process.Copy-on-Write Filesystems: Filesystems such as ZFS, Btrfs, and APFS write updated file blocks to unallocated storage locations rather than overwriting in place, preserving prior block trees for atomic updates and near-instantaneous snapshots.
Container and Disk Overlays: Storage drivers like OverlayFS and virtual disk formats like QCOW2 pair a read-only base image with a writable upper layer, copying a file or block into the writable layer only when an instance modifies it.
Value-Semantic Data Structures: Languages and libraries such as Swift's standard collections share a single heap buffer across copied values and duplicate that buffer only when a mutating operation observes more than one active reference.
Example
When a Redis server with a 16 GB in-memory dataset runs
BGSAVE, it callsfork()so the child snapshot process shares the parent's physical RAM pages marked read-only instead of duplicating all 16 GB upfront. As the parent continues handling client updates, each write to a shared 4 KB page raises a page fault that prompts the kernel to copy only that modified page, isolating the child's point-in-time view without copying unmodified memory.