Redis Universal Configuration

Source: Internet
Author: User
Tags allkeys syslog

By default, Redis is not run in daemon form. With daemonize configuration items you can control how Redis runs, and if you change to Yes, Redis will run in daemon form:


Daemonize No


When running in daemon form, Redis generates a PID file, which is generated by default in/var/run/redis.pid. Of course, you can specify the location of the PID file generation by Pidfile, for example:


Pidfile/path/to/redis.pid


By default, Redis responds to connection requests for all available network cards on this computer. Of course, Redis allows you to specify the IP to bind through the BIND configuration item, such as:


Bind 192.168.1.2 10.8.4.2


The default service port for Redis is 6379, which you can modify via the port configuration item. If the port is set to 0, Redis will not listen on the port.


Port 6379


Some students asked, "If Redis does not listen to the port, how to communicate with the outside world", in fact, Redis also support through the UNIX socket way to receive requests. You can specify the path to the UNIX socket file through the Unixsocket configuration item and specify permissions for the file through Unixsocketperm.


Unixsocket/tmp/redis.sock
Unixsocketperm 755


When a redis-client has not been requested to send to the server side, then the server side has the right to actively shut down the connection, you can set the "Idle Timeout Time" by timeout, 0 means never shut down.


Timeout 0


TCP connection KeepAlive policy, can be set by the Tcp-keepalive configuration item, in seconds, if set to 60 seconds, the server side will every 60 seconds to connect to idle clients to initiate an ACK request to check whether the client has been hung off, For unresponsive clients, their connections are closed. So it takes up to 120 seconds to close a connection for a maximum of one time. If set to 0, keepalive detection is not performed.


Tcp-keepalive 0


Redis supports setting the log level through the LogLevel configuration item, which is divided into four levels, namely debug, verbose, notice, warning.


LogLevel Notice


Redis also supports setting the log file generation location through the LogFile configuration item. If set to an empty string, Redis outputs the log to standard output. If you set the log to output to standard output in daemon case, the log will be written to/dev/null.


LogFile ""


If you want the log to print to the syslog, it is also easy to control by syslog-enabled. In addition, Syslog-ident allows you to specify log flags in the syslog, such as:


Syslog-ident Redis


It also supports specifying a syslog device with a value of either user or LOCAL0-LOCAL7. Specifically, you can refer to the usage of the Syslog service itself.


Syslog-facility local0


For Redis, you can set the total number of databases, and if you want a redis to contain 16 databases, set the following:


Databases 16


The numbers for these 16 databases will be 0 to 15. The default database is a database numbered 0. Users can use Select <DBid> to select the appropriate database.


"Redis Configuration – Snapshots"

Snapshot, which is mainly about Redis's RDB persistence-related configuration, let's take a look.

We can use the following instructions to save the data to disk, that is, to control the RDB snapshot function:


Save <seconds> <changes>


For example:


Save 900 1//indicates that a persistence is triggered once every 15 minutes and at least 1 key changes


Save 300 10//indicates that a persistence is triggered once every 5 minutes and at least 10 key changes

Save 60 10000//indicates that at least 10,000 key changes every 60 seconds to trigger a persistence


If you want to disable the RDB persistence policy, you can do so as long as you do not set any save instructions, or pass an empty string parameter to save to achieve the same effect, like this:


Save ""


If a user turns on the Rdb snapshot feature, if a failure occurs when Redis persists data to disk, Redis stops accepting all write requests by default. The advantage of this is that it makes it clear to the user that the data in memory and the data on the disk are already inconsistent. If Redis continues to receive write requests regardless of this inconsistency, it can cause some disastrous consequences.


If the next RDB persists successfully, Redis will automatically resume accepting write requests.

Of course, if you don't care if the data is inconsistent or if there are other ways to find and control this inconsistency, you can turn this feature off so that Redis can continue to accept new write requests when the snapshot write fails. The configuration entries are as follows:


Stop-writes-on-bgsave-error Yes


For snapshots stored on disk, you can set whether to compress storage. If so, Redis uses the LZF algorithm for compression. If you do not want to consume the CPU for compression, you can set this feature to off, but the snapshots stored on the disk are relatively large.


