Detailed explanation of common configuration of Redis

Table of contents

1. Use the config command to view and modify the configuration

2. Edit the redis.conf file to modify the redis configuration

3. Description of common configuration items in redis.conf

The Redis configuration file is located in the Redis installation directory, and the file name is redis.conf (Windows name is redis.windows.conf). You can use the config command or directly open the redis.conf file to view or modify it.


1. Use the config command to view and modify the configuration

  • config view command format
127.0.0.1:6379> config get config_setting_name
  • config command to view configuration
127.0.0.1:6379> config get loglevel
1) "loglevel"
2) "notice"

Use * to get all configurations

127.0.0.1:6379> config get *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) ""
  5) "masterauth"
  6) ""
  7) "cluster-announce-ip"
  8) ""
  9) "unixsocket"
 10) ""
 11) "logfile"
 12) "server_log.txt"
 13) "pidfile"
 14) ""
 15) "slave-announce-ip"
 16) ""
 17) "replica-announce-ip"
 18) ""
 19) "maxmemory"
 20) "104857600"
 21) "proto-max-bulk-len"
 22) "536870912"
 23) "client-query-buffer-limit"
 24) "1073741824"
 25) "maxmemory-samples"
 26) "5"
 27) "lfu-log-factor"
 28) "10"
 29) "lfu-decay-time"
 30) "1"
 31) "timeout"
 32) "0"
 33) "active-defrag-threshold-lower"
 34) "10"
 35) "active-defrag-threshold-upper"
 36) "100"
 37) "active-defrag-ignore-bytes"
 38) "104857600"
 39) "active-defrag-cycle-min"
 40) "5"
 41) "active-defrag-cycle-max"
 42) "75"
 43) "active-defrag-max-scan-fields"
 44) "1000"
 45) "auto-aof-rewrite-percentage"
 46) "100"
 47) "auto-aof-rewrite-min-size"
 48) "67108864"
 49) "hash-max-ziplist-entries"
 50) "512"
 51) "hash-max-ziplist-value"
 52) "64"
 53) "stream-node-max-bytes"
 54) "4096"
 55) "stream-node-max-entries"
 56) "100"
 57) "list-max-ziplist-size"
 58) "-2"
 59) "list-compress-depth"
 60) "0"
 61) "set-max-intset-entries"
 62) "512"
  • config modification command allocation value syntax
127.0.0.1:6379> config set config_setting_name new_config_value
  • Use the config command to modify the configuration
127.0.0.1:6379> config set loglevel notice
OK

2. Edit the redis.conf file to modify the redis configuration

The editor opens the value of the configuration that needs to be modified in the redis.conf file, and restarts redis after saving.

3. Description of common configuration items in redis.conf

# Specify which IP address redis receives requests from, if not set, then all requests will be processed
bind 127.0.0.1
 
#Whether to enable protection mode, it is enabled by default. If the bind and password are not specified in the configuration. After enabling this parameter, redis will only access locally and reject external access. If password and bind are enabled, they can be enabled. Otherwise, it is best to close it and set it to no
protected-mode yes
 
#redis default port number
port 6379
 
#This parameter determines the length of the completed queue (after completing the three-way handshake) in the TCP connection. Of course, this value must not be greater than that defined by the Linux system /proc/sys/net/core/somaxconn value, the default is 511, and the default parameter value of Linux is 128. When the system has a large amount of concurrency and the client speed is slow, these two parameters can be set together as a reference. The default value of this kernel parameter is generally 128, which is not enough for service programs with heavy loads. It is generally modified to 2048 or larger.
Add in /etc/sysctl.conf: net.core.somaxconn = 2048, then execute sysctl -p tcp-backlog 511 in the terminal
 
#This parameter is to set the client to idle for more than timeout, the server will disconnect, it is 0 Then the server will not actively disconnect, and it cannot be less than 0
timeout 0
 
#tcp keepalive parameter. If the setting is not 0, use the SO_KEEPALIVE value of tcp configuration. Using keepalive has two advantages: detecting the peer that hangs up. Reduce the problem of intermediate equipment failures that cause the network to appear to be connected but already connected to the peer port. In the Linux kernel, if keepalive is set, redis will send ack to the peer regularly. Detecting that the peer is down requires twice the setting value
tcp-keepalive 300
 
