[Redis Notes] Article 2nd: basic configuration items of redis. conf

Source: Internet
Author: User
Tags allkeys
The configuration items of Redis seem complicated. In analysis, they can be divided into several categories (redis with redisv2.6.14 version. conf example): 1) basic configuration item 2) Persistence (Persistence) related configuration 3) Replication configuration 4) Security Configuration 5) Limit configuration 6) SlowLog configuration 7) Advanced Configuration 8) Primary des Configuration

The configuration items of Redis seem complicated. Under the analysis, they can be divided into several categories (using redis v2.6.14. conf example): 1) basic configuration item 2) Persistence (Persistence) related configuration 3) Replication configuration 4) Security Configuration 5) Limit configuration 6) SlowLog configuration 7) Advanced Configuration 8) Primary des Configuration

The configuration items of Redis seem complicated. In analysis, they can be divided into several categories (take redis. conf of redis v2.6.14 as an example ):

1) basic configuration items

2) Persistence Configuration

3) Replication Configuration

4) Security Configuration

5) Limit Configuration

6) SlowLog Configuration

7) Advanced Configuration

8) configure the DES

Persistence configuration and Replication configuration are very important for redis. Therefore, I am going to explain them separately in the next two documents.

This document describes several other configuration items.

1. Basic configuration items

1) daemonize

Whether to start in daemon mode. The default value is no. If yes is configured, the system starts in daemon mode. In this case, redis instance writes the process ID pid to the default file/var/run/redis. pid.

2) pidfile

Configure the pid file path. Default Value:/var/run/redis. pid. This configuration item is valid when redis is started in daemonized mode.

3) port

The TCP port listened by the Redis process. The default value is 6379. If it is set to 0, redis will no longer listen TCP socket

4) bind

Configure the bind Nic. If it is configured with a specific ip address, redis only listens to connections from the NIC. This configuration item is in the commented status by default, indicating that redis will listen to connections from all network adapters of the machine.

5) unixsocket and unixsocketperm

Configure the path and permission of the unix sock file, indicating that redis needs to listen for data requests from the unix socket file in the specified path. These configuration items are commented by default, that is, redis does not listen on unix socket by default.

6) timeout

Configure the connection timeout time, in seconds. After the timeout, The redis process actively disconnects. If the value is set to 0, the redis service process will not actively disconnect the connection from the client.

7) tcp-keepalive

Configure the time interval for redis to send active ACKs to the client. The default value is 0, indicating that keep-alive packets are not sent.
Note: This configuration item is newly added. It is not available in version 2.6.7 and is available in version 2.6.14 (which version has not been investigated ).

8) loglevel

Configure the Log level of the redis process. Four types of logs are supported: debug, verbose, notice, and warning. The log details are decreased and can be configured according to the actual situation.

9) logfile

Redis output Log Path. The default value is stdout. If you want to change to another directory (such as./log/redis-running.log), the parent path of the log file must be mkdir in advance, otherwise startup will fail

10) sys-log-enable/syslog-ident/syslog-facility

These three configuration items are related to syslog. By default, they are all in the commented state. I will not repeat it here. If you are interested, you can check man syslog in shell terminal.

Summary: In the above basic configuration items, port is required, and the remaining items are generally kept by default.

2. Persistence Configuration

The length is too large. The next note will be explained in detail. Update: see here

3. Replication Configuration

There are a lot of sections. I will explain them in detail in the next article. Update: see here

4. Security Configuration

If the redis instance may receive commands from a client that is not controlled by itself (such as access from a third party), you can enable password protection (that is, the client must first pass authentication (through AUTH) ). You can also use rename-command to disable Dangerous commands that may compromise the normal operation of Redis.

1) requirepass

Access Password

2) rename-command

RENAME Command. For example, rename-command CONFIG randomcommand or rename-command CONFIG "", the latter completely disables the CONFIG command.

Note: Changing the name of commands that are logged into the AOF file or transmitted to slaves may cause problems. that is, if some commands that are written to the aof file or synchronized to the slave database are rename, it may cause a problem: when the aof file is played back, the redis instance may not recognize the commands after the rename; similarly, if the master instance is configured with rename commands and is synchronized to the slave instance for execution, the latter may not recognize these unofficial "Custom" commands.

5. Limit Configuration

1) maxclients

The number of concurrent connections on the client. The default value is 10000. When the system fd limit cannot be changed for a redis instance, the system limit n minus 32 is used as the maximum number of connections supported by Redis (32 is because Redis retains 32 fd for internal logic ). When the maximum number of connections supported by Redis is reached, the new connection will be closed, and the corresponding client will receive an error message "max number of clients reached.

2) maxmemory

Configure the maximum memory occupied by the Redis Server, in bytes. If this threshold is reached, Redis will try to delete the key that meets the elimination Condition Based on the elimination policy configured by the user. If the user configures a noeviction policy, Redis will not Delete the existing key. At this time, all write or sort commands from the client that require more memory will report an error, the read command can be executed normally.

This configuration item is useful when Redis is used as the LRU cache.

Note: In master-slave deployment, when the master is configured with an elimination policy and maxmemory is configured, you must configure a threshold value that is smaller than the number of physical memory available on the machine, because the master-slave synchronization needs to retain the output buffer for the slave. If the master's maxmemory is configured to a value close to the host's Physical Memory, it may cause serious consequences for the elimination of all master keys!