Rdbcompression Yes


After storing the snapshot, we can also have Redis use the CRC64 algorithm for data validation, but doing so will increase the performance cost by approximately 10%, and if you want to get maximum performance gains, you can turn this feature off.


Rdbchecksum Yes


We can also set the name of the snapshot file, which is configured by default:


Dbfilename Dump.rdb


Finally, you can also set the path for this snapshot file to be stored. For example, the default setting is the current folder:


Dir./


"Redis Configuration – Replication"

Redis provides a master-slave synchronization feature.

The Slaveof configuration item allows you to control one Redis as the slave server for another redis, locating the location of the master Redis by specifying the IP and port. In general, we recommend that users set a cycle for a different frequency snapshot from Redis, or configure a different service port from Redis, and so on.


Slaveof <masterip> <masterport>


If the master Redis is set with a verification password (set using Requirepass), the Masterauth will be used to set the checksum password in the configuration from Redis, otherwise the master Redis rejects the access request from Redis.


Masterauth <master-password>


How does Redis handle external access requests when it loses a connection to the master Redis from Redis, or if the master-slave synchronization is in progress? Here, there are two options available from Redis:


First option: If Slave-serve-stale-data is set to Yes (the default), the client's read-write request continues to be answered from Redis.

The second option: If Slave-serve-stale-data is set to No, the request from Redis will return "SYNC with Master in Progress" and, of course, exceptions, when the client sends an info request and a slaveof request, Processing is still done from Redis.

You can control whether a write request can be accepted from Redis. Writing data directly from Redis is generally only applicable to data with very short life cycles, because the temporary data is cleared when the master-slave synchronization occurs. Since the redis2.6 version, the default from Redis is read-only.


Slave-read-only Yes


Read-only from Redis is not suitable for direct exposure to untrusted clients. To minimize risk, you can use the Rename-command directive to rename some potentially destructive commands to avoid external direct calls. Like what:


Rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52


Ping packets are periodically sent from Redis to the master Redis. You can control its cycle by repl_ping_slave_period instructions. The default is 10 seconds.


Repl-ping-slave-period 10


During master-slave synchronization, a timeout may occur in these cases:


1. In terms of Redis, when there is a large IO transmission.
2. From a redis point of view, when data transfer or ping, the master Redis timeout
3. From the point of view of Master Redis, when replying to ping from Redis, timeout from Redis

The user can set the time-out period, but make sure that this time limit is greater than the Repl-ping-slave-period value, or each time the master Redis is considered to be out of Redis.


Repl-timeout 60


We can control whether Tcp_nodelay is disabled during master-slave synchronization. If Tcp_nodelay is turned on, the primary Redis uses fewer TCP packets and less bandwidth to transfer data from Redis. However, this may increase the latency of some synchronizations, which will probably reach about 40 milliseconds. If you shut down the tcp_nodelay, the delay time for data synchronization will be reduced, but will consume more bandwidth. (If you do not know tcp_nodelay, you can come here to popular science).


Repl-disable-tcp-nodelay No


We can also set the synchronization queue length. The queue Length (backlog) is a buffer in the primary redis that is used by the master Redis to cache data that should be sent from Redis during disconnection from Redis. That way, when you reconnect from Redis, you don't have to re-synchronize the data all the time, just synchronize this part of the incremental data.


Repl-backlog-size 1MB


If the master Redis is still unable to connect to the slave redis after waiting for some time, the data in the buffer queue will be cleaned up. We can set the length of time the master Redis will wait. If set to 0, it means never clean up. The default is 1 hours.


Repl-backlog-ttl 3600


We can give a lot of priority from Redis, and in the case of primary redis continuing to work abnormally, high priority from Redis will be upgraded to primary redis. The smaller the number, the higher the priority. For example, a master Redis has three from Redis, the priority number is 10, 100, 25, then the number 10 from Redis will be selected first to upgrade to primary Redis. When the priority is set to 0 o'clock, this from Redis will never be selected. The default priority is 100.


Slave-priority 100


