Records of command executions and key changes are stored in append-only files (AOFs). You can parse AOFs to track these records.
Redis persistence modes
- Redis Database (RDB) snapshot mode: This mode creates point-in-time snapshots of your dataset at specified intervals. Keys and values are encoded as Redis strings and stored in RDB snapshots.
- AOF persistence mode: Similar to the binlog, AOFs keep a record of data changes that occur by writing each change to the end of the file. You can restore the entire dataset by replaying the AOF from the beginning to the end.
Details of the AOF persistence mode
A Redis client communicates with the Redis server through a protocol called REdis Serialization Protocol (RESP). RESP can serialize the following types of data:
- Simple strings:
A string that starts with a plus sign (+) and ends with rn. Example: +OKrn.
- Error messages:
A string that starts with a minus sign (-) and ends with rn. Example: -ERR Readonlyrn.
A data structure that starts with a colon (:), ends with rn, and contains an integer between the beginning and the end. Example: (:1rn).
- Large strings
A string structure that starts with a dollar sign ($)followed by the string length (less than 512 MB) and rn, and ends with the string content and rn. Example: $0rnrn.
A data structure that starts with an asterisk symbol (*), followed by array elements that are separated by rn. The above four data types can be used as array elements. Example: *1rn$4rnpingrn.
The Redis client sends an array command to the server. The server responds based on the implementation method of the command and records the responses in the AOF.
The following example shows how to parse an AOF by invoking hiredis with Python:
#! /usr/bin/env python """ A Redis appendonly file parser """ import logging import hiredis import sys if len(sys.argv) ! = 2: print sys.argv, 'AOF_file' sys.exit() file = open(sys.argv) line = file.readline() cur_request = line while line: req_reader = hiredis.Reader() req_reader.setmaxbuf(0) req_reader.feed(cur_request) command = req_reader.gets() try: if command is not False: print command cur_request = '' except hiredis.ProtocolError: print 'protocol error' line = file.readline() cur_request += line file.close
The AOF is parsed into the following format, where you can check the operations performed on a specific key. After you obtain the following results, you can view the operations related to a specific key at any time.
['PEXPIREAT', 'RedisTestLog', '1479541381558'] ['SET', 'RedisTestLog', '39124268'] ['PEXPIREAT', 'RedisTestLog', '1479973381559'] ['HSET', 'RedisTestLogHash', 'RedisHashField', '16'] ['PEXPIREAT', 'RedisTestLogHash', '1479973381561'] ['SET', 'RedisTestLogString', '79146']