fork() on a 2 GB process returns in under a millisecond, and what you pay for it later.
Several processes share one instance of the data instead of each getting a private copy. The duplicate is created at the exact moment a process tries to modify it — not a moment sooner.
Underneath the friendly word “copy”: only the page table is duplicated, not the pages. Both processes point at the same physical frames, every shared page is marked read-only, and the copy is instant regardless of size. You pay per page touched, not per byte owned.
Two sets of virtual → physical mappings over one set of physical frames. Cheap, small, O(mappings) — not O(bytes).
Every shared page loses its write permission. That flag is the whole enforcement mechanism — there is no bookkeeping thread watching you.
The CPU traps the write instruction mid-flight and hands control to the kernel. The instruction has not committed yet, so it can be resumed.
The kernel copies a single page, flips it writable for the writer only, and leaves everyone else on the original. Cost scales with pages written.
fork(). Duplicating a 2 GB process returns in well under a millisecond, because nothing was actually copied yet.The cost is deferred, not removed, and it arrives as latency at the worst possible moment.
A Redis instance that forks to save while write traffic is heavy can end up copying nearly every page anyway. Peak memory approaches double the dataset and the host starts swapping — the “free” copy becomes the most expensive operation of the day.
The experiment: fork under a write-heavy load and watch resident set size climb, rather than assuming the copy stayed free. If RSS tracks toward 2× your dataset, you have found the deferred bill.
fork() and returns in under a millisecond. What was actually duplicated, and what was not?
Hint: name the structure that was copied, and what makes the pages themselves safe to share.