preface

When we buy a machine, we first need to read the instructions to know how to use it. Similarly for Redis, by viewing the configuration file, we can also have a general understanding of the components and operation mechanism of Redis, so as to better use Redis. The Redis configuration file is divided into different modules, which we also study in order of modules.

#Redis Memory unit. It is case insensitive.
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes

Copy the code

Includes

#To import other configuration files, use include to import configuration files
# include /path/to/local.conf
# include /path/to/other.conf

Copy the code

Modules

#The module is loaded during startup. If the server fails to load the module, the loading operation will be interrupted.
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so

Copy the code

Network

#Set IP addresses that are allowed to connect, similar to whitelist. If you don't set itbind, all network connections are allowed. This is very dangerous, so only local access is allowed by default.
#In front of the address"-"Prefix to indicate that Redis will start successfully even if the address is not available.
# bind192.168.1.100 10.0.0.1 These two IP addresses are allowed to connect to Redis
#The defaultbind127.0.0.1 - : : 1The bind 127.0.0.1 - : : 1
#Protected mode, enabled by default
#If the protection mode is enabled, no protection mode is configuredbindOr no password is set and only local connections are accepted
#Only sure in no usebindWhen the network interface is bound and authentication is not configured, the protected mode should be disabled only if clients of other hosts are allowed to connect to Redis
protected-mode yes

#Redis port
port 6379

#This parameter determines the length of the completed queue (after the three-way handshake) in a TCP connection,
#, of course, this value must be greater than Linux system definition/proc/sys/net/core/somaxconn value, default is 511.
#The default value for Linux is 128. When the number of concurrent requests is large and the client speed is slow, you can set the two parameters together.
#In high concurrency environments you need a high backlog value to avoid slow client connection problems
tcp-backlog 511

#How many seconds does the server disconnect after the client is idle? 0 indicates continuous connection
timeout 0

#The TCP connection is kept alive
#If this option is not set to 0, Redis will periodically send an ACK request to the client to check if the client is dead, and close the connection to any client that does not respond. The default value is 300 seconds.
tcp-keepalive 300

Copy the code

General

#Whether to run in daemon mode, change to yes, otherwise background startup will not be possible
daemonize no

#When Redis is running as a daemon, Redis writes the PID to the pidfile by default"/var/run/redis.pid"
pidfile /var/run/redis_6379.pid

#Log levels (DEBUG, verbose, notice, and Warning)
loglevel notice

#Specifies the log file name. An empty string indicates that standard output is used. If you are running as a daemon and using standard output, the log will write"/dev/null"
logfile ""

#You can use the syslog-enabled command to control whether logs are printed to the syslog. You can also use the syslog-ident command to specify the log id in the syslog.
syslog-enabled no
syslog-ident redis

#Specifies the syslog device. The value can be USER or local0-local7. For details, see the usage of the Syslog service.
syslog-facility local0

#Disable the built-in crash log. This configuration is commented out by default. That is, logging is enabled by default
crash-log-enabled no

#Memory checking in the event of a crash, which is part of the above crash logging, is enabled by default and uncommented is disabled
crash-memcheck-enabled no

#The number of databases, by default using the 0th database, that you can passThe "SELECT 1 "command selects the first database
databases 16

Copy the code

SnapShotting

Redis is an in-memory database and will be lost immediately after a power failure. To prevent data loss after the system breaks down, data in the memory must be saved to hard disks. Redis provides two ways to save data. One is the SnapShot, which saves all data in the memory to the hard disk. This file is called the RDB file (Redis DataBase). The other is the AOF (Append Only File) log. After each update operation is completed, the log corresponding to the operation is written to the File. After the system breaks down and restarts, the commands in the log are executed one by one to recover the data. Here we look at the RDB configuration first, and the AOF configuration later.

#Redis is an in-memory database and will be lost immediately after a power failure. To prevent data loss after the system breaks down, data in the memory must be saved to hard disks
#Save 
      
         saves data to the hard disk with at least the number of updates in seconds
       