#Whether to run as a daemon thread, Redis does not run as a keep-alive thread by default (Windows does not support the configuration of daemon threads as no)
daemonize no
 

#When redis runs as a daemon thread, the pid is written to the /var/run/reids.pid file by default, and the pidfile can be specified through the pidfile
/var/run/redis/redis.pid
 
#Specifies the level of the server log. Levels include: debug, verbose, notice (suitable for production environments), warn (only very important information), and the default is notice
loglevel notice
 
#Logging method, default standard output, if the string is empty, the log will be printed to the standard output device. The standard output of redis running in the background is /dev/null
logfile ""
 
 
#Whether to enable recording syslog function
# syslog-enabled no
 
#Identifier of syslog.
# syslog-ident redis
 
#Log source, equipment
# syslog-facility local0
 
#The number of databases, the default database is 0. You can use the "SELECT [database serial number]" command to specify the database id
databases 16

 #RDB core rule configuration Specify the time period and how many update operations to synchronize the data to the data file, and multiple conditions can be matched. The official factory configuration defaults to 1 change within 900 seconds, 10 changes within 300 seconds, and 10,000 changes within 60 seconds, then the data snapshot in memory is written to disk.
If you don't want to use the RDB scheme, you can open the comment of save ""
#save <seconds> <changes>
# save ""

save 300 10
save 60 10000


#When an error occurs in RDB persistence, whether to continue to work, yes: can not work, no: can continue to work, you can know whether there is an error in RDB persistence through rdb_last_bgsave_status in info stop-writes-on-
bgsave- error yes
 
#Configure whether to compress data when storing to the local database, the default is yes. Redis uses LZF compression, but it takes up a little CPU time. If this option is turned off, it will cause the database file to become huge. It is recommended to enable it.
rdbcompression yes
 
#Whether to verify the rdb file; starting from the fifth version of the rdb format, the CRC64 checksum will be added at the end of the rdb file. This is beneficial to the fault tolerance of the file, but when saving the rdb file, there will be about 10% performance loss, so if you pursue high performance, you can turn off this configuration rdbchecksum yes #Specify the local database file name, generally use
the
 
default dump.rdb
dbfilename dump.rdb
 
#Data directory, the database will be written in this directory. rdb and aof files will also be written in this directory
dir ./

# Copy option, slave copies the corresponding master.
# replicaof <masterip> <masterport>
 
#If the master has set requirepass, then the slave needs to have the password of the master to connect to the master. masterauth is used to configure the password of the master, so that authentication can be performed after connecting to the master.
# masterauth <master-password>

 
#When the slave library loses connection with the host or replication is in progress, the slave library has two operating modes: 1) If slave-serve-stale-data is set to yes (default setting), the slave library will continue to respond to client requests. 2) If slave-serve-stale-data is set to no,
INFO,replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG,SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE,
PUNSUBSCRIBE, PUBLISH, PUBSUB,COMMAND, POST, HOST: and LATENCY Any requests other than commands
will return an error "SYNC with master in progress".
replica-serve-stale-data yes
 
#As a slave server, it is read-only by default (yes), and can be changed to NO for writing (not recommended)
#replica-read-only yes
 
# Whether to use socket mode to copy data . Currently, redis replication provides two methods, disk and socket. If a new slave is connected or a reconnected slave cannot be partially synchronized, full synchronization will be performed, and the master will generate an rdb file. There are two ways: the disk way is that the master creates
a new process to save the rdb file to the disk, and then passes the rdb file on the disk to the slave. The socket is a new process created by the master
, and directly sends the rdb file to the slave in the form of a socket. In the disk mode, when an rdb is being saved, multiple slaves can
Share this rdb file. The way of socket is to copy slaves one by one sequentially. The socket method is recommended when the disk speed is slow and the network speed is fast.
repl-diskless-sync no

#slave sends a ping request to the server according to the specified time interval. The time interval can be set by repl_ping_slave_period, the default is 10 seconds.
# repl-ping-slave-period 10
 
