[database] redis (4) — advanced redis configuration and persistence


1. Redis.conf configuration file description

The redis configuration file involves some important parameters of redis startup and operation, and it is also an important file related to redis sentinel and redis cluster configuration. It is necessary to be familiar with the configuration of redis.config file.

Here is a brief description of the main configurations in redis.conf:

① Include: include

Similar to spring configuration files, you can include other configuration files through includes, and redis.conf is the general file


② Network: network configuration


Common configurations are modified as follows:

Bind # modify the bound IP
Protected mode yes # is the protected mode. The default value is yes
Port 6379 # default port modification


③ General: general configuration


The main common configurations are modified as follows:

Daemon yes # runs as a daemon. It defaults to no and needs to be manually modified to yes
pidfile /var/run/redis_ 6379.pid # if running in background mode, you need to specify a PID file # log level configuration

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)   # Debug, used in the test development phase
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)   # Notice, which is used in the production environment by default
# warning (only very important / critical messages are logged)   # Warning level
loglevel notice

logfile “”   # Configure the generated file name and modify the storage address information

databases 16   # The number of configuration databases is 16 by default

always-show-logo yes    # The logo information is displayed by default


④ Snapshooting snapshot:

It is mainly used for persistence operations. The number of operations performed within a specified time will be persisted to the. RDB file or. AOF file.

Redis is an in memory database. If there is no persistence configuration, the breakpoint will be lost.


#If at least one key is modified within 900s, the persistence operation is performed
save 900 1    
#If at least 10 keys are modified within 300s, the persistence operation is performed
save 300 10   
#If at least 10000 keys are modified within 60s, the persistence operation is performed
save 60 10000  
#If persistence fails, do you need redis to continue working
stop-writes-on-bgsave-error yes
#Whether to compress RDB files requires some CPU resources
rdbcompression yes
#Error checking when saving RDB files
rdbchecksum yes
#The directory where RDB files are saved. The default directory is the current directory
dir ./


⑤ Security security:

You can set the password for redis login, generally using the command line command: > config get requirepass # get the password of redis
1) "requirepass"
2) "" > config set requirepass "123456" # set the password of redis
OK> config get requirepass 
(error) NOAUTH Authentication required. > auth 123456 # login verification password


⑥ Restrict clients, memory management

Set the maximum number of clients that can connect to redis


Maxclients 10000 # sets the maximum number of clients that can connect to redis

  maxmemory #Set the maximum memory capacity of redis configuration

maxmemory-policy noeviction    # Processing strategy after the memory reaches the upper limit

  Volatile LRU: LRU only for keys with expiration time set (default value)  
  allkeys-lru  :  Delete the key of LRU algorithm   
  Volatile random: randomly delete keys that are about to expire   
  Allkeys random: random deletion    
  volatile-ttl  :  Delete expiring   
  noeviction  :  Never expire, return error  


⑦ Append only mode, AOF configuration:

Appendonly no # by default, the AOF mode is not enabled. By default, the RDB persistence mode is used. In most cases, the name of the appendfilename "appendonly. AOF" # persistence file is fully used by RDB

  # appendfsync always   # Sync is synchronized with each modification, which consumes performance
  appendfsync everysec   # Sync is executed once per second, and 1s of data may be lost
  # appendfsync no       # Without synchronization, the operating system synchronizes data by itself, with the fastest performance


2. Redis persistence

Redis is an in memory database. If the database state in memory is not saved to disk, the database state in the server will disappear once the server process exits. So redis provides persistence!

Redis provides two persistence methods: RDB and AOF.

2.1.RDB(Redis DataBase)

RBD persistence mode will write the data set snapshot in memory to disk within the specified time interval, that is, snapshot snapshot mode. When it recovers, it reads the snapshot file directly into memory.

Redis will separately create (fork) a sub process for persistence. It will first write the data to a temporary file. After the persistence process is completed, redis will use this temporary file to replace the last persistent file. In the whole process, the main process does not perform any IO operations. This ensures extremely high performance. If large-scale data recovery is required and the integrity of data recovery is not very sensitive, RDB method is more efficient than AOF method. The disadvantage of RDB is that the data may be lost after the last persistence (the redis service is down).

RDB is our default persistence mode. Generally, we don’t need to modify this configuration.




The file saved in RDB mode is dump.rdb file.


Trigger mechanism

1. When the save rule is satisfied, the RDB rule will be triggered automatically.

2. Executing the flush command will also trigger RDB rules.

3. Exiting redis will also generate RDB files. The system will automatically generate a dump.rdb file during backup.


Recover RDB file data:

1. Move the backup file (dump. RDB) to the redis installation directory and start the service;

2. Check the file storage directory:> config get dir
1) "dir"
2) "/usr/local/bin"


Advantages and disadvantages of RDB persistence:


1. Suitable for large-scale data recovery;

2. Low requirements for data integrity and consistency;


1. Make a backup at a certain interval, so if it goes down except redis, all modifications after the last snapshot will be lost;

2. When forking the parent process, a copy of the data in memory is cloned, and the memory space needs to be taken into account.


2.2.AOF(Append Only File)

The AOF save form of redis will record each write operation in the form of a log, and record all instructions executed by redis (read operations are not recorded). Only files can be added, but files cannot be overwritten. At the beginning of redis startup, redis will read the file and rebuild the data, in other words, When redis restarts, it executes the write instruction from front to back according to the contents of the log file to complete the data recovery.


How redis configures AOF:

Aof is not enabled by default. We need to configure it manually. Change appendonly no to yes to enable AOF. Redis will take effect after restart.

If the AOF is misaligned or abnormally damaged in the process, redis cannot be started. We need to repair the AOF file. Redis provides us with a tool:

redis-check-aof  –fix

#Switch to the redis conf Directory:
redis-check-aof --fix appendonly.aof



Aof file related configuration items:

Append only no # whether to use append only mode as the persistence mode. RDB mode persistence (no) is used by default. Yes is required to enable AOF
Appendfilename "appendonly. AOF" # appendfilename AOF file name
Appendfsync everysec # appendfsync AOF persistence policy configuration
#No means that fsync is not executed. The operating system ensures that the data is synchronized to the disk, and the speed is the fastest.
#Always means that fsync is executed for each write to ensure data synchronization to disk.
#Everysec means that fsync is executed every second, which may cause the loss of this 1s data.
Whether appendfsync can be used during no appendfsync on rewrite # rewriting. Use the default no to ensure data security
Auto AOF rewrite min size # sets the override reference value
Auto AOF rewrite percentage # sets the override base value


Aof advantages and disadvantages:


1. Synchronization per modification: appendfsync always synchronizes and persists. Every time a data change occurs, it will be immediately recorded to the disk. The performance is poor, but the data integrity is good;

2. Synchronization per second: appendfsync everysec asynchronous operation, recording every second. If it goes down for one second, data will be lost;


1. For the data of the same dataset, AOF files are much larger than RDB files, and the recovery speed is slower than RDB files.

2. The running efficiency of AOF is slower than that of RDB. The efficiency of synchronization strategy per second is better, and the asynchronous efficiency is the same as that of RDB.


Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]