#If this parameter is not configured, Redis uses the following three policies to save snapshots
#Save 3600 1: Updates at least once within 3600 seconds (one hour)
#Save 300 100: At least 100 update operations are performed within 300 seconds (5 minutes)
#Save 60 10000: at least 10000 update operations are performed within 60 seconds
# save ""Disable snapshot
save <seconds> <changes>


#By default, if the RDB snapshot is enabled and the latest snapshot failed to be saved, Redis will refuse to receive updates to remind users that data persistence has failed, otherwise the updated data may be lost.
#Redis will resume receiving updates when the background snapshot operation is restored.
#Of course, you can change this configuration to allow Redis to continue receiving updates if the latest snapshot operation fails.
stop-writes-on-bgsave-error yes


#Whether to enable compression storage of RDB snapshot files
#This function is enabled by default. If a large amount of data is required, compression saves disk space but increases CPU consumption. You are advised to disable this function to save CPU resources.
rdbcompression yes

#After version 5.0, the RDB file is saved and loaded, and the verification function is added to ensure file integrity. Enabling this option increases the performance cost by about 10%. You can disable this option if you want high performance.
rdbchecksum yes

#RDB file name
dbfilename dump.rdb

#Full Redis synchronization is implemented through RDB file transfer. If persistence is not enabled and whether to remove the RDB files in primary/secondary synchronization after the synchronization is complete, the default value is no. If legal compliance or security concerns are involved, you can set this parameter to yes. (This configuration takes effect only if both AOF and RDB persistence are disabled.)
rdb-del-sync-files no

#Directory for storing RDB files and AOF files
dir ./

Copy the code

Replication

Redis is highly available and needs to ensure that the service cannot be interrupted. Therefore, Redis provides a master-slave library mode, in which a copy of data is saved to multiple instances to share the pressure. At the same time, when the master fails, other instances can continue to provide services. So what we need to think about is, how do we keep the data consistent across multiple instances, and how do we elect a new master when the master fails?

#Specify this Redis instance as the replica of masterIP: MasterPort
#The replication between the master and replica is asynchronous. However, you can also specify that if the number of copies of the master is less than the specified number, write requests will be stopped during replication
#When the Replica is temporarily disconnected, Redis uses incremental synchronization (only receives updates received by the Master after the disconnection) instead of full synchronization (replicates all data of the Master again).
#Master - slave synchronization is automatic and does not require human participation
replicaof <masterip> <masterport>


#Password for the slave service to connect to the master service when password protection is set for the master service
masterauth <master-password>


#When the Replica disconnects from the Master or is replicating full data, the Replica responds to the client's request:
#(1) Replica-serve-stale-data yes
#The replica still accepts the request from the client. The data returned may be outdated (the master updates the new data, but the replica is disconnected and does not update the data in time) or empty (the data has not been replicated in time during the first replication).
#(2) Replica-serve-stale-data no
#INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,
#     UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,
#Except for the "HOST and LATENCY" command, the "SYNC with master" error message is displayed for all other commandsinProgress"
replica-serve-stale-data yes


#Whether replica is read-only. The replica is read-only by default
replica-read-only yes


#Whether to use diskless synchronization in the primary/secondary replication
#When a new replica is connected to the master, or a replica is connected again after being disconnected for a long time (incremental synchronization cannot be used), the RDB file must be used for full data synchronization. In this case, two methods are available:
#1. Use disks: The master saves the RDB snapshot of full data to disks and sends the RDB file to replicas
#2. Diskless replication: The RDB file is directly transferred without saving the RDB file to the hard disk
#If disks are used and multiple Replicas need to synchronize data, the RDB file is shared. If diskless replication is used, each replica must be replicated one by one.
#For non-disk replication, Redis has also optimized. When a replica needs full synchronization, the Master does not send data immediately, but waits for a few seconds to see if there are new replicas that need synchronization. In this way, data can be sent in parallel. Follow-up replicas can start only after this synchronization is complete.
#When to use diskless replication: The disk speed is slow and the bandwidth is high
repl-diskless-sync no


