Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of basic configuration items of redis.conf

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the basic configuration items of redis.conf, which is very detailed and has certain reference value. Friends who are interested must finish it!

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

1) basic configuration items

2) persistence (Persistence) related configuration

3) Replication configuration

4) Security configuration

5) Limit configuration

6) SlowLog configuration

7) Advanced configuration

8) INCLUDES configuration

Among them, persistence configuration and Replication configuration are very important to redis, which will be explained separately later.

This note mainly introduces several other types of configuration items.

1. Basic configuration item

1) daemonize

Whether to start in daemon mode. Default is no. If it is configured as yes, redis instance will write the process number pid to the default file / var/run/redis.pid.

2) pidfile

Configure the pid file path, default / var/run/redis.pid, which is valid when starting redis with daemonized mode

3) port

The TCP port that the Redis process listens to. Default is 6379. If 0 is configured, redis will no longer listen TCP socket.

4) bind

Configure the bind network card. If it is configured with a specific IP value, the redis only listens for connections from the network card. This configuration item defaults to commented status, which means that redis will listen for connections from all network cards of the machine.

5) unixsocket and unixsocketperm

Configure the path and permissions of the unix sock file to indicate that the redis listens for data requests from the unix socket file under 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 (in seconds). The redis process disconnects actively after the timeout. If it is configured as 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 live ACKs to client. Default is 0, which means no keep-alive message is sent.

Note: this configuration item is new, not in version 2.6.7, and the latest version 2.6.14 (there is no research on which version was introduced).

8) loglevel

Configure the log level of the redis process, which supports four types: debug/verbose/notice/warning. The details of the log information are decreasing and can be configured according to the actual situation.

9) logfile

Redis output log path, default stdout. If you need 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, and the default is commented status. I'm not going to repeat it here. If you're interested, you can check it out by man syslog in shell terminal.

Summary: among the above basic configuration items, port is required, and the rest can be kept by default.

two。 Persistence (Persistence) related configuration

There is a lot of space, so the next note will be explained in detail. Update: see here

3. Replication configuration

There is a lot of space, which will be explained in detail in the next note. Update: see here

4. Security configuration

If the redis instance may receive commands from clients that are not under its control (such as access from a third party), you can consider enabling password protection (that is, the client must be authenticated (through AUTH) before executing other commands, or you can disable some dangerous commands that will endanger the normal operation of Redis through rename-command.

1) requirepass

Specify access password

2) rename-command

Rename the command. Such as rename-command CONFIG randomcommand or rename-command CONFIG "", where 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. In other words, if some commands that will be written to the aof file or synchronized to the slave library are rename, it may cause problems: when the aof file is played back, the redis instance may not recognize the commands after rename; similarly, the master instance is configured with rename commands, synchronized to the slave instance execution, the latter may not be able to recognize these unofficially supported "custom" commands.

5. Limit configuration

1) maxclients

The number of concurrent connections to the client. Default is 10000. When the redis instance is unable to change the system fd limit, it takes the system limit n minus 32 as the maximum number of connections supported by Redis (minus 32 because Redis reserves 32 fd for internal logic). When the maximum number of connections supported by Redis is reached, the new connection will be close and the corresponding client will receive an error message of "max number of clients reached".

2) maxmemory

Configure the maximum memory value that can be consumed by Redis Server, in byte. If this threshold is reached, Redis attempts to delete key that meets the phase-out criteria based on the user-configured phase-out policy. If the user is configured with a never-obsolete (noeviction) policy, Redis will not delete the existing key, at this time, all commands from the client that require more memory, such as writing or sorting, will report an error, and the read command can be executed normally.

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

Special note: under the master-slave deployment, when master configures the elimination policy, when configuring maxmemory, you need to configure a threshold smaller than the amount of physical storage available on the machine, because master-slave synchronization needs to reserve output buffer for slave. If the maxmemory of master is configured to a value that is very close to the machine Physical Memory, it may lead to the elimination of all key of master!

