Persistence — fork, CoW, and the 1-second window
RDB forks a child that writes the dataset via copy-on-write, and AOF's everysec fsync trades up to one second of writes for throughput — together they define what 'durable Redis' actually means.
You've seen what's in RAM and how it's encoded. None of it survives a crash on its own — persistence is how the live keyspace becomes bytes on disk.
Scene 04
Persistence — fork, CoW, and the 1-second window
- Watch
- Try it
- Predict
- Capture
Watch writes land on the AOF tail (amber = buffered) and flip emerald when the everysec fsync fires. After a few seconds, BGSAVE forks a child — pages start flipping orange one by one as the parent keeps writing during the snapshot.
Highlighted lines are the ones running in the diagram right now.
def bgsave():pid = fork()if pid == 0:# child: frozen view of memory at fork timewriteRDB(snapshot=heap_snapshot())exit(0)# parent returns immediately; serves clients# while pages diverge via CoW on each write.return pid
def handleWrite(cmd):apply(cmd, db) # in-memory mutationaof_buf.append(cmd) # OS page-cache writeif cfg.appendfsync == 'always':fsync(aof_fd) # block until on diskreply_ok(client) # ack returns to caller
def aofFlushBackground():last_fsync = now()while running:if now() - last_fsync >= 1.0:fsync(aof_fd)last_fsync = now()sleep(0.1)
def pageWrite(page):if page.shared:new = copy_page(page) # 4 KB, or 2 MB under THPpage.shared = Falsemark_copied(page, new)write_into(page)
Where this sits in Build Redis
Scene 04 of 10, in the Memory act — Persistence, eviction, TTL — what makes RAM disappear.. RDB snapshots via fork+CoW, AOF fsync policies, and why default Redis can lose ~1s of writes on crash.
Up next. Eviction — persistence handles the crash. But while the process is running and RAM fills up, who has to leave to make room for the next write?
Designs that use this
- URL ShortenerShorten a long URL. Read-heavy. Don't collide.
- Distributed Rate LimiterEnforce a per-key request limit across a fleet of enforcers — accurately, in under a millisecond, without becoming the outage.
- Twitter / X TimelinePush or pull? Both. The canonical fanout problem.
- Uber / Lyft — Match Drivers and RidersMatch a rider to the closest acceptable driver in under 3 s. Geohash, S2, surge.