CodePark/ questions / Persistence (RDB & AOF)
START PRACTICINGPRACTICE
Backend question · Redis & caching › Persistence (RDB & AOF)

What is AOF rewriting and why is it needed?

AOF rewriting replaces the long AOF log with the shortest set of commands that rebuilds the current data. It is needed because the AOF grows with every write.

For example, a counter incremented a million times creates a million INCR entries. But its final state is just one value. The rewrite stores that one value instead.

How Redis does it without blocking:

  • Redis forks: it creates a child process with a copy of memory.
  • The child writes the new base file from memory.
  • Meanwhile, the parent keeps serving clients and logging new writes.

Since Redis 7, the AOF is multi-part. It has a base file (in RDB format by default) plus incremental AOF files. A manifest file keeps track of them. During a rewrite, new writes go to a fresh incremental file.

Rewrites start automatically, based on auto-aof-rewrite-percentage and auto-aof-rewrite-min-size. You can also start one manually with BGREWRITEAOF.

  • The AOF log grows with redundant commands.
  • A rewrite writes the fewest commands needed for the current state.
  • A forked child does the rewrite, so clients are not blocked.
  • Redis 7 uses a base file, incremental files and a manifest.
  • Rewrites start at automatic thresholds or with BGREWRITEAOF.