Specific trigger process: after the actual memory used by Redis Server reaches the threshold, the key of master will be deleted according to the elimination policy, and the key of slave will be deleted synchronously through the DEL command. In this case, master needs to apply for output buffer to store commands sent to slave, which will cause master to try to use more memory, thus aggravating the severity of memory overrun. Therefore, master can only try to reduce memory usage by removing more keys, and these keys DELs commands also need to be synchronized to slaves, which means that master needs to request a larger output buffer to hold synchronization commands or data. In a typical "avalanche effect", the worst-case scenario is that master will delete all key.

If the maxmemory is configured to a lower value than the machine Physical Memory (such as 90% of the latter), when the actual memory used by the Redis reaches the configuration threshold, the key will be eliminated and the synchronization commands sent to the slave will be saved to the output buffer, and the actual memory used by the Redis may continue to grow. Since the system still has about 10% memory resources available, the output buffer will borrow resources from the memory of these free (starting with Redis 2.4). Master will think that this growth is temporary and memory can be freed after the synchronization is completed), thus preventing master from making room for output buffer by deleting more keys.

A more detailed discussion of this issue and the implementation strategy of the Redis author can be found here.

In summary, keep in mind that if the system is deployed in a master-slave manner and the master is configured with an elimination policy, then the maxmemory of master must be configured with a reasonable value (compared to the maximum physical memory that users can provide to Redis) to avoid all key being completely deleted after the actual memory used by Redis reaches the threshold!

3) maxmemory-policy

Configure the elimination policy for Redis, which defaults to volatile-lru. Currently, 6 strategies 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 when the key elimination algorithm runs, which defaults to 3. The reason for this configuration item is that redis uses an approximate elimination algorithm in order to save memory. This configuration item can be used to adjust the accuracy of the elimination algorithm: when you need to eliminate key (such as memory reaches a threshold), Redis will randomly sample n key in the key set that meets the elimination criteria (specified by maxmemory-policy) and delete the key that meets the LRU. By default, n takes 3. If you want to improve the accuracy of the elimination algorithm, n can be increased (at the cost of increasing the CPU operation time).

6. SlowLog configuration

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

Note: because Redis Server is implemented in a single thread, if one of the query commands causes blocking, it will affect subsequent client requests. Therefore, it is best to enable slow query records in the online environment in order to track the problem.

1) slowlog-log-slower-than

Specifies the threshold for slow queries in microseconds. Query commands that take longer than this value are recorded in the log.

2) slowlog-max-len

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

7. Advanced configuration

1) threshold configuration for internal data structure optimization

You can configure the appropriate threshold, based on which Redis determines whether the relevant data structure is optimized during its internal implementation. For example:

Hash-max-ziplist-entries 512

Hash-max-ziplist-value 64

The above configuration specifies that when the number of key in the ziplist is not greater than 512 and the maximum value is less than 64 bytes, the Redis will use a special, more memory-saving data structure to store the KMurv data.

Similar configurations include list, set, zset, and other data types.

2) activerehashing

Configure whether Redis is active rehashing, the default yes, which means that Redis has 10 automatic rehashing actions per second (triggered once per 100ms) to optimize memory usage as much as possible.

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

3) output buffer

Output buffer is the buffer assigned by Redis to 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 that end according to the configuration policy.

For example, if Redis is used as a message queue, the corresponding output buffer exceeds the limit when the consumer processing speed of the ordering message does not keep up with the producer of the published message.

The configuration item is in the following format:

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 size of output buffer exceeds soft limit and the duration of this situation exceeds soft seconds, Redis will close the connection to the corresponding client.

The default configuration is as follows:

Client-output-buffer-limit normal 0 0 0

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

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

8. INCLUDES configuration

When there is more than one Redis instance on the machine, the "personalized" configuration of each Redis instance can be implemented. At this point, the common configuration of these instances can be written to the redis.conf, and the personalized configuration can be written to the file specified by the include configuration path.

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

The above is all the contents of the article "sample Analysis of redis.conf basic configuration items". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report