0
0
RedisHow-ToBeginner · 3 min read

How to Use BGREWRITEAOF Command in Redis for AOF Rewrite

Use the BGREWRITEAOF command in Redis to asynchronously rewrite the Append Only File (AOF) without blocking the server. This helps reduce the AOF file size and improves Redis persistence performance while the server continues to serve requests.
📐

Syntax

The BGREWRITEAOF command has a simple syntax with no arguments. It triggers an asynchronous rewrite of the Append Only File (AOF) in the background.

  • BGREWRITEAOF: Starts the background AOF rewrite process.
redis
BGREWRITEAOF
💻

Example

This example shows how to run the BGREWRITEAOF command in the Redis CLI. It starts the background rewrite of the AOF file, allowing Redis to continue serving clients without blocking.

redis
127.0.0.1:6379> BGREWRITEAOF
OK

# After some time, Redis logs will show the rewrite completion message.
Output
OK
⚠️

Common Pitfalls

Common mistakes when using BGREWRITEAOF include:

  • Running BGREWRITEAOF while a rewrite is already in progress, which will return an error.
  • Expecting the command to block Redis; it runs asynchronously in the background.
  • Not monitoring Redis logs to confirm successful rewrite completion.

Always check Redis logs or use INFO persistence to verify the rewrite status.

redis
127.0.0.1:6379> BGREWRITEAOF
(error) Background append only file rewriting already in progress
Output
(error) Background append only file rewriting already in progress
📊

Quick Reference

CommandDescription
BGREWRITEAOFStarts asynchronous rewrite of the Append Only File
INFO persistenceShows persistence status including AOF rewrite progress
LASTSAVEReturns the last time the DB was saved to disk

Key Takeaways

Use BGREWRITEAOF to rewrite the AOF file without blocking Redis.
BGREWRITEAOF runs asynchronously and returns immediately with OK.
Do not run BGREWRITEAOF if a rewrite is already in progress to avoid errors.
Check Redis logs or INFO persistence to monitor rewrite status.
Regular AOF rewrites help keep the AOF file size manageable and improve performance.