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 Redis
explained the concept and basic commands of . Today we will look at its configuration file, which Redis
plays a very important role in our development and practical application.
redis.conf
We can find the configuration file in the installation directory and vim
view 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 Redis
is5.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 structs2
configuration files, it can be includes
included. redis.conf
It 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
Redis
The module can loadmodule
be loaded at startup through the option, if the server cannot load the module, the server will stop. loadmodule
Multiple modules can be loaded with multiple options.
NETWORK
- bind 127.0.0.1 : By default, if no "bind" configuration directive is specified,
Redis
will 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 bind
addresses are set, Redis
multiple addresses will be maintained internally Socket
, each Socket
for one network interface
.
- protected-mode yes : This option is enabled by default.
When Redis
the server does not use bind
options to explicitly specify what to listen to network interface
, and no password is set, Redis
the server will only accept connections from 127.0.0.1
and from ::1
clients and Unix
domains .Socket
- port 6379 : used to set the
Redis
listeningTCP
port, the default is 6379, set to 0 means no listeningTCP
port - timeout 0 : close the connection after idle seconds, "0" means not close
- tcp-keepalive 300 : the unit is seconds, if it is 0, no
keepalive
detection will be performed, it is recommended to set it to 60 - tcp-backlog 511 : The setting
tcp
isbacklog
actuallybacklog
a connection queue.
backlog
The sum of the queues = unfinished three-way handshake queue + completed three-way handshake queue
A high value is needed in a high-concurrency environment backlog
to avoid slow client connection problems.
Note : Linux
the kernel will reduce this value to /proc/sys/net/core/somaxconn
the value of , so need to confirm the increase somaxconn
and tcp_max_syn_backlog
two values to achieve the desired effect.
GENERAL
daemonize
Redis
The single-process multi-thread mode is adopted, daemonize
which is used to specify redis
whether to start with a daemon thread. By default, Redis
does 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 upstart
or systemd
run Redis
, you can interact Redis
with your , the available options are:supervision tree
- no no interaction (default)
- upstart notifies by
Redis
sending a signal toSIGSTOP
upstart
- systemd notifies by
$NOTIFY_SOCKET
writing toREADY=1
systemd
- auto is determined by whether
UPSTART_JOB
or notNOTIFY_SOCKET
the environment variable is setupstart
orsystemd
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/testingverbose
: lots of little useful information, but not as confusing as the debug levelnotice
: Moderately verbose, probably what is needed in productionwarning
: Only log very important/critical messages
logfile
logfile ""
The name of the log, if it is empty, it redis
will send the standard output to the console, if it is configured to run as a daemon process, and set logfile
to 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 tosyslog
, the system log is closed by defaultsyslog-ident redis
: The specifiedsyslog
log flag deviceredis
starts withsyslog-facility local0
: Specifiessyslog
the device, the value can beUSER
orLOCAL0-LOCAL7
, the default is usedlocal0
Security
requirepass 12345!@#
Set redis
the connection password. If the connection password is configured, the client redis
needs to Auth <password>
provide the password through the command when connecting, which is disabled by default.
If the password is set, ping
it fails, and it prompts "NoAuth Authentication required", adding auth + 密码
it will work.
Requires auth + password before any command
Redis
Generally, what is done is caching, not security, and the system will considerLinux
it to be in a safe environment.
CLIENTS
maxclients 10000
: maximum number of connections
Set redis
how many clients can be connected at the same time. 10000
Clients by default .
When you cannot set the process file handle limit, redis
it will be set to the current file handle limit value minus 32
, because redis
some handles will be reserved for its own internal processing logic.
If this limit is reached, redis
new connection requests are rejected and a max number of clients reached is sent to those connection requesters in response.
MEMORY MANAGEMENT
Sets redis
the amount of memory that can be used. Once the upper limit of memory usage is reached, redis
it will try to remove internal data, and the removal rules can be maxmemory-policy
specified by .
If redis
the data in the memory cannot be removed according to the removal rules, or the removal is not allowed , then redis
an error message will be returned for those instructions that need to apply for memory, such as SET
, LPUSH
etc. But for commands without memory application, it will still respond normally, such as GET
etc.
If yours redis
is the master redis
(indicating that you redis
have 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 maxmemory
and the corresponding recovery strategy algorithm should be set to 3/4 of the physical memory , or a smaller ratio, because redis
when copying data and other services, caches are also required. In case the cached data is too large to cause redis
a crash, causing the system to fail due to an error.
Sacrifice a part of the cache data to preserve the overall system availability. redis
The new memory mechanism will key
put it in the memory and value
store it in swap
the area.
This configuration needs to be used in conjunction with maxmemory-policy . redis
When the data in the middle memory reaches maxmemory
, the clearing policy is triggered . When memory is low , any write
operation (eg set
, lpush
etc.) will trigger the execution of the purge policy .
actual environment
It is recommended redis
that the hardware configuration of all physical machines be consistent (memory consistency), and at the same time ensure that master/replica
the maxmemory policy configuration is consistent.
When memory is full
If the command is still received set
, redis
it will try to remove the set expire
information first key
, regardless of whether key
the expiration time has arrived.
When deleting, it will be deleted according to the expiration time, and the earliest to be expired key
will be deleted first. If everything with expire
information key
is deleted and the memory is not enough, an error will be returned. This way, redis
no write requests will be accepted, only get
requests.
maxmemory
The setting of is more suitable for useredis
asmemcached
a similar cache.
Maximum Cache Policy
maxmemory-policy:
- volatile-lru: Use
LRU
the (least recently used) algorithm to removekey
, only for keys with an expiration time set - allkeys-lru: use
LRU
algorithm to removekey
(allkey
) - 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 random
key
- volatile-ttl: remove those
TTL
with the smallest valuekey
, 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)
LRU
Algorithms,LFU
algorithms, orTTL
algorithms are not exact algorithms, but approximate algorithms.
Use policy rules:
- 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
. - 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 Redis
5 keys are randomly selected, and then the one that best meets the conditions is selected. For LRU
5 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 5
the start, by default, replica
the node ignores maxmemory
the setting (unless failover
the node is promoted to master
a node after that happens).
This means that only master
expired delete policies will be enforced and commands will be sent to the peer master
after the key has been deleted .replica
DEL
This behavior guarantees master
sum replicas
consistency, and this is usually what you want, but if your replica
nodes are writable, or you want replica
nodes with different memory configurations, and you ensure that all replica
write 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 maxmemory
memory than the set value. So you need to monitor replicas
the machine where the node is running and make sure that the node does not exceed the size of physical memory when master
the node reaches the configured size.maxmemory
replicas
Let's talk about this first today. As for the master-slave replication and persistence in the configuration file, we will explain it later.