#When no disk is being replicated, the waiting time for the master to start transferring RDB snapshots is 5 seconds by default.
#Once transmission starts, follow-up replicas can only queue up and wait for the RDB transmission to be completed before synchronization starts. If the Master waits for a few seconds, multiple replicas may arrive, allowing parallel transmission
repl-diskless-sync-delay 5



#When diskless replication is used, how does Replica load data after receiving data
#Generally, the disk speed is slower than the network, and saving and loading RDB files will increase the time, but parsing RDB directly from the network will always refresh the database, so the following three options are available
#1. Save the received data to the local PC and load it after transmission (default)
#2. To ensure security, load the device directly without a disk
#3. Save a copy of the received data in memory (memory usage, possibly OOM)
repl-diskless-load disabled


#The default interval for replica PING Master is 10 seconds
repl-ping-replica-period 10


#There are three types of timeout Settings
#Timeout period of RDB data transmitted by the Replica and master SYNC
#Replica Angle, timeout period for the master to send data packets or ping packets. 
#Timeout duration of replica ACK ping in master
#Ensure that the timeout period is greater than repl-ping-replica-period
repl-timeout 60


#Whether to disable TCP_NODELAY
#If you select yes, TCP_NODELAY is disabled. A small bandwidth is used to send data and the delay for replica to receive data is set. In Linux, the default delay is 40ms
#If no is selected, the parameter is disabled. That is, TCP_NODELAY is enabled. Redis uses a large bandwidth to send data to replica, so the data receiving delay by replica is small
#The default value is no to ensure a low latency for the master and slave. If the network is congested, you can select Yes.
repl-disable-tcp-nodelay no


#Replication backlog: When an update request is received, the master writes an extra copy of data to the backlog. This is a circular buffer. When replicas are temporarily disconnected and reconnected, only the updated portion of the data needs to be synchronized.
#Because the replica is a ring buffer, when the replica is disconnected for a long time, the previous data will be overwritten. In this case, full synchronization is required.
#The larger the replication backlog is, the longer the replica disconnection time is allowed. The default value is 1MB
repl-backlog-size 1mb


#How long does it take for all replicas to be disconnected to release the master backlog? The default value is 3600s. 0 indicates that the backlog will never be cleared.
repl-backlog-ttl 3600


#Redis is highly available. If the Master falls offline due to a fault, select one replica from all replicas as the Master. This configuration indicates the replica priority. The smaller the replica is, the higher the priority is. The default value is 100
#For example, if the priorities of three replicas are 10, 100 and 25, the first replica selects the first one.
replica-priority 100



#Min-replicas-to-write: Specifies that if the number of healthy replicas is less than N, the Master stops receiving update requests. The default value is 0, indicating that the configuration is disabled
#Min-replicas-max-lag: Slaves are considered healthy only when their delay is less than or equal to min-replicas-max-lag seconds. The default value is 10 seconds.
min-replicas-to-write 3
min-replicas-max-lag 10

Copy the code

Clients

#Set the maximum number of client connections at a time. The default value is 10000
#If Redis Server fails to set process file limits based on maxClients, set the maximum number of client connections to -32 (for internal connections).
#If the current number of connections reaches MAXClients, new connections will be closed and an error will be reported"max number of clients reached"
maxclients 10000

Copy the code

Memory Management

#Set the maximum memory limit available to Redis in bytes.
#If the memory limit is reached, the key-value is removed to free up space according to the maxmemory-policy
#If the key cannot be removed according to the clear policy, or the clear policy is SET to "do not clear", Redis will report an error to the commands that increase the memory usage, such as SET and LPUSH, and the read operation will not be affected.
#Note that the memory limit does not include the buffer sent by the master to the Replica command. If the buffer is full, the master will delete some keys to clear the memory. At the same time, the master will need to put these commands into the buffer, which will increase the memory usage. If the memory is full, the master will need to clear the key again. Therefore, the memory limit must be smaller than the memory limit to allow memory space for the buffer.
maxmemory <bytes>