If the master Redis discovers that more than m of connections from Redis are delayed by more than n seconds, then the master Redis will stop accepting foreign write requests. This is because redis typically pings to the master Redis every second, and the primary Redis records every point in time from which Redis was last sent to Ping, so the master Redis is able to understand every operation from Redis.


Min-slaves-to-write 3
Min-slaves-max-lag 10


The above example shows that if there is greater than or equal to 3 connections from Redis with a delay greater than 10 seconds, then the master Redis will no longer accept external write requests. If one of the two configurations is set to 0, this feature will be turned off. By default, Min-slaves-to-write is 0, while Min-slaves-max-lag is 10.


"Redis Configuration – Security"

We can ask the Redis client to authenticate the password before sending the request to redis-server. When your redis-server is in a less trusted network environment, I believe you will use this feature. Because Redis performance is very high, so you can complete up to 150,000 password attempts per second, so you'd better set a complex enough password, otherwise it is easy to hack.


Requirepass Zhimakaimen


Here we set the password to "open sesame" via Requirepass.

If you know the password, you can log in using Redis-cli-a River


Redis allows us to rename a Redis directive, such as changing some of the more dangerous commands to a name to avoid being mistakenly executed. For example, you can change the Config command to a very complex name, which avoids external calls and also satisfies the need for internal calls:


Rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c89


We can even disable the Config command, which is to change the name of config to an empty string:


Rename-command CONFIG ""


However, it is important to note that if you use the AoF method for data persistence, or if you need to communicate with Redis, changing the name of the instruction may cause some problems.


"Redis Configuration-Limitations"

We can set up how many clients a Redis can connect to at the same time. By default, it is 10,000 clients. When you cannot set the process file handle limit, Redis sets the current file handle limit value minus 32, because Redis leaves some handles for its internal processing logic.

If this limit is reached, Redis rejects new connection requests and issues "max number of clients reached" to these connection requesters in response.


MaxClients 10000


We can even set the amount of memory that Redis can use. Once the memory usage limit is reached, Redis will attempt to remove the internal data, and the removal rule can be specified by Maxmemory-policy.


If Redis cannot remove the in-memory data according to the removal rules, or if we set "do not allow removal", REDIS returns error messages for those instructions that require memory, such as set, Lpush, and so on. However, for instructions that do not have a memory request, they will still respond normally, such as get.


MaxMemory <bytes>


One thing to note is that if your redis is a master Redis (that means your redis has a redis), then when you set the memory usage limit, you need to leave some memory space in the system for the Sync queue cache, only if you set the "Do not remove" situation, do not consider this factor.


For memory-removal rules, Redis provides up to 6 removal rules. They are:

1.VOLATILE-LRU: Using the LRU algorithm to remove keys from an outdated collection
2.ALLKEYS-LRU: Using the LRU algorithm to remove key
3.volatile-random: Remove the random key from the expired collection
4.allkeys-random: Remove the Random key
5.volatile-ttl: Remove the key with the lowest TTL value, which is the key that has only recently expired.
6.noeviction: Do not remove. For write operations, only error messages are returned.

Regardless of which removal rule is used, REDIS returns an error message for a write request if no appropriate key can be removed.


Maxmemory-policy VOLATILE-LRU


Both the LRU algorithm and the minimum TTL algorithm are not exact algorithms, but are estimates. So you can set the size of the sample. If Redis checks three keys by default and chooses which one is LRU, then you can change the number of this key sample.


Maxmemory-samples 3


Finally, we add a message that the Redis-supported write commands include the following as of the current version (2.8.4):


Set Setnx Setex Append
INCR decr rpush lpush rpushx lpushx linsert lset Rpoplpush sadd
Sinter sinterstore sunion sunionstore sdiff sdiffstore zadd Zincrby
Zunionstore zinterstore hset hsetnx hmset hincrby Incrby Decrby
Getset mset msetnx exec sort


"Redis Configuration – Append Mode"

By default, Redis asynchronously persists data to disk. This pattern has been proven to be effective in most applications, but when some problems occur, such as a power outage, this mechanism can result in the loss of a few minutes of write requests.

As described in the upper part of the blog, appending files (Append only file) is a better way to maintain data consistency. Even when the server loses power, only 1 seconds of write requests are lost, and even a write request is lost when the Redis process is in trouble and the operating system is functioning properly.

