This article explains how you can perform Point-in-time-Recovery (PITR) in Valkey/Redis.
Requirements
To perform PITR, you need to have append-only logging enabled.
By default, AOF in Valkey/Redis only records the operations that have been executed against the instance, not when they were executed. For that, we need to enable the aof-timestamp-enabled parameter.
So your Valkey/Redis instance needs to have the following parameters:
|
1 2 3 |
appendonly yes appendfilename "appendonly.aof" aof-timestamp-enabled yes |
A little refresher on append-only logging in Valkey/Redis
AOF persistence logs every write operation received by the server. These operations can then be replayed again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Valkey protocol itself.
Valkey/Redis writes the commands to AOF files in plaintext, so if you only need to remove an accidental command (i.e., FLUSHDB), or there are corrupted commands in the append-only logs, you can simply open the AOF file and fix/delete it.
A more comprehensive documentation on Valkey/Redis serialization format can be found on the official Valkey documentation.
Determine the timestamps you can restore to
So, because AOF will be rewritten when its size reaches the value defined by auto-aof-rewrite-*, if you do not have backups of the append-only logs available, you can only restore up to the point when AOF rewrite started. While the auto rewrite process can be disabled (by setting auto-aof-rewrite-percentage to 0), if you opt to do so, you will need to monitor the server’s storage closely and take action to ensure it is not completely filled.
You can identify the available restore points by grepping the AOF file for the string #TS:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
# grep '#TS' appendonly.aof.1.incr.aof #TS:1764918042 #TS:1764918195 #TS:1764918200 #TS:1764918201 #TS:1764918207 #TS:1764918214 #TS:1764918216 #TS:1764918220 #TS:1764918221 #TS:1764918222 #TS:1764918330 |
To quickly convert the timestamps in AOF files into human-readable dates, we can use the commands below
- Lines in AOF files are terminated by CRLF, so you might have difficulty parsing the timestamps if you write a script for it. To solve that, we can just remove the line terminator using tr:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# grep '#TS' appendonly.aof.1.incr.aof | sed 's|#TS:||g' |tr -d 'r' | while read -r ts do echo "#TS:${ts} = $(date -d "@$ts" +"%Y-%m-%d %H:%M:%S")" done #TS:1764918042 = 2025-12-05 07:00:42 #TS:1764918195 = 2025-12-05 07:03:15 #TS:1764918200 = 2025-12-05 07:03:20 #TS:1764918201 = 2025-12-05 07:03:21 #TS:1764918207 = 2025-12-05 07:03:27 #TS:1764918214 = 2025-12-05 07:03:34 #TS:1764918216 = 2025-12-05 07:03:36 #TS:1764918220 = 2025-12-05 07:03:40 #TS:1764918221 = 2025-12-05 07:03:41 #TS:1764918222 = 2025-12-05 07:03:42 #TS:1764918330 = 2025-12-05 07:05:30 |
Truncate the AOF to your chosen timestamp
After identifying the timestamp you wish to restore to, we can use the command valkey-check-aof to truncate the AOF file to that point. For example, if I wanted to restore to the timestamp 1764918201 (2025-12-05 07:03:21):
|
1 2 3 |
# valkey-check-aof --truncate-to-timestamp 1764918201 appendonly.aof.1.incr.aof Start checking Old-Style AOF Successfully truncated AOF appendonly.aof.1.incr.aof to timestamp 1764918201 |
We can then check the result by grepping the AOF file again for timestamps
|
1 2 3 4 5 |
grep '#TS' appendonly.aof.1.incr.aof #TS:1764918042 #TS:1764918195 #TS:1764918200 #TS:1764918201 |
Start the service
After truncating, we can start the service as normal, and confirm that valkey-server can read the truncated AOF file by viewing its log:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
93048:M 05 Dec 2025 07:38:35.798 * Server initialized 93048:M 05 Dec 2025 07:38:35.798 * Reading RDB base file on AOF loading... 93048:M 05 Dec 2025 07:38:35.798 * Loading RDB produced by Valkey version 8.1.4 93048:M 05 Dec 2025 07:38:35.798 * RDB age 2292 seconds 93048:M 05 Dec 2025 07:38:35.798 * RDB memory usage when created 0.87 Mb 93048:M 05 Dec 2025 07:38:35.798 * RDB is base AOF 93048:M 05 Dec 2025 07:38:35.798 * Done loading RDB, keys loaded: 0, keys expired: 0. 93048:M 05 Dec 2025 07:38:35.798 * DB loaded from base file appendonly.aof.1.base.rdb: 0.000 seconds 93048:M 05 Dec 2025 07:38:35.800 * DB loaded from incr file appendonly.aof.1.incr.aof: 0.002 seconds 93048:M 05 Dec 2025 07:38:35.800 * DB loaded from append only file: 0.002 seconds 93048:M 05 Dec 2025 07:38:35.800 * Opening AOF incr file appendonly.aof.1.incr.aof on server start 93048:M 05 Dec 2025 07:38:35.800 * Ready to accept connections tcp |
Conclusion
Valkey/Redis Point-in-Time Recovery (PITR) is achieved exclusively through the Append Only File (AOF) mechanism with timestamping enabled.
The final process is:
- Select a timestamp embedded within the AOF using valkey-check-aof to truncate the AOF file precisely to the chosen time
- Then, restart the service to load the truncated AOF, which restores the database state to the specified point in time.