Redis runs on memory, but the memory capcity is not unlimited. So there will be insufficient memory.
To resolve this problem, Redis has 3 Data Elimination Strategy.
- RDB: Redis Database File (Default).
- AOF: Append Only File.
- volatile-lru : Delete a L east R ecently U sed key from when memory is deficient.
- volatile-lfu : Delete a L east F requently U sed key from when memory is deficient.
- volatile-random : Delete a key Randomly from when memory is deficient.
- volatile-ttl : Delete the key with the nearest expire time (Minor TTL).
- allkeys-lru : Delete a L east R ecently U sed key from .
- allkeys-lfu : Delete a L east F requently U sed key from .
- allkeys-random : Delete a key Randomly from .
- noeviction : Just on write operations when memory is deficient.
RDB is a default memchanism for Redis persistence. It is enable by default. This is a snapshot method.
.rdb file saves is .
graph LR A[Memory]; B[Disk]; A --> |rdbSave| B; B --> |rdbLoad| A;
When it is enable, Redis will dump the data to the
.rdb file automatically by the configured strategy.
- RDB is a file used to persist data. It is a binary file and is easy to transmit.
But RDB can not ensure the absolute security of data due to the time interval of writing.
If the time interval is too short, efficiency declines.
If the time interval is too long, if there is an accident before the next interval, the data written from last write to the accident will be lost.
There are 2 ways to trigger RDB updates:
You can configure the strategy of time interval in the
You can actively send the command
BGSAVE to trigger updates.
SAVE command will block current process, update data, and then resume.
BGSAVE command will not block current process. It calls the
fork() (a linux system call) to create the child process.
the Automatic way is a BGSAVE.
RDB File structure¶
RDB file consists of 5 base parts:
- Magic number: It is 5 characters --"REDIS", used to indicate this is a Redis file. 5 bytes.
- Version: It means the redis version. 4 bytes.
- Data: If there is no data, it is 0 byte.
- EOF: A common end-of-file symbol, used to indicate the end of file. 1 byte.
- Checksum: It is a 8 bytes unsigned integer, generated by digest algorithm, used to check whether the file is complete.
The part of data is consists of:
Database number and
- SELECTDB symbol: It is a constant. 1 byte. When the reading program read this value, it will know the next section is a database num.
- Database number: Depending on the size of number, the length of this section can be 1, 2, or 5 bytes. When the reading program read this value, it will call the
SELECTcommand to switch current database.
- Key-Value pairs: This part is the real data.
The part of Key-Value pairs is consists of:
Value. If the expiration time is set, there will include the
EXPIRE_MS symbol and
TYPE: This records the type of value, it can be one of the following constant:
Each constant represents an underlying encoding. Where redis-server reading key-value pairs, how to read and interpre the value depending on TYPE symbol.
Key: Key is always a string object and is encoded the same way. The length of key varies depending on the length of the content.
Value: This is the place where the real data in.
- EXPIRE_MS symbol: It tell the reading program the next section is an expiration time in milliseconds. 1 byte.
- Timestamp: This is an 8-byte lone signed integer. It records a UNIX timestamp.
The RDB file structure look like this:
AOF means Append Only File.
.aof file saves is .
Note that AOF only stores WRITE instructions such as
READ instructions, such as
HGET, LRANGE, will not be stores.
- For data security, AOF mode is safer than RDB mode.
- BTW, Redis official recommends to enable .
- AOF mode is a higher priority than RDB mode. After Redis goes down and restart, AOF file will be perferred to recover data.
AOF is not the default memchanism, you can configure it in the