Back to Knowledge Base
storageNODE: #cow-filesystem

Copy-on-Write (CoW) Filesystems

A storage design pattern where modified data blocks are written to newly allocated blocks rather than overwriting existing data in place.

AKA:Copy-on-Write, CoW, Redirect-on-Write
#storage#filesystems#cow#architecture#zfs#btrfs
GRAPH_RELATIONS // CONNECTED_NODESLINKED

Copy-on-Write (CoW) Filesystems

Overview

Copy-on-Write (CoW) is a storage management technique where existing data blocks are never mutated in place. When data is modified:

  1. The filesystem allocates new unallocated blocks on disk.
  2. The modified data is written to these new blocks.
  3. The parent metadata pointers (e.g., uberblock, merkle tree nodes) are updated atomically to point to the new location.
  4. The old blocks are marked free only if not referenced by an active snapshot.

Key Advantages of CoW

  • Crash Consistency & Atomic Updates: No half-written or corrupted metadata states on unexpected power failure. Eliminates the need for traditional journal recovery (fsck).
  • Instant, Zero-Cost Snapshots: Taking a snapshot simply preserves the current root pointer; no data is duplicated until blocks are modified.
  • Efficient Deduplication & Clones: Multiple datasets or VM images can share common base blocks indefinitely.

Trade-offs & Considerations

  • Write Amplification: Small random writes cause parent metadata tree nodes to be rewritten all the way up to the root.
  • Fragmentation: Free-space fragmentation can increase over time with random write workloads (e.g., databases or VM disk images).
  • Out-of-Space Deadlocks: CoW systems require free space to delete files (because modifying the directory tree requires new writes).

Major CoW Filesystems

  • ZFS (OpenZFS)
  • Btrfs
  • APFS (Apple File System)
  • Bcachefs