#Diskless replication delay time, prevent setting to 0. Once replication starts, the node will not receive replication requests from new slaves until the next rdb transfer.
So it's better to wait for a while, and wait for more slaves to connect.
repl-diskless-sync-delay 5
 

 
# Replication connection timeout. Both master and slave have timeout settings. The master detects that the last sending time of the slave exceeds the repl-timeout, that is, the slave is considered offline, and the slave information is cleared. If the slave detects that the last time it interacted with the master exceeds the repl-timeout, it considers the master to be offline. It should be noted that repl-timeout needs to be set to a larger value than repl-ping-slave-period, otherwise it will often detect timeout
# repl-timeout 60
 
 
#Whether to prohibit copying the tcp nodelay parameter of the tcp link, you can pass yes or no . The default is no, that is, use tcp nodelay. If
the master sets yes to disable the tcp nodelay setting, when copying data to the slave, it will reduce the number of packets and smaller network bandwidth
. But this may also cause data delay. By default, we recommend a smaller delay, but in the case of a large amount of data transmission, it is recommended to choose yes
repl-disable-tcp-nodelay no
 
#Replication buffer size, this is a ring replication buffer, used to save the latest replication Order. In this way, when the slave is offline, there is no need to complete
To fully replicate the data of the master, if partial synchronization can be performed, you only need to copy part of the data in the buffer to the slave, and the normal replication state can be restored
. The larger the size of the buffer, the longer the slave can be offline, and the replication buffer only allocates memory when there is a slave connection. When there is no
slave for a period of time, the memory will be released, the default is 1m
# repl-backlog-size 1mb
 
# The master will release the memory of the copy buffer when there is no slave for a period of time, and repl-backlog-ttl is used to set the length of time. The unit is seconds.
# repl-backlog-ttl 3600
 
# When the master is unavailable, Sentinel will elect a master according to the slave's priority. The slave with the lowest priority is elected as the master.
If it is configured as 0, it will never be elected.
replica-priority 100
 
#redis provides a way for the master to stop writing. If min-replicas-to-write is configured, the number of healthy slaves is less than N, and the mater prohibits it write. The master must have at least how many healthy slaves survive to execute the write command. Although this configuration cannot guarantee that all N slaves can receive the master's write operation, it can prevent the master from writing to avoid data loss when there are not enough healthy slaves. Setting it to 0 is to turn off this function
# min-replicas-to-write 3
 
# A slave whose delay is less than min-replicas-max-lag seconds is considered a healthy slave
# min-replicas-max-lag 10
 
# Set 1 or another Set to 0 to disable this feature.
# Setting one or the other to 0 disables the feature.
# By default min-replicas-to-write is set to 0 (feature disabled) and
# min-replicas-max-lag is set to 10.

# Set the maximum number of client connections that can connect to redis. The default is 10000 client connections. Since redis does not distinguish whether the connection is a client connection or an internal open file or a connection with a slave, the minimum recommended setting of maxclients is 32. If maxclients is exceeded, redis will send 'max number of clients reached' to the new connection and close the connection
# maxclients 10000

Specify the maximum memory limit of Redis. Redis will load data into the memory when it starts. After reaching the maximum memory, Redis will first try to clear the keys that have expired or are about to expire. After this method is processed, the maximum memory setting is still reached. Write operations will no longer be possible, but read operations will still be possible. The new vm mechanism of Redis will store the Key in the memory and the Value in the swap area

# maxmemory <bytes>

Specify whether to perform log records after each update operation. By default, Redis writes data to disk asynchronously. If it is not enabled, it may cause data loss for a period of time when power is off. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no

appendonly no

Specify the update log file name, the default is appendonly.aof

appendfilename "appendonly.aof"

Specify update log conditions, there are 3 optional values:

  • no : Indicates waiting for the operating system to synchronize the data cache to the disk (fast)
  • always : Indicates that fsync() is manually called after each update operation to write data to disk (slow, safe)
  • everysec : Indicates synchronization every second (compromise, default value)

appendfsync everysec

Specify whether to enable the virtual memory mechanism. The default value is no. Briefly, the VM mechanism stores the data in pages, and Redis swaps the pages with less access, that is, cold data, to the disk, and the pages with more access are automatically swapped out by the disk. into memory

vm-enabled no

For more details, please refer to the detailed explanation of common configurations in the redis configuration file

Guess you like

Origin blog.csdn.net/qq_56044050/article/details/123755008