Specific trigger process: After the memory actually used by the Redis Server reaches the threshold, the master key is deleted according to the elimination policy, and the slave key is also deleted using the DEL command synchronously, the master needs to apply for the output buffer to store the command sent to the slave, which will make the master try to use more memory, thus increasing the memory exceeding the limit. Therefore, the master can only delete more keys to reduce memory usage, and the DELs commands of these keys also need to be synchronized to slaves, this means that the master needs to apply for a larger output buffer to store synchronization commands or data. The typical "avalanche effect", the worst result is that the master will delete all the keys.

If maxmemory is set to a value smaller than Physical Memory (for example, 90% of the latter), when the Memory used by Redis reaches the configured threshold, the key is eliminated, the synchronization command sent to slave is stored in the output buffer. In this case, the actual memory usage of Redis may continue to grow. As there are still about 10% of memory resources available in the system, therefore, the output buffer will borrow resources from these free memory instances (starting from Redis 2.4, the master will consider this growth to be temporary, and the memory will be released after synchronization ), in this way, the master does not delete more keys to free up space for the output buffer.

For more details about this issue and the implementation policy of the author of Redis, refer to here.

In short, remember: if the system is deployed in the master-slave mode and the master is configured with an elimination policy, the maxmemory of the master must be configured with a reasonable value (compared with the maximum physical memory that the user can provide for Redis ), this prevents all keys from being completely deleted when the memory used by Redis reaches the threshold!

3) maxmemory-policy

Configure the Redis elimination policy. The default value is volatile-lru. Currently, six policies are supported:

A. volatile-lru => remove the key with an expire set using an LRU algorithm;

B. allkeys-lru-> remove any key accordingly to the LRU algorithm

C. volatile-random-> remove a random key with an expire set

D. allkeys-random-> remove a random key, any key

E. volatile-ttl-> remove the key with the nearest expire time (minor TTL)

F. noeviction-> don't expire at all, just return an error on write operations

4) maxmemory-samples

Configure the number of samples for the key elimination algorithm. The default value is 3. This configuration item exists because redis uses an approximate elimination algorithm to save memory. This configuration item can be used to adjust the accuracy of the elimination algorithm: when the key needs to be eliminated (for example, when the memory reaches the threshold), Redis will be in the key set that meets the elimination condition (specified by maxmemory-policy, sample n keys randomly and delete the key that matches the LRU. By default, n takes 3. To improve the accuracy of the elimination algorithm, n can be increased (the cost is to increase the CPU computing time ).

6. SlowLog Configuration

Redis can record slow queries whose processing time exceeds a certain threshold. The processing time here does not include I/O operations (such as the read/write time of a session with the client ).

Note: Because Redis Server is implemented in a single thread, blocking of a query command may affect subsequent client requests. Therefore, it is best to enable slow query records in the online environment to track problems.

1) slowlog-log-slower-

Specify the threshold for slow query. Unit: microseconds. Query commands whose processing time exceeds this value are recorded in logs.

2) slowlog-max-len

Configure the maximum number of slowlog records. The size is unlimited, but more memory is consumed. 128 by default.

7. Advanced Configuration

1) Threshold configuration for Internal Data Structure Optimization

You can configure the corresponding threshold, and Redis determines whether the related data structure is optimized during its internal implementation. For example:

Hash-max-ziplist-entries 512

Hash-max-ziplist-value 64

The configuration above specifies that when the number of keys in the ziplist is not greater than 512 and the maximum value is not greater than 64 bytes, redis uses a special memory-saving data structure to store these k-v data.

Similar configurations include data types such as list, set, and zset.

2) activerehashing

Configure whether Redis actively rehashing. The default value is yes, which means that Redis will automatically rehashing 10 times per second (triggered once every MS) to optimize memory usage as much as possible.

Note: Redis adopts the lazy rehashing policy, that is, the more frequently accessed hash table, the more frequently Redis will rehashing the table. On the contrary, if a hash table is in the idle state, then the rehashing for it will never be actually executed.

3) output buffer

The output buffer is the buffer allocated by Redis to the client (the "client" here may be the real client, or slave or monitor ), if the output buffer allocated to a client exceeds the reserved size, Redis may close the connection to the client according to the Configuration Policy.

For example, if Redis is used as the message queue and the consumer processing speed of the ordered message cannot keep up with the producer of the published message, the corresponding output buffer may exceed the limit.

The configuration item format is as follows:

Client-output-buffer-limit

: Currently, three clients are supported: 1) normal => normal clients; 2) slave clients and MONITOR clients; 3) pubsub => clients subcribed to at least one pubsub channel or pattern

: If the output buffer size exceeds this value, Redis will immediately close the connection with the corresponding client

: If the output buffer size exceeds soft limit and the duration exceeds soft seconds, Redis will close the connection with the corresponding client.

The default configuration is as follows:

Client-output-buffer-limit normal 0 0 0

Client-output-buffer-limit slave 256 mb 64 mb 60

Client-output-buffer-limit pubsub 32 mb 8 mb 60

8. Configure the DES

When there is not only one Redis instance on the machine, the "personalized" configuration of each Redis instance can be realized here. At this time, the common configurations of these instances can be written to redis. conf, And the personalized configuration is written to the file specified by the include configuration path.

Configuration format: include/path/to/local. conf

[References]

1. redis. conf (redis v2.6.14)

2. redis github issue: maxmemory + evicting policy + slaves = death

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.