Error
Error Code: AOFLOAD

Fix Redis Error AOFLOAD: AOF File Corruption

📦 Redis
📋

Description

The `AOFLOAD` error in Redis indicates a problem reading the append-only file (AOF) during startup or AOF rewriting. This usually means the AOF file is corrupted or has an invalid format, preventing Redis from loading persisted data.
💬

Error Message

ERR Bad file format reading the append only file
🔍

Known Causes

4 known causes
⚠️
File Corruption
The AOF file may have been corrupted due to unexpected system shutdowns, disk errors, or incomplete writes. This results in an invalid file format that Redis cannot parse.
⚠️
Disk Space Issues
Insufficient disk space during AOF rewriting can lead to incomplete or truncated AOF files. This leads to a corrupted file format and the AOFLOAD error.
⚠️
Software Bugs
Rarely, bugs in Redis or the underlying operating system can cause AOF file corruption during writing or rewriting operations.
⚠️
Manual Editing
Accidental or intentional manual modifications to the AOF file can introduce invalid syntax or formatting, leading to parsing errors.
🛠️

Solutions

4 solutions available

1. Restore from a Redis Snapshot (RDB) easy

If you have a Redis snapshot (RDB) file, you can use it to restore your data instead of loading the corrupted AOF file. This is the safest and quickest method if an RDB backup exists.

1
Stop the Redis server if it is running.
redis-cli shutdown
2
Locate your RDB file. By default, it's named `dump.rdb` and is usually in the Redis working directory.
3
Locate your AOF file (e.g., `appendonly.aof`). Rename or delete it to prevent Redis from trying to load it.
mv appendonly.aof appendonly.aof.backup
4
Ensure your `redis.conf` file is configured to use the RDB file for persistence. Check the `dbfilename` directive.
dbfilename dump.rdb
5
Start the Redis server. It will load data from the RDB file.
redis-server /path/to/your/redis.conf

2. Use `redis-check-aof` to Fix the AOF File medium

Redis provides a utility called `redis-check-aof` that can attempt to repair a corrupted AOF file by removing invalid entries. This is a common solution when an RDB backup is not available.

1
Stop the Redis server if it is running.
redis-cli shutdown
2
Locate your AOF file (e.g., `appendonly.aof`). Make a backup of the corrupted file before proceeding.
cp appendonly.aof appendonly.aof.corrupted
3
Run `redis-check-aof` on the corrupted AOF file. The `--fix` option attempts to repair the file.
redis-check-aof --fix appendonly.aof
4
If `redis-check-aof` reports successful repair, you can try starting Redis with the fixed AOF file.
redis-server /path/to/your/redis.conf
5
If the fix is successful, Redis should start without the `AOFLOAD` error. You may have lost some commands that were corrupted.

3. Manually Edit the AOF File (Advanced) advanced

This is a last resort and requires caution. It involves manually inspecting and editing the AOF file to remove or correct malformed commands. Data loss is highly probable if not done correctly.

1
Stop the Redis server.
redis-cli shutdown
2
Make a full backup of your AOF file.
cp appendonly.aof appendonly.aof.master_backup
3
Open the AOF file in a text editor (e.g., `vim`, `nano`). AOF files are human-readable but contain specific Redis command formats.
vim appendonly.aof
4
Look for lines that appear malformed or are associated with the error message's likely point of failure. Redis commands typically start with `*` for bulk strings, `$` for string lengths, and then the command and its arguments. Errors often occur at the end of a command or an incomplete command.
5
Carefully delete or comment out suspected corrupted lines. Be extremely cautious not to delete valid commands. If you are unsure, it's better to err on the side of caution and attempt to fix it by removing a smaller section.
6
Save the modified AOF file.
7
Attempt to start Redis. If it fails, revert to your backup and try a more conservative removal of lines.
redis-server /path/to/your/redis.conf
8
If Redis starts, verify the integrity of your data. You will likely have lost the commands that were deleted.

4. Rebuild AOF from Redis Cluster (if applicable) advanced

If you are running a Redis cluster and have a replica that is not experiencing AOF corruption, you can potentially promote a replica and rebuild the AOF from its state.

1
Identify a healthy replica node in your Redis cluster that is not showing AOF corruption.
2
Stop the master node experiencing the `AOFLOAD` error.
redis-cli -h <master_ip> -p <master_port> shutdown
3
Promote the healthy replica to become the new master. This can be done using `redis-cli CLUSTER FAILOVER` on the replica or by manual configuration if using older Redis versions.
redis-cli -h <replica_ip> -p <replica_port> cluster failover
4
Once the replica is the new master, it will start writing its own AOF file. If the replica's AOF is healthy, Redis will load it correctly.
5
Reconfigure other nodes to point to the new master. You may need to re-add nodes to the cluster.
6
This method ensures a consistent dataset is loaded, but it requires a properly functioning cluster setup.