Details on redis optimal configuration and redis.conf description of recommendations

  • 2020-05-14 05:20:37
  • OfStack

1. Redis.conf configuration parameter:

Whether to run as a daemon

daemonize yes

#, such as process after operation, the need to specify a pid, defaults to/var run/redis pid

pidfile redis.pid

# bind host IP with a default value of 127.0.0.1

#bind 127.0.0.1

#Redis listens on the default port

port 6379

After the client is idle for many seconds, disconnect, default is 300 (seconds)

timeout 300

Logging level, with 4 optional values, debug, verbose (default), notice, warning

loglevel verbose

Specify the file name of the log output, stdout by default, or /dev/null to mask the log

logfile stdout

Number of available databases, default is 16, default database is 0

databases 16

Policy for saving data to disk

When 1 piece of Keys data is changed, refresh to disk1 in 900 seconds

save 900 1

When 10 Keys data are changed, refresh to disk1 in 300 seconds

save 300 10

When 1w bar keys data is changed, refresh to disk1 time in 60 seconds

save 60 10000

Whether to compress data objects when dump.rdb database

rdbcompression yes

Local database file name, dump.rdb by default

dbfilename dump.rdb

Local database store path, default value is./

dir /var/lib/redis/

########### Replication #####################

Replication configuration for #Redis

# slaveof < masterip > < masterport > When the native is a slave service, set the IP and port of the primary service

# masterauth < master-password > When the native is a slave service, set the connection password for the master service

Connection password

# requirepass foobared

Maximum number of client connections, unlimited by default

# maxclients 128

Maximum memory usage setting. After the maximum memory setting is reached, Redis will first try to clear the Key that has expired or is about to expire. When this method is processed, it will reach the maximum memory setting and no longer be able to write.

# maxmemory < bytes >

Whether to log after each update operation, if not turned on, the data may be lost for a period of time when the power is off. Because redis itself synchronizes data files according to the save condition above, some data will only exist in memory for a period of time. The default value is no

appendonly no

Update log file name, default is appendonly.aof

#appendfilename

Update logging conditions with three optional values. no means waiting for the operating system to synchronize the data cache to disk, always means manually calling fsync() to write the data to disk after each update operation, and everysec means synchronization once per second (the default).

# appendfsync always

appendfsync everysec

# appendfsync no


################ VIRTUAL MEMORY ###########

Whether to turn on VM or not, the default value is no

vm-enabled no

# vm-enabled yes

Virtual memory file path, default value is /tmp/ redis.swap, cannot be Shared by multiple instances of Redis

vm-swap-file /tmp/redis.swap

All data larger than vm-max-memory is stored in virtual memory. No matter how small the vm-max-memory setting is, all index data is stored in memory (Redis's index data is keys). In other words, when vm-max-memory is set to 0, all value is actually on disk. The default value is 0.

vm-max-memory 0

vm-page-size 32

vm-pages 134217728

vm-max-threads 4


############# ADVANCED CONFIG ###############

glueoutputbuf yes

hash-max-zipmap-entries 64

hash-max-zipmap-value 512

Whether to reset Hash table

activerehashing yes

Note: the Redis official documentation makes some recommendations for the use of VM:

When your key is small and your value is large, VM works better because it saves more memory.

When your key is not too small, consider using some unusual methods to turn a large key into a large value. For example, consider combining key and value into a new value.

It is better to save your swap files on a file system with good support for sparse files, such as Linux ext3.

With the parameter vm-max-threads, you can set the number of threads to access the swap file, preferably not exceeding the machine's core. If set to 0, then all operations to the swap file will be serial.

2. Adjust the system kernel parameters

If memory is tight, you need to set kernel parameters:

echo 1 > /proc/sys/vm/overcommit_memory

Here under the meaning of this configuration: 1 / proc sys/vm/overcommit_memory

This file specifies the kernel's policy for memory allocation, which can be 0, 1, or 2.

0, means the kernel will check whether there is enough available memory for the process to use; If there is enough memory available, the memory request is allowed; Otherwise, the memory application fails and the error is returned to the application process.

1, means that the kernel allows all physical memory to be allocated, regardless of the current state of memory.

2, which means that the kernel allows more memory to be allocated than the sum of all physical memory and swap Spaces

In theory, the memory occupied by the child process is the same as that of parent. For example, the memory occupied by parent is 8G. At this time, the memory of 8G should also be allocated to child. So the optimal memory allocation policy here should be set to 1 (meaning that the kernel allows all physical memory to be allocated, regardless of the current memory state)

1. Run the service

# redis - server etc/redis/redis conf open

# redis-cli shutdown closed

2. The test

1) you can start the redis service in the background and test it with the redis-benchmark command

2) actual operation test by redis-cli command

3. Save/backup

Data backup can be done by backing up the file periodically.

Since redis is written asynchronously to disk, if you want to write data from memory to disk immediately, you can execute the following command:

redis-cli save or redis-cli-p 6380 save (specified port)

Note that the above deployment operations require certain permissions, such as copying and setting kernel parameters.

The redis-benchmark command also writes memory data to the hard disk.

4. Open the port number

1) open /etc/sysconfig/iptables,

2) after - [A INPUT-p tcp-m state --state m tcp --dport 22-j ACCEPT], Plus [-A INPUT-p state m tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp tcp dport dport dport dport dport ACCEPT] // 6379 here is the default port number of Redis

3) save, restart the firewall: / etc/init d/iptables restart


Related articles: