How to parse Redis AOF file?
Asked Answered
D

2

6

I am trying to understand how Redis AOF file works and maybe write a parser given some simple Redis AOF file. Right now I generated an AOF file by doing these commands in Redis:

SET firstkey firstvalue
SET secondkey secondvalue

and the generated AOF file looks like this:

*2
$6
SELECT
$1
0
*3
$3
SET
$8
firstkey
$10
firstvalue
*3
$3
SET
$9
secondkey
$11
secondvalue

I can see the keywords like firstkey, firstvalue and SET, etc. But I didn't quite understand the rest, espcially what all these numbers like *2, $6 means, and how they work when redis trying to read the aof file and rebuild the database. I couldn't find any file format document online either, so any help is appreciated!

Dorking answered 24/3, 2019 at 18:39 Comment(1)
Interesting question. Had a look at the source already? github.com/antirez/redis/blob/unstable/src/aof.cLallage
D
7

*N is the number of arguments of the command, and $M is the length, i.e. the number of bytes, of each argument.

In your case, Redis executed 3 commands: SELECT 0, SET firstkey firstvalue and SET secondkey secondvalue.

SELECT 0 command has 2 arguments, i.e. SELECT and 0. The length of the first argument is 6, and the length of the second argument is 1. So AOF file records:

*2\r\n$6\r\nSELECT\r\n$1\r\n0\r\n

You can try the other 2 commands for practice.

Discoloration answered 25/3, 2019 at 2:23 Comment(1)
For more information, take a look at the Redis serialization protocol (RESP) specification redis.io/docs/reference/protocol-specFormality
P
0

I had to parse the AOF to recover a deleted sorted list, and this Python script was incredibly helpful! It essentially translates the AOF into a human-readable format which is quite easy to parse.

Pax answered 2/8, 2024 at 7:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.