#Data clearance policy when memory usage reaches the limit
#Volatile -lru: removes the key set to expire using the LRU algorithm (Least Recently Used)
#Allkeys-lru: Remove any key using the LRU algorithm
#Volatile -lfu: The key that sets the expiration time is removed using the LFU algorithm
#Allkeys-lfu: removes any key using the LFU algorithm
#Volatile -random: Randomly removes a key that sets the expiration time
#Allkeys-random: removes a key randomly
#Volatile-ttl: removes the key closest to the expiration time
#Noeviction: Does not remove any keys, just returns a write error. Default option
maxmemory-policy noeviction

#LRU, LFU, and Minimal TTL are approximate algorithms, relatively accurate algorithms save memory, and speed or precision can be adjusted by this configuration
#The smaller Maxmemory-samples are, the faster and less accurate they are; The opposite is more accurate, but consumes more CPU resources.
#The default is 5, which is a compromise configuration.
maxmemory-samples 5


#Starting from Redis 5, the maximum memory limit is valid only for master but not for replica, i.e., replica-ignore-maxmemory yes.
#The clearing policy is implemented only on the Master side. Then the master sends the command to clear the key to replica. If the replica reaches the memory limit and the master does not exist, the replica does not delete the key according to the clearance policy
#This configuration ensures master/slave consistency. If the replica is writable or the replica memory is different from the master, you can set it tofalse
replica-ignore-maxmemory yes


#Redis has two ways to discover that a key is expired: when the key is accessed, it is expired, or a background thread scan (this method is called active Expire Key).
#Ensure that the number of expired keys does not exceed 10% of the memory and does not consume more than 25% of the CPU resources
#The default value is 1 and the maximum value is 10. The larger the configuration, the fewer expired keys exist in the memory after scanning, but the more CPU resources are occupied, increasing the system delay.
active-expire-effort 1

Copy the code

Lazy Freeing

#Redis has two deletion modes: synchronous deletion and asynchronous deletion.
#The DEL command is used to delete data synchronously. After the DEL command is executed, data can be deleted before other commands are executed. If the value corresponding to the key is smaller, the blocking time is shorter. If the value of the key is large, the service may block for seconds to delete the data. Redis also provides asynchronous delete commands: UNLINK (corresponding to the non-blocking version of DEL), FLUSHALL ASYNC(corresponding to the non-blocking version of FLUSHDB), and FLUSHDB ASYNC(corresponding to the non-blocking version of FLUSHDB). Upon receiving the command, Redis will start a thread to delete the data without blocking.

#With blocking or non-blocking, control is in the user's hands. However, Redis Server sometimes needs to delete the Key or refresh the entire database, so the user configuration is provided, and the default blocking mode is deleted. The scenarios and configuration items are as follows.
#1. When the memory reaches the limit, delete the key according to the configured clearance policy 
#2. Delete the expired Key
#3. The influence of some instructions, such assetAn existing key that needs to be deleted 
#4. When replica replicates in full, the database must be cleared first

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no

#When the DEL command is executed, the Redis command is executed instead of the UNLINK command
lazyfree-lazy-user-del no

#The FLUSHDB and FLUSHALL commands use the SYNC and ASYNC tags to identify synchronous or asynchronous operations. Use this configuration if no tags are provided. The default value is no, indicating synchronization.
lazyfree-lazy-user-flush no

Copy the code

Threaded I/O

#We usually say that Redis is single-threaded, which mainly means that the network IO and key-value read and write of Redis are completed by one thread, but other Redis functions, such as persistence, asynchronous deletion, cluster data synchronization and so on, are actually performed by additional threads.

#Now Redis provides multithreading for reading and writing. Because of the slow write operations, our previous solutions were to pipeline, or use cluster shards to scale horizontally. Now with multi-threading, you don't have to worry about pipeline and sharding, and you can double the speed.

#Multithreading is disabled by default. Make sure it is enabled because the CPU is busy causing poor performance. It is recommended that the machine should be configured with more than 4 cores while keeping one core free after opening. For example, with 4 cores, configure 2-3 I/O threads. 8 cores, 6 threads configured.
#Setting it to 1 means that multithreading is not enabled and only one thread is used for reading and writing.
io-threads 4

