Take a look at the configuration file of Redis

Hello everyone, I am Ah Q!

Being busy is a kind of happiness, which makes us have no time to experience pain; running is a kind of happiness, which allows us to truly experience life; exhaustion is a kind of enjoyment, which makes us have no time to be empty. These few words can briefly summarize my recent work and life.

Many friends have come to private message "reminder", today it is here!

In previous articles, we have Redisexplained the concept and basic commands of . Today we will look at its configuration file, which Redisplays a very important role in our development and practical application.

redis.confWe can find the configuration file in the installation directory and vimview it through the command. In order to prevent the configuration file from being changed, you must back it up before using it!

The version of this article Redisis5.0.7

UNITS

1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 1024*1024 bytes
1g => 1000000000 bytes
1gb => 1024*1024*1024 bytes

The unit is not case sensitive, only bytes are supported

INCLUDES

Similar to structs2configuration files, it can be includesincluded. redis.confIt can be used as the main gate, including others.

include /path/to/local.conf
include /path/to/other.conf

MODULES

loadmodule /path/to/my_module.so
loadmodule /path/to/other_module.so

RedisThe module can loadmodulebe loaded at startup through the option, if the server cannot load the module, the server will stop. loadmoduleMultiple modules can be loaded with multiple options.

NETWORK

  • bind 127.0.0.1 : By default, if no "bind" configuration directive is specified, Rediswill listen for connections from all available network interfaces on the server.

It is possible to use the "bind" configuration directive followed by one or more IP addresses to listen on only one or more selected interfaces.
For example:
bind 192.168.1.100 10.0.0.1

When multiple bindaddresses are set, Redismultiple addresses will be maintained internally Socket, each Socketfor one network interface.

  • protected-mode yes : This option is enabled by default.

When Redisthe server does not use bindoptions to explicitly specify what to listen to network interface, and no password is set, Redisthe server will only accept connections from 127.0.0.1and from ::1clients and Unixdomains .Socket

  • port 6379 : used to set the Redislistening TCPport, the default is 6379, set to 0 means no listening TCPport
  • timeout 0 : close the connection after idle seconds, "0" means not close
  • tcp-keepalive 300 : the unit is seconds, if it is 0, no keepalivedetection will be performed, it is recommended to set it to 60
  • tcp-backlog 511 : The setting tcpis backlogactually backloga connection queue.

backlogThe sum of the queues = unfinished three-way handshake queue + completed three-way handshake queue

A high value is needed in a high-concurrency environment backlogto avoid slow client connection problems.

Note : Linuxthe kernel will reduce this value to /proc/sys/net/core/somaxconnthe value of , so need to confirm the increase somaxconnand tcp_max_syn_backlogtwo values ​​to achieve the desired effect.

GENERAL

daemonize

RedisThe single-process multi-thread mode is adopted, daemonizewhich is used to specify rediswhether to start with a daemon thread. By default, Redisdoes not run as a daemon process. Use "Yes" if required.

#daemonize no 
//当前界面将进入redis的命令行界面,
exit强制退出或者关闭连接工具(putty,
xshell等)都会导致redis进程退出。

daemonize yes     
//代表开启守护进程模式。在该模式下,
redis 会在后台运行,并将进程 pid 号写入
至 redis.conf 选项 pidfile 设置的文件中,
此时 redis 将一直运行,除非手动kill该进程。

supervised no

When you pass upstartor systemdrun Redis, you can interact Rediswith your , the available options are:supervision tree

  • no no interaction (default)
  • upstart notifies by Redissending a signal toSIGSTOPupstart
  • systemd notifies by $NOTIFY_SOCKETwriting toREADY=1systemd
  • auto is determined by whether UPSTART_JOBor not NOTIFY_SOCKETthe environment variable is set upstartorsystemd

pidfile

pidfile /var/run/redis_6379.pid //进程pid文件

loglevel notice

Specifies the server log level: decreasing from top to bottom

  • debug: a lot of information, useful for development/testing
  • verbose: lots of little useful information, but not as confusing as the debug level
  • notice: Moderately verbose, probably what is needed in production
  • warning: Only log very important/critical messages

logfile

logfile ""

The name of the log, if it is empty, it rediswill send the standard output to the console, if it is configured to run as a daemon process, and set logfileto stdout, the log will be sent to/dev/null

database

databases 16

There are 16 default libraries in the system, and 0 libraries are used by default

syslog

  • syslog-enabled no: Whether to output the log to syslog, the system log is closed by default
  • syslog-ident redis: The specified sysloglog flag device redisstarts with
  • syslog-facility local0: Specifies syslogthe device, the value can be USERor LOCAL0-LOCAL7, the default is usedlocal0

Security

requirepass 12345!@#

Set redisthe connection password. If the connection password is configured, the client redisneeds to Auth <password> provide the password through the command when connecting, which is disabled by default.

If the password is set, pingit fails, and it prompts "NoAuth Authentication required", adding auth + 密码it will work.

