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!
*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.