AOF persistence stores commands in a file as raw text and executes them one by one on reboot.

AOF storage structure

For example, set MSG hello:

  1. The first thing I write is 3, and then append \r\n
  2. Iterate over each character, first writing the $sign, then noting the length of the character, appending \r\n then noting the length and the character itself, then appending \r\n
  3. Set MSG hello will append to3\r\n$3\r\nmsg\r\n$5\r\nhello\r\n

AOF file size problem

The AOF file itself records a lot of commands, but over time, many of the commands may be repeated. Redis can simplify the AOF file by manually bgrewriteAOF or configuring automatic AOF overwriting

AOF related source tracking

AOF storage format

Code.SLICE.source("buf[0] = '*';" +
" len = 1+ll2string(buf+1,sizeof(buf)-1,argc);" +
" buf[len++] = '\r';" +
" buf[len++] = '\n';" +
" dst = sdscatlen(dst,buf,len);" +
" for (j = 0; j < argc; j++) {" +
" o = getDecodedObject(argv[j]);" +
" buf[0] = '$';" +
" len = 1+ll2string(buf+1,sizeof(buf)-1,sdslen(o->ptr));" +
" buf[len++] = '\r';" +
" buf[len++] = '\n';" +
" dst = sdscatlen(dst,buf,len);" +
" dst = sdscatlen(dst,o->ptr,sdslen(o->ptr));" +
" dst = sdscatlen(dst,\"\r\n\",2);" +
" decrRefCount(o);" +
"}")
.interpretation("Convert commands in a certain format to the destination to be stored.")
.interpretation("1: count the number of characters in the command, such as set MSG hello, then write 3, then append \r\n")
.interpretation("2: Iterate over each character, first writing the $sign, then noting the length of the character, append \r\n then noting the length and the character itself, then append \r\n")
.interpretation("3: set MSG hello to append 3\r\n$3\r\nmsg\r\nA $5\r\nhello\r\n");
Copy the code

Start loading

/ /.. Code.SLICE.source("while(1) ")
    .interpretation("Read the contents of the AOF file according to the REPL format, 1 command processing"); / /.. Code.SLICE.source("if (fgets(buf,sizeof(buf),fp) == NULL)")
    .interpretation("Read a byte from a file into a BUF"); / /.. Code.SLICE.source("argc = atoi(buf+1);")
    .interpretation("Get the length of the order."); / /.. Code.SLICE.source("for (j = 0; j < argc; j++) ")
    .interpretation("Parses all the data of this command one by one, and checks the data written in between."); / /.. Code.SLICE.source("argv[j] = createObject(OBJ_STRING,argsds);")
    .interpretation("Store data in argV array"); / /.. Code.SLICE.source("cmd = lookupCommand(argv[0]->ptr);")
    .interpretation("Make sure the command to be executed is a valid Redis command."); / /.. Code.SLICE.source("cmd->proc(fakeClient);")
    .interpretation("Simulated execution");
Copy the code

The advantage of AOF

Real-time persistence can be achieved at the second level

conclusion

  1. After each command is executed, it is appended to the memory according to the original text of the command.
  2. Redis periodically writes data to the disk according to the configuration
  3. Rewrite is written to a new temporary file and redirected to an AOF file in serverCron. The rewrite process is asynchronous
  4. Loading AOF means executing all commands one by one

The appendix

AOF and timing to write files source tracking AOF rewrite source tracking start loading source tracking Redis design and implementation of Redis development and operation