We suggest that the AOF mechanism and the RDB mechanism can be used at the same time without any conflict. For a discussion of how to maintain data consistency, see.



AppendOnly No


We can also set the name of the AoF file:


Appendfilename "Appendonly.aof"


A fsync () call that tells the operating system to immediately write the cached instruction to disk. Some operating systems will be "immediately", while others will be "as soon as possible."

Redis supports three different modes:

1.no: Do not Call Fsync (). Instead, let the operating system decide the sync time for itself. In this mode, Redis has the fastest performance.
2.always: Fsync () is called after each write request. In this mode, Redis is relatively slow, but the data is the safest.
3.everysec: Fsync () is called once per second. This is a tradeoff between performance and security.

Everysec is the default. The disclosure of data consistency can be consulted.



Appendfsync everysec


When the Fsync mode is set to always or everysec, if the background persistence process needs to perform a large disk IO operation, Redis may get stuck at the Fsync () call. This problem is not fixed at this point because even if we execute Fsync () in another new thread, the synchronous write call will be blocked.


To alleviate this problem, we can use the following configuration item, so that when Bgsave or bgwriteaof runs, the Fsync () call in the main process is blocked. This means that when another process is refactoring the aof file, the persistence of Redis is invalidated, as if we had set "Appendsync none". If your redis sometimes delays the problem, set the option below to Yes. Otherwise, keep No because this is the safest option to ensure data integrity.


No-appendfsync-on-rewrite No


We allow Redis to automatically rewrite aof. When aof grows to a certain size, redis implicitly calls bgrewriteaof to rewrite the log file to reduce the file volume.


Redis works this way: Redis records the size of the aof when it was last rewritten. If Redis has not been rewritten since it was started, the size of the AoF file will be used as the base value at startup. This benchmark value is compared to the current aof size. If the current aof size exceeds the set growth ratio, an override is triggered. In addition, you need to set a minimum size to prevent the aof from triggering an override at very small hours.


Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB


If you set Auto-aof-rewrite-percentage to 0, this override feature is turned off.


"Redis Configuration –lua Script"

The maximum running time for a LUA script is strictly limited, and it is important to note that the unit is milliseconds:


Lua-time-limit 5000


If this value is set to 0 or negative, neither an error nor a time limit will be available.


"Redis Configuration – Slow Log"

Redis Slow log refers to a system that logs queries more than the specified length of time. This length does not include IO operations, such as interacting with the client, sending the response content, etc., and only includes the time that the query command was actually executed.

For slow logs, you can set two parameters, one is the execution length, the unit is microseconds, the other is the length of the slow log. When a new command is written to the log, the oldest one is removed from the command log queue.

The unit is microseconds, which means that 1000000 represents one second. A negative number disables slow logging, and 0 means that each command is forced to be logged.


Slowlog-log-slower-than 10000


Slow log maximum length, you can fill in the value, there is no upper limit, but note that it will consume memory. You can use Slowlog reset to reset this value.


Slowlog-max-len 128


"Redis Configuration – Event Notification"

Redis can notify clients of the occurrence of certain events. A specific explanation of this feature can be found in.

"Redis Configuration – Advanced Configuration"

Some configuration items about the hash data structure:


Hash-max-ziplist-entries 512
Hash-max-ziplist-value 64


Some configuration items for the list data structure:


List-max-ziplist-entries 512
List-max-ziplist-value 64


Configuration items about the collection data structure:


Set-max-intset-entries 512


Configuration items for an ordered collection data structure:


Zset-max-ziplist-entries 128
Zset-max-ziplist-value 64


For configuration items that require re-hashing:


activerehashing Yes


About the control for client output buffering:


Client-output-buffer-limit Normal 0 0 0
Client-output-buffer-limit slave 256MB 64MB 60
Client-output-buffer-limit pubsub 32MB 8MB 60


Configuration Items About frequency:


Hz 10


For configuration items that override AoF


Aof-rewrite-incremental-fsync Yes


This article from "愺 Burgundy pounding his 豩" blog, declined reprint!

Redis Universal Configuration

Related Article

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.