Databases 4 min read

How Redis AOF Rewrite Shrinks Log Files and Keeps Data Consistent

Redis’s AOF persistence logs every write command, causing the log file to grow, but the AOF rewrite feature creates a compact new file by replaying the current dataset, runs in a child process, and uses a buffer to avoid blocking and ensure data consistency.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How Redis AOF Rewrite Shrinks Log Files and Keeps Data Consistent

Redis’s Append‑Only File (AOF) persistence records every write command, so the AOF file continuously grows. If unchecked, a large AOF file can degrade Redis performance and increase recovery time.

Example commands:

RPUSH list "A" "B"
RPUSH list "C"
RPUSH list "D" "E"
LPOP list
LPOP list

Some of these commands become redundant because they do not affect the final database state.

To combat AOF bloat, Redis provides an AOF rewrite (or rewrite ) feature that creates a new, compact AOF file containing only the minimal set of commands needed to reconstruct the current dataset.

For the list example, the five original commands can be replaced by a single command: RPUSH list "C" "D" "E" The rewrite works by reading the current value of each key from the database and writing a single command that reproduces that value.

Because rewriting involves heavy I/O, Redis performs it in a child process, allowing the parent process to continue serving client requests.

During the rewrite, new write commands are stored in an AOF rewrite buffer . When the child finishes, it signals the parent, which then:

Appends the buffered commands to the new AOF file, ensuring the file reflects the latest state.

Atomically renames the new file over the old one, completing the replacement.

Only the brief signal‑handling phase blocks the server, minimizing impact on availability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databasePersistenceAOF
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.