Requires auth + password before any command

RedisGenerally, what is done is caching, not security, and the system will consider Linuxit to be in a safe environment.

CLIENTS

maxclients 10000: maximum number of connections

Set redishow many clients can be connected at the same time. 10000Clients by default .

When you cannot set the process file handle limit, redisit will be set to the current file handle limit value minus 32, because redissome handles will be reserved for its own internal processing logic.

If this limit is reached, redisnew connection requests are rejected and a max number of clients reached is sent to those connection requesters in response.

MEMORY MANAGEMENT

Sets redisthe amount of memory that can be used. Once the upper limit of memory usage is reached, redisit will try to remove internal data, and the removal rules can be maxmemory-policyspecified by .

If redisthe data in the memory cannot be removed according to the removal rules, or the removal is not allowed , then redisan error message will be returned for those instructions that need to apply for memory, such as SET, LPUSHetc. But for commands without memory application, it will still respond normally, such as GETetc.

If yours redisis the master redis(indicating that you redishave a slave redis), then when setting the upper limit of memory usage, you need to reserve some memory space in the system for the synchronization queue cache, only if you set "do not remove", It is not necessary to consider this factor.

max cache

#maxmemory <bytes>
maxmemory 128MB

The settings maxmemoryand the corresponding recovery strategy algorithm should be set to 3/4 of the physical memory , or a smaller ratio, because rediswhen copying data and other services, caches are also required. In case the cached data is too large to cause redisa crash, causing the system to fail due to an error.

Sacrifice a part of the cache data to preserve the overall system availability. redisThe new memory mechanism will keyput it in the memory and valuestore it in swapthe area.

This configuration needs to be used in conjunction with maxmemory-policy . redisWhen the data in the middle memory reaches maxmemory, the clearing policy is triggered . When memory is low , any writeoperation (eg set, lpushetc.) will trigger the execution of the purge policy .

actual environment

It is recommended redisthat the hardware configuration of all physical machines be consistent (memory consistency), and at the same time ensure that master/replicathe maxmemory policy configuration is consistent.

When memory is full

If the command is still received set, redisit will try to remove the set expireinformation first key, regardless of whether keythe expiration time has arrived.

When deleting, it will be deleted according to the expiration time, and the earliest to be expired keywill be deleted first. If everything with expireinformation keyis deleted and the memory is not enough, an error will be returned. This way, redisno write requests will be accepted, only getrequests.

maxmemoryThe setting of is more suitable for use redisas memcacheda similar cache.

Maximum Cache Policy

maxmemory-policy:

  • volatile-lru: Use LRUthe (least recently used) algorithm to remove key, only for keys with an expiration time set
  • allkeys-lru: use LRUalgorithm to remove key(all key)
  • volatile-lfu: use the LFU (least frequently used) approximation algorithm for expired keys
  • allkeys-lfu: use the LFU approximation algorithm for all keys
  • volatile-random: Remove random ones from the expired collection key, only for keys with an expiration time set
  • allkeys-random: remove randomkey
  • volatile-ttl: remove those TTLwith the smallest value key, i.e. those that will expire most recentlykey
  • noeviction: Do not remove. For write operations, just return an error message (default) (go to the company to observe the dimension, you should not choose this)

LRUAlgorithms, LFUalgorithms, or TTLalgorithms are not exact algorithms, but approximate algorithms.

Use policy rules:

  1. If the data presents a power-law distribution, that is, some data access frequency is high, and some data access frequency is low, use it allkeys-lru.
  2. Use if the data is equally distributed, that is, all data is accessed at the same frequency allkeys-random.

Number of samples

Set the sample size, the algorithms mentioned above are not exact algorithms, but estimates, so you can set the sample size.

maxmemory-samples 5

The default value is 5, which means that Redis5 keys are randomly selected, and then the one that best meets the conditions is selected. For LRU5 is more appropriate. 10 is already pretty close to real LRU, but will consume more CPU. 3 would be faster but not as precise.

Copy ignores max memory

replica-ignore-maxmemory yes

From Redis 5the start, by default, replicathe node ignores maxmemorythe setting (unless failoverthe node is promoted to mastera node after that happens).

This means that only masterexpired delete policies will be enforced and commands will be sent to the peer masterafter the key has been deleted .replicaDEL

This behavior guarantees mastersum replicasconsistency, and this is usually what you want, but if your replicanodes are writable, or you want replicanodes with different memory configurations, and you ensure that all replicawrite operations are idempotent, then You can modify this default behavior (make sure you understand what you are doing).

Note
that by default replica, the node will not implement the expiration policy, it may use more maxmemorymemory than the set value. So you need to monitor replicasthe machine where the node is running and make sure that the node does not exceed the size of physical memory when masterthe node reaches the configured size.maxmemoryreplicas

Let's talk about this first today. As for the master-slave replication and persistence in the configuration file, we will explain it later.

Guess you like

Origin blog.csdn.net/Qingai521/article/details/127984225