Overview
Copy-on-write (COW) is an optimization strategy that lets multiple consumers share a single instance of a resource (memory pages, file blocks, object data) until one consumer attempts to modify it. Instead of eagerly duplicating the resource for each user, the system gives all users references to the same data. Only when a write is performed does the system create a private copy for the writer, so other users continue to see the original unchanged content.
How it works
At a technical level COW typically relies on reference counting, access permissions, or read-only mappings. In virtual memory, for example, pages are marked read-only and mapped into each process. A write triggers a page fault; the operating system then allocates a new page and copies the content, updating the writing process to use the new page. In higher-level code, a shared object maintains a counter of owners and clones its contents when a mutating operation occurs.
Common uses and examples
- Process creation: Traditional UNIX fork() implementations use COW so the child and parent share pages until one writes.
- File systems and snapshots: Modern file systems (examples include snapshot-capable designs) use COW to create efficient snapshots and clones without instantly copying all data.
- Containers and layered images: Container storage drivers implement layered filesystems with COW behavior so images can share common layers and only write unique changes.
- Data structures and libraries: Some libraries and language runtimes apply COW to strings, arrays, or buffers to reduce copies when values are duplicated but seldom mutated.
Benefits and trade-offs
COW can greatly reduce memory use and I/O, improve performance for read-heavy workloads, and enable fast snapshots and cloning. The trade-offs include added implementation complexity, runtime overhead for maintaining counts or handling faults, and possible performance penalties if writes are frequent (which forces many copies). Concurrency also complicates correct and efficient COW implementations.
History and notable facts
Copy-on-write concepts have long been part of operating systems and have evolved into critical features of modern storage and virtualization stacks. While widely beneficial for read-dominant scenarios, some language and library maintainers have moved away from COW for simple types where thread-safety and predictable performance are more important. In contrast, COW remains foundational for features such as snapshotting, cloning virtual disks, and copy-efficient forking.