#With multithreading enabled, by default it is only used for writing, but can also be configured for reading (multithreaded reading is not useful).
io-threads-do-reads no

Copy the code

Append Only Mode

This is another way of persisting Redis files using snapshots, which we discussed above.

After the configuration is enabled, Redis writes the command to the log file after each update operation. When the machine breaks down, Redis can run the command in the log file one by one to recover data. If you use this approach, even if the machine goes down, very little data will be lost relative to the RDB snapshot file. However, because the log is written once for each update operation, the log file is very large.

#By default, Redis uses RDB snapshots for persistence. In this mode, the entire memory file must be backed up. Otherwise, Redis performance will be affected.
#However, if the system goes down between two RDB backups, it will lose data at the minute level, so Redis provides AOF persistence, losing up to one update or second level data (depending on the configuration).
#RDB and AOF can be enabled at the same time. If AOF is enabled, the default Redis restart loads the AOF file.
#By default, AOF is disabled and RDB is used
appendonly no

#AFO filename
appendfilename "appendonly.aof"

#AOF Log drive policy
#After an update command is executed, Redis then writes the command to the AOF buffer (using the main thread). Calling fsync() causes the operating system to write the buffer data to disk. Redis provides three types of buffer write timing:
#1. Always: Synchronous write back: Logs are synchronously written back to disk immediately after each write command is executed
#2. Everysec, write back every second: After each write command is executed, the log is first written to the memory buffer of the AOF file, and the contents of the buffer are written to disk every second
#3. No, operating system controlled write back: after each write command is executed, the log is first written to the memory buffer of the AOF file, and the operating system decides when to write the buffer back to disk

#Each of the three operations has its advantages and disadvantages:
#1. Always: Almost no data is lost (at most one is lost). However, logs must be sent to the disk after each write command, which affects the performance of the main thread
#2. No: High efficiency, but the operating system is responsible for the disk, Redis cannot control, once the breakdown of data will be lost
#3. Everysec: a compromise solution, which is made every second to balance efficiency and data
appendfsync everysec


#When AOF is enabled and the always or everysec policy is configured, the fsync() call may be blocked if a large number of I/ OS are being performed in the background.
#When a large number of I/ OS are executed using the BGSAVE or BGREWRITEAOF command in the background, the default value is no, that is, the disk continues to be driven.
#If you select Yes, the disk is stopped. If the system breaks down, data may be lost for up to 30 seconds. 
no-appendfsync-on-rewrite no


#AOF file rewrite
#AOF is a file that records all write commands received. As more write commands are received, the AOF file grows larger. Thus Redis provides an AOF rewrite mechanism for compressing files (multiple update commands for the same key can eventually be rewritten as a write command).
#Auto-aof -rewrite-min-size: The rewrite can be triggered only when the size of the Aof file is larger than this configuration
#Auto-aof -rewrite-percentage: Indicates that the rewrite is triggered if the growth rate of the AOF file size (compared with the size after the last rewrite) is greater than this parameter.
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb


#Whether to allow loading truncated AOF files.
#The AOF file may not be completely written due to the downtime. As a result, the file is incomplete. When Redis restarts, the AOF file will be loaded.
#1. Redis exits and an error message is displayed. Subsequent users can log inThe "redis-check-aof "tool fixes the aof file and then restarts
#2. Load available information and discard incorrect data
aof-load-truncated yes



#Mixed RDB and AOF persistence
#RDB is the replication of full memory data. Frequent generation of RDB files has a significant impact on performance. If an RDB file breaks down, data at the minute level is affected.
#AOF is a write operation log. If AOF is used only once a second, it has little impact on performance. If AOF is used only, the log file is too large, and the AOF needs to be overwritten
#So you can combine the two: the data between the two RDB snapshots is saved using AOF logs. In this way, data is not lost and performance is improved.
aof-use-rdb-preamble yes

Copy the code

More and more

Welcome to visit my blog, learn together, grow together! lifelmy.github.io/