Ultra-strong, ultra-detailed Redis database Getting Started tutorial _redis

Source: Internet
Author: User
Tags allkeys hash numeric value socket syslog redis cluster install redis redis server

"This Tutorial directory"

What's 1.redis?
The author of 2.redis, who also
3. Who is using Redis
4. Learn to install Redis
5. Learn to start Redis
6. Using the Redis Client
7.REDIS Data Structure-Introduction
8.REDIS Data Structure –strings
9.REDIS Data Structure –lists
10.REDIS Data Structure-collection
11.REDIS data structure-ordered set
12.REDIS Data Structure-hash
13. Talk about Redis-two ways
14. Talk about Redis persistence –rdb
15. Talk about Redis persistence –aof
16. Chat Redis Persistent –aof rewrite
17. Talk about Redis-how to choose Rdb and AOF
18. Talk about master-slave-usage
19. Talk about master-slave-sync principle
20. Talk about Redis transaction processing
21. Teach you to read Redis configuration-Introduction
22. Teach you to read Redis configuration-General
23. Teach you to read Redis configuration – Snapshots
24. Teach you to read Redis configuration – Replication
25. Teach you to understand Redis configuration – security
26. Teach you to read Redis configuration-Restrictions
27. Teach you to read Redis configuration-Append mode
28. Teach you to read Redis configure –lua script
29. Teach you to read Redis configuration-Slow log
30. Teach you to read Redis configuration – Event notification
31. Teach you to read Redis configuration – Advanced Configuration

"What's Redis?"

Redis is an open-source, Key-value database that is written in C, that supports network interaction, and can be based on memory and persistence.

Redis's official website address, very good remember, is Redis.io. (specially checked, the domain name suffix io belongs to the national domain name, is British Indian Ocean territory, namely British Indian Ocean Territory)

Currently, VMware is funding the development and maintenance of REDIS projects.

"Who is the author of Redis?"

To get to the point, first look at the photos:

Is it beyond your expectation, well, there will always be some places different from the master.

This is the author of Redis, who is called Salvatore Sanfilippo, a Sicilia, I. Di from Italy and now lives in Catania. Currently works for pivotal company.

He uses the net name is Antirez, if you are interested, may go to his blog to stroll, the address is antirez.com, certainly also may go to follow his GitHub, the address is the Http://github.com/antirez.

"Who's using Redis?"

Blizzard, Digg, StackOverflow, GitHub, Flickr ...

"Learn to install Redis"

Download the latest version from the Redis.io redis-x.y.z.tar.gz after decompression, and then enter the Redis-x.y.z folder directly to make, installation is very simple.

Make succeeds in producing some binary executables under the SRC folder, including Redis-server, REDIS-CLI, and so on:

Copy Code code as follows:

$ find. -type f-executable
./redis-benchmark//tools for performing Redis performance testing
./redis-check-dump//Dump.rdb file to fix the problem
./REDIS-CLI//redis Clients
./redis-server//redis Service-side
./redis-check-aof//aof file to fix the problem
./redis-sentinel//For cluster management

"Learn to start Redis"

Startup Redis is very simple, straightforward./redis-server can start the server, and you can specify the configuration file to load in the following ways:

Copy Code code as follows:

./redis-server. /redis.conf



By default, Redis-server runs in a daemon manner and the default service port is 6379.

There is an interesting allusion to why the author chose 6379 as the default port, and the English-speaking students can read the author's explanation in this blog post.

"Use Redis Client"

Let's look directly at an example:

Copy Code code as follows:

This will start the Redis client.
$./redis-cli
Set key, value with set instruction
127.0.0.1:6379> set name "Roc"
Ok
To get the value of name
127.0.0.1:6379> Get Name
"Roc"
To shut down the Redis server through the client
127.0.0.1:6379> shutdown
127.0.0.1:6379>

"Redis Data structure – Introduction"

Redis is an advanced Key:value storage system in which value supports five types of data:

1. String (strings)
2. List of strings (lists)
3. Collection of strings (sets)
4. Ordered collection of strings (sorted sets)
5. Hash (hashes)

And on key, there are a few points to remind everyone:

1.key not too long, try not to exceed 1024 bytes, which not only consumes memory, but also reduce the efficiency of the search;
2.key is also not too short, too short, key readability will be reduced;
3. In a project, the key is best to use a uniform naming scheme, such as USER:10000:PASSWD.

"Redis Data Structure –strings"

It is said that if you use only the string type in Redis and do not use the Redis persistence feature, then Redis is very much like memcache. This shows that the strings type is a very basic data type and is a required data type for any storage system.

Let's take a look at one of the simplest examples:

Copy Code code as follows:

Set mystr "Hello world!"//Set String type
Get MyStr//Read String type



The use of string types is as simple as binary security, so you can store the contents of a picture file as a string.

In addition, we can also use string types for numeric operations:

Copy Code code as follows:

127.0.0.1:6379> set Mynum "2"
Ok
127.0.0.1:6379> Get Mynum
"2"
127.0.0.1:6379> incr Mynum
(integer) 3
127.0.0.1:6379> Get Mynum
"3"

See, when a numeric operation is encountered, Redis converts the string type to a numeric value.

Because the incr and other instructions themselves have the characteristics of atomic operation, so we can use the Redis incr, Incrby, DECR, Decrby and other instructions to achieve the effect of atomic counting, if, in a scenario where 3 clients read the value of Mynum (value 2), Then add 1 to it at the same time, then the last Mynum value must be 5. Many websites use this characteristic of Redis to realize the statistic count demand of the business.

"Redis Data Structure –lists"

Another important data structure of Redis is called lists, translated into Chinese called "list".

The first thing to be clear is that the lists in Redis is not an array on the underlying implementation, but a linked list, which means that for a lists with millions of elements, the time complexity of inserting a new element at the head and tail is constant. For example, insert new elements with Lpush in the lists head of 10 elements, and the speed of inserting new elements in the lists head of tens of millions of elements should be the same.

Although lists has such advantages, but also has its drawbacks, that is, the lists of the linked list of elements will be relatively slow, and array-type lists element positioning will be much faster.

The common operations of lists include Lpush, Rpush, Lrange, etc. We can insert a new element on the left side of the lists with Lpush, insert a new element rpush the right side of the lists, and use the Lrange command to specify a range from the lists to extract the elements. Let's look at a few examples:

Copy Code code as follows:

Create a new list called MyList, and insert the element "1" in the head of the list
127.0.0.1:6379> Lpush mylist "1"
Returns the number of elements in the current mylist
(integer) 1
Insert Element "2" to the right of MyList
127.0.0.1:6379> Rpush mylist "2"
(integer) 2
Insert Element "0" to the left of MyList
127.0.0.1:6379> Lpush mylist "0"
(integer) 3
List elements from numbered 0 to number 1 in MyList
127.0.0.1:6379> Lrange mylist 0 1
1) "0"
2) "1"
List MyList from number 0 to penultimate element
127.0.0.1:6379> Lrange mylist 0-1
1) "0"
2) "1"
3) "2"

The application of lists is quite extensive, just give a few examples:

1. We can use lists to implement a message queue, and we can ensure that the sequence, not as MySQL does not need to be ordered by the order by.
2. The use of Lrange can also be very convenient to implement the paging function.
3. In the blog system, comments for each blog post can also be stored in a separate list.

"Redis Data Structure – collection"

A collection of Redis, an unordered collection in which the elements in a collection are not sequential.

Collection-related operations are also rich, such as adding new elements, deleting existing elements, taking intersections, taking and collecting sets, taking difference sets, and so on. Let's look at the example:

Copy Code code as follows:

Add a new element "one" to the collection MySet
127.0.0.1:6379> sadd MySet "one"
(integer) 1
127.0.0.1:6379> sadd MySet "two"
(integer) 1
List all elements in the collection MySet
127.0.0.1:6379> smembers MySet
1) "One"
2) "Two"
To determine if element 1 is in the set MySet, returning 1 indicates that the
127.0.0.1:6379> sismember MySet "one"
(integer) 1
Determines whether element 3 is in the collection MySet, and returns 0 to indicate that it does not exist
127.0.0.1:6379> sismember MySet "three"
(integer) 0
Create a new collection Yourset
127.0.0.1:6379> sadd yourset "1"
(integer) 1
127.0.0.1:6379> sadd Yourset "2"
(integer) 1
127.0.0.1:6379> smembers Yourset
1) "1"
2) "2"
Ask for a set of two sets
127.0.0.1:6379> sunion MySet Yourset
1) "1"
2) "One"
3) "2"
4) "Two"

There are also some common ways to use collections, such as QQ has a social function called "friend Tag", you can give your friends stickers, such as "Big Beautiful", "tyrants", "Obadhai" and so on, then you can use the Redis set to achieve, each user's tag is stored in a collection.

"Redis data structure – ordered set"

Redis not only provides no need to set (sets), but also very considerate to provide an ordered set (sorted sets). Each element in an ordered set is associated with an ordinal number (score), which is the basis for sorting.

Most of the time, we call the ordered set in the Redis as Zsets, because in Redis, the instructions for an ordered set are all started with Z, such as Zrange, Zadd, Zrevrange, Zrangebyscore, and so on.

As usual, let's take a look at some vivid examples:
Add an ordered set of Myzset and add an element baidu.com, giving it an ordinal number 1:

Copy Code code as follows:

127.0.0.1:6379> zadd Myzset 1 baidu.com
(integer) 1
Add an element 360.com to the myzset and give it an ordinal number of 3
127.0.0.1:6379> Zadd Myzset 3 360.com
(integer) 1
Add an element google.com to the Myzset and give it an ordinal number of 2
127.0.0.1:6379> Zadd Myzset 2 google.com
(integer) 1
Listing all the elements of Myzset and listing their serial numbers, you can see that Myzset is already in order.
127.0.0.1:6379> Zrange Myzset 0-1 with scores
1) "Baidu.com"
2) "1"
3) "Google.com"
4) "2"
5) "360.com"
6) "3"
Only Myzset elements are listed
127.0.0.1:6379> Zrange Myzset 0-1
1) "Baidu.com"
2) "Google.com"
3) "360.com"

"Redis Data structure – hash"

Finally, we would like to introduce the hashes, that is, the hash. Hashes are data structures that are only available after the redis-2.0.0 version.

Hashes is a mapping between string and string values, such as a user who wants to store their full name, last name, age, and so on, and is well suited to use hashes.

Let's take a look at an example:

Copy Code code as follows:

Create a hash, and assign a value
127.0.0.1:6379> hmset user:001 username antirez password P1pp0 age 34
Ok
List the contents of the hash
127.0.0.1:6379> Hgetall user:001
1) "username"
2) "Antirez"
3) "Password"
4) "P1pp0"
5) "Age"
6) "34"
Change one of the values in a hash
127.0.0.1:6379> hset user:001 Password 12345
(integer) 0
List the contents of the hash again
127.0.0.1:6379> Hgetall user:001
1) "username"
2) "Antirez"
3) "Password"
4) "12345"
5) "Age"
6) "34"

The operation of the hashes is also very rich, when needed, you can query from here.

"Talk about Redis persistence – two Ways"

Redis provides two ways to persist, namely RDB (Redis DataBase) and aof (Append only File).

RDB, in short, is at different points of time, the data stored in the Redis to generate snapshots and storage to disk and other media;

AOF, it is to change an angle to achieve the persistence, that is, the Redis executed all write instructions recorded, in the next Redis restart, as long as the written instructions to repeat the past, and then repeated, you can achieve data recovery.

In fact, RDB and aof two ways can also be used, in this case, if the Redis reboot, will be preferred to use the AoF method for data recovery, because the aof way of data recovery integrity is higher.

If you don't have data persistence requirements, you can also turn off RDB and aof, so that Redis will become a pure memory database, just like memcache.

"Talk about Redis persistence –rdb"

The RDB approach is to persist data from a redis moment to disk, which is a snapshot-style persistence method.

Redis in the process of data persistence, the data is written to a temporary file, and the temporary file is used to replace the last persisted file until the persistence process is over. It is this feature that allows us to back up at any time because the snapshot files are always fully available.

For the RDB approach, Redis creates (fork) a subprocess to persist, and the main process does not perform any IO operations, which ensures a redis high performance.

If large-scale data recovery is required and is not very sensitive to the integrity of data recovery, the RDB approach is more efficient than the AOF approach.

Although RDB has many advantages, its shortcomings are not to be overlooked. If you are sensitive to the integrity of your data, the RDB approach is not appropriate for you, because even if you persist every 5 minutes, there will still be nearly 5 minutes of data loss when the Redis fails. So, Redis also provides another way to persist, which is aof.

"Talk about Redis persistence –aof"

AOF, English is append only file, that is, only allow appending files that are not allowed to be overwritten.

As described earlier, the AOF Way is to record the written instructions that have been executed, and to perform the instructions once again in the order of data recovery, which is so simple.

We can turn on AOF functionality by configuring the AppendOnly Yes in redis.conf. If there is a write operation such as set, Redis is appended to the end of the aof file.

The default AOF persistence strategy is fsync once per second (Fsync refers to recording the write instructions in the cache to disk), because in this case, Redis can still maintain good processing performance, even if the Redis failure, will only lose the last 1 seconds of data.

If the log is not complete and is not related to a situation where disk space is full, an inode full, or a power outage occurs when the log is appended, Redis provides a redis-check-aof tool that can be used for log repair.

Because of the Append method, if you do not do any processing, the AoF file will become larger, for this reason, Redis provides aof file rewriting (rewrite) mechanism, that is, when the aof file size exceeds the threshold set, Redis will start aof file content compression, Only the minimum set of instructions that can recover data is retained. For example, perhaps more image, if we call 100 times INCR instructions, in the aof file to store 100 instructions, but this is obviously very inefficient, it can be combined with the 100 instructions to a set instruction, which is the principle of rewriting mechanism.

In the AoF rewrite, still use the first write temporary files, complete and then replace the process, so power outages, disk full and other issues will not affect the availability of aof files, this can be assured.

Another benefit of the AOF approach is illustrated by a "scene reappearance". A classmate in the operation of Redis, accidentally executed the flushall, resulting in Redis in the memory of all the data is emptied, this is a very tragic thing. But this is not the end of the world, as long as Redis is configured with aof persistence and aof files have not been rewritten (rewrite), we can pause Redis and edit aof files at the fastest speed, delete the last line of Flushall commands, and then restart Redis, You can restore all the Redis data to the state before Flushall. is not very magical, this is one of the benefits of the aof way of persistence. But if the aof file has been rewritten, then it is not possible to recover the data in this way.

Although the advantages are many, but the aof way also has the flaw, for instance in the same data scale case, the aof file is bigger than the Rdb file size. Also, the AOF approach is slower to recover than the RDB approach.

If you execute the bgrewriteaof command directly, Redis generates a completely new aof file, which includes a minimal set of commands that can restore the existing data.

If the luck is worse, aof file appears to be written bad situation, also need not excessively worry, redis and not rashly load this problem aof file, but error exit. You can then repair the error file by following these steps:

1. Back up the AoF file that was written bad
2. Run Redis-check-aof–fix for repair
3. Use Diff-u to look at the differences between the two documents, identify the problem point
4. Reboot the Redis, load the repaired aof file

"Chat redis persistent –aof rewrite"

AOF rewrite the internal workings of the principle, we need to understand.

At the beginning of the rewrite, Redis creates (fork) an "rewrite subprocess", which reads the existing AOF file first, analyzes and compresses the instructions it contains, and writes it to a temporary file.

At the same time, the main worker process accumulates the newly received write instruction to the memory buffer while continuing to write to the original aof file, which guarantees the availability of the original aof file and avoids surprises during the rewrite process.

When the rewrite child process completes the rewrite work, it sends a signal to the parent process, which appends the cached write instruction in memory to the new AoF file after the parent process receives the signal.

When the append is over, Redis will replace the old aof file with the new AoF file, and then the new write instruction will be appended to the new AoF file.

"Talk about Redis persistence – how to choose Rdb and AoF"

For us to choose RDB or AOF, the official proposal is to use both. This provides a more reliable solution to persistence.

"Talk about master-slave-usage"

Like MySQL, Redis supports master-slave synchronization, as well as a multiple-and multilevel-from structure.

Master-Slave structure, one is for pure redundant backup, the second is to improve read performance, such as the very consuming performance of the sort can be borne by the server.

The master-slave synchronization of Redis is asynchronous, which means that master-slave synchronization does not affect the main logic and does not degrade the processing performance of Redis.

In the master-slave architecture, you can consider shutting down the data persistence of the primary server, allowing only the server to be persisted, thus improving the processing performance of the primary server.

In the master-slave architecture, from the server is usually set to read-only mode, so that the data from the server can be avoided incorrectly modified. However, from the server can still accept the instructions such as config, so should not be directly exposed from the server to the insecure network environment. If this is the case, consider renaming the important instructions to avoid the order being executed by outsiders.

"Talk about master-slave-sync principle"

The sync instructions are issued from the server to the primary server, and when the primary server receives this command, the bgsave instruction is invoked to create a child process dedicated to data persistence, that is, the primary server's data is written to the Rdb file. During data persistence, write instructions that the primary server will execute are slow to exist in memory.

After the bgsave instruction is completed, the master server sends the persisted RDB file to the server from which it is stored to disk after it is received from the server, which is then read into memory. When this action is complete, the primary server sends the write instructions cached by this time to the Redis protocol in the format of the server.

Also, one thing to say is that even if multiple sync instructions are sent from the server at the same time, the primary server will only perform a bgsave and then send the Rdb file to multiple downstream. Prior to the redis2.8 version, if the server and the primary server were disconnected for some reason, a master-slave data synchronization was performed, and after the 2.8 release, Redis supported a more efficient incremental synchronization strategy, which greatly reduced the cost of recovery from connection disconnection.

The primary server maintains a buffer in memory in which the content to be sent from the server is stored. From the server after the network transient with the primary server, the server will try to connect again with the primary server, once the connection is successful, from the server will "want to sync the primary server ID" and "The desired data offset (replication offset)" sent out. When a primary server receives such a synchronization request, it first verifies that the primary server ID matches its own ID, and then checks whether the "requested offset" exists in its own buffer, and if both are satisfied, the primary server sends incremental content to the server.

Incremental sync, requiring server-side support for new Psync directives. This instruction is only available after redis-2.8.

"Talk about Redis transactions."

As we all know, business refers to "a complete action, or all the implementation, or nothing to do."

Before talking about Redis transaction processing, we must first introduce four Redis instructions, namely multi, EXEC, discard, WATCH. These four directives form the basis of REDIS transaction processing.

1.MULTI is used to assemble a transaction;
2.EXEC is used to perform a transaction;
3.DISCARD is used to cancel a transaction;
4.WATCH is used to monitor some keys, and once they are changed before the transaction is executed, the execution of the transaction is canceled.

On paper, let's take a look at a multi and exec example:

Copy Code code as follows:

Redis> MULTI//Mark Transaction start
Ok
redis> INCR user_id//multiple commands in sequence
QUEUED
Redis> INCR user_id
QUEUED
Redis> INCR user_id
QUEUED
Redis> PING
QUEUED
Redis> EXEC//Executive
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG

In the above example, we see the typeface of queued, which means that when we assemble the transaction with multi, each command is cached in the memory queue, and if queued indicates that the command was successfully inserted into the cache queue, when the exec is executed in the future, These queued commands are assembled into a single transaction to execute.

For the execution of a transaction, if the Redis turns on aof persistence, then once the transaction is successfully executed, the commands in the transaction are written to disk at once, and if there are problems with power outages, hardware failures, and so on while writing to disk, Then there is the possibility that only some of the commands are aof persisted, and then the aof file will be incomplete, and then we can use the Redis-check-aof tool to fix the problem, which will remove the incomplete information from the AoF file and ensure that the AoF file is fully available.

For a transaction, you will often encounter two types of errors:

1. Errors prior to calling exec
2. Invoke the error after exec

"Errors prior to calling exec" may have been caused by incorrect syntax or, possibly, due to insufficient memory. Whenever a command fails to write to the buffer queue, Redis records that Redis will refuse to perform the transaction when the client invokes exec. (This is the policy after version 2.6.5.) In the previous version of 2.6.5, Redis would ignore those commands that failed to join the team and only execute those commands that were successful on the team. Let's look at an example of this:

Copy Code code as follows:

127.0.0.1:6379> Multi
Ok
127.0.0.1:6379> haha//an obvious wrong instruction
(Error) ERR unknown command ' haha '
127.0.0.1:6379> Ping
QUEUED
127.0.0.1:6379> exec
Redis mercilessly rejected the execution of the transaction because "an error occurred before"
(Error) Execabort Transaction discarded because of previous errors.

With "Invoke exec error", Redis takes a completely different strategy, that is, Redis ignores the errors and continues to execute the other commands in the transaction down. This is because the application-level error is not a problem that Redis itself needs to consider and handle, so if a single command fails in a transaction, it does not affect the execution of the other commands that follow. Let us also take a look at an example:

Copy Code code as follows:

127.0.0.1:6379> Multi
Ok
127.0.0.1:6379> Set age 23
QUEUED
Age is not a collection, so the following is an apparently incorrect instruction
127.0.0.1:6379> Sadd age 15
QUEUED
127.0.0.1:6379> Set Age 29
QUEUED
127.0.0.1:6379> exec//execute transaction, Redis ignore the 2nd instruction execution error
1) OK
2) (Error) Wrongtype Operation against a key holding the wrong of value
3) OK
127.0.0.1:6379> Get Age
"29"//You can see that the 3rd instruction was executed successfully.

Well, let's say the last instruction, "WATCH," is a very useful instruction that can help us achieve an effect similar to "optimistic lock", that is, CAS (check and set).

Watch itself is the role of "monitoring key has been changed", but also to support the monitoring of multiple keys at the same time, as long as not really trigger the transaction, watch will be responsible for monitoring, once found that a key has been modified, in the executive Exec will return nil, indicating that the transaction can not trigger.

Copy Code code as follows:

127.0.0.1:6379> Set age 23
Ok
127.0.0.1:6379> Watch age//Start monitoring age
Ok
127.0.0.1:6379> set age 24//before Exec, the age value was modified
Ok
127.0.0.1:6379> Multi
Ok
127.0.0.1:6379> Set age 25
QUEUED
127.0.0.1:6379> Get Age
QUEUED
127.0.0.1:6379> exec//trigger EXEC
(nil)//transaction cannot be executed

"Teach you to read Redis configuration – Introduction"

We can specify the configuration file that should be loaded when Redis-server is started, as follows:

Copy Code code as follows:

$./redis-server/path/to/redis.conf

Next, we'll explain the implications of the various configuration items for the Redis configuration file, and note that this article is based on the redis-2.8.4 version.

Redis official redis.conf file, 700+ line, where more than 100 acts of effective configuration lines, the other 600 more behavior annotation description.

At the beginning of the configuration file, you first identify some units of measure:

Copy Code code as follows:

# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1GB => 1024*1024*1024 bytes

As you can see, the Redis configuration is insensitive to the case of units, 1GB, 1Gb, and 1gB are the same. This also shows that Redis only supports bytes, and does not support bit units.

Redis supports the introduction of external profiles in the master configuration file, much like the include directives in C + +, such as:

Copy Code code as follows:

Include/path/to/other.conf

If you've seen the Redis profile, you'll find that it's still very organized. The Redis configuration file is divided into a few large areas, respectively:

1. General
2. Snapshot (snapshotting)
3. Replication (Replication)
4. Safety (Security)
5. Restrictions (Limits)
6. Append mode (append only mode)
7.LUA script (Lua scripting)
8. Slow logs (slow log)
9. Notification of events (event notification)

Let's go through each of these.

"Teach you to understand Redis configuration-General"

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

Copy Code code as follows:

Daemonize No



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


Copy Code code as follows:

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 entry, such as:


Copy Code code as follows:

Bind 192.168.1.2 10.8.4.2



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


Copy Code code as follows:

Port 6379



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


Copy Code code as follows:

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 Limit" by timeout, 0 means never close.

Copy Code code as follows:

Timeout 0



TCP connection protection policy, can be set up by tcp-keepalive configuration items, in seconds, if set to 60 seconds, the server will be every 60 seconds to connect to the idle client to initiate an ACK request to check whether the client has been hung out, For clients that are not responding, their connections are closed. So it takes a maximum of 120 seconds to close a connection. If set to 0, no live detection is performed.


Copy Code code as follows:

Tcp-keepalive 0



Redis supports setting the log level by LogLevel configuration entries, divided into four levels, that is, debug, verbose, notice, warning.


Copy Code code as follows:

LogLevel Notice



Redis also supports setting the build location of log files through logfile configuration entries. If set to an empty string, Redis outputs the log to standard output. If you set the log to output to standard output in a daemon case, the log will be written to the/dev/null.


Copy Code code as follows:

LogFile ""



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


Copy Code code as follows:

Syslog-ident Redis



It also supports the designation of a syslog device, which can be either user or LOCAL0-LOCAL7. The use of the Syslog service itself can be referenced specifically.


Copy Code code as follows:

Syslog-facility local0



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


Copy Code code as follows:

Databases 16



The number of these 16 databases will be 0 to 15. The default database is the database with number 0. The user can use select <DBid> to select the appropriate database.

"Teach you to read Redis configuration – Snapshots"

Snapshots, which are primarily concerned with Redis RDB-related configuration, let's take a look.

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

Copy Code code as follows:

Save <seconds> <changes>



For example:


Copy Code code as follows:

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

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

Save 60 10000//indicates that there are at least 10,000 key changes per 60 seconds, triggering a persistent




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


Copy Code code as follows:

Save ""



If a user turns on the Rdb snapshot feature, if a failure occurs when Redis data is persisted to disk, Redis will stop 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 on the disk are inconsistent. If Redis disregard this inconsistency, and continue to receive written requests, it may cause some disastrous consequences.

If the next rdb is persisted successfully, Redis will automatically resume accepting write requests.

Of course, if you don't care about this data inconsistency or if there are other ways to detect and control this inconsistency, you can completely turn off this feature to ensure that Redis continues to accept new write requests when the snapshot write fails. The configuration items are as follows:

Copy Code code as follows:

Stop-writes-on-bgsave-error Yes



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


Copy Code code as follows:

Rdbcompression Yes



After the snapshots are stored, we can also have Redis use the CRC64 algorithm for data validation, but doing so increases the performance cost by approximately 10%, and you can turn off this feature if you want to get the maximum performance boost.


Copy Code code as follows:

Rdbchecksum Yes



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


Copy Code code as follows:

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:


Copy Code code as follows:

Dir./

"Teach you to understand Redis configuration – copy"

Redis provides a master-slave synchronization function.

The Slaveof configuration item controls the location of a redis as another redis from the server, by assigning IP and ports to the main Redis. In general, we recommend that users set a cycle of snapshot persistence for different frequencies from Redis, or configure a different service port from Redis, and so on.

Copy Code code as follows:

Slaveof <masterip> <masterport>



If the primary redis has a validation password set (using Requirepass), the Masterauth is used to set the checksum password in the configuration from Redis, otherwise the primary Redis rejects the access request from the Redis.


Copy Code code as follows:

Masterauth <master-password>



When the connection to the primary Redis is lost from the Redis, or when the master-slave synchronization is in progress, how can Redis handle external access requests? Here, there are two options from Redis:

The first option: If Slave-serve-stale-data is set to Yes (the default), the client's read and write requests continue 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" from the client and, of course, with exceptions, when the client sends the info request and the slaveof request, It will be processed from the Redis.

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

Copy Code code as follows:

Slave-read-only Yes



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


Copy Code code as follows:

Rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52



From Redis will periodically ping the main Redis the package. You can control the cycle by Repl_ping_slave_period instructions. The default is 10 seconds.


Copy Code code as follows:

Repl-ping-slave-period 10



During master-slave synchronization, there may be timeouts in these cases:

1. From a redis point of view, when there is a large-scale IO transmission.
2. From a redis point of view, when data transfer or ping, the main redis timeout
3. From the point of view of the primary Redis, when replying to a ping from Redis, timeout from Redis

The user can set the time limit for the above timeout, but make sure that the time limit is larger than the Repl-ping-slave-period value, otherwise each time the primary Redis will assume that the timeout is from Redis.

Copy Code code as follows:

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 the Redis. However, this may increase the latency of some synchronizations, which can reach about 40 milliseconds. If you turn off the tcp_nodelay, the latency of data synchronization decreases, but it consumes more bandwidth. (If you don't know tcp_nodelay, you can come here for a popular science).


Copy Code code as follows:

Repl-disable-tcp-nodelay No



We can also set the synchronization queue length. Queue Length (backlog) is a buffer in the main redis that the primary Redis uses to cache data that should be sent from Redis during a disconnect from Redis. In this way, when you reconnect from the Redis, you don't have to resynchronize the data again, just sync this part of the incremental data.


Copy Code code as follows:

Repl-backlog-size 1MB



If the main Redis waits for some time, or cannot connect to the Redis, the data in the buffer queue is cleared. We can set the length of time the main Redis will wait. If set to 0, it means never clean up. The default is 1 hours.


Copy Code code as follows:

Repl-backlog-ttl 3600



We can give many of the priority from the Redis, in the main redis continuous work is not normal situation, high priority from the Redis will be upgraded to the main redis. The smaller the number, the higher the priority. For example, a primary redis has three from Redis, the priority number is 10, 100, 25, then the number 10 from Redis will be first selected to upgrade the primary Redis. When the priority is set to 0 o'clock, this will never be selected from Redis. The default priority is 100.


Copy Code code as follows:

Slave-priority 100



If the main redis finds that there are more than M connections from the Redis delay greater than n seconds, then the main Redis stops accepting foreign write requests. This is because the redis typically pings every second to the primary Redis, and the master Redis records each time that a ping was last sent from Redis, so the main redis is able to understand every operation from Redis.


Copy Code code as follows:

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



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

"Teach you to understand 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, believe that you will use this function. Because the Redis performance is very high, so can be completed up to 150,000 times per second password attempt, so you had better set a complex enough password, otherwise it is easy to hack.

Copy Code code as follows:

Requirepass Zhimakaimen



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

Redis allows us to rename the Redis instructions, such as changing some of the more dangerous commands to a name and avoiding being wrongly executed. For example, you can change the Config command to a very complex name, so that you can avoid external calls and meet the needs of internal calls:

Copy Code code as follows:

Rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c89



We can even disable the Config command by changing the config name to an empty string:


Copy Code code as follows:

Rename-command CONFIG ""



Note, however, that if you use the AoF method for data persistence, or if you need to communicate with the Redis, changing the name of the directive may cause problems.

"Teach you to understand Redis configuration-Restrictions"

We can set Redis how many clients can be connected at the same time. The default is 10,000 clients. When you cannot set the process file handle limit, Redis will set the current file handle limit to minus 32, because Redis will leave some handles for its own internal processing logic.

If this limit is reached, Redis rejects the new connection request and sends "max number of clients reached" to those connection requesters to respond.

Copy Code code as follows:

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 data in memory based on the removal rule, or if we set "do not allow removal", then Redis returns an error message for instructions that require memory, such as set, Lpush, and so on. However, the instructions for no memory request will still respond normally, such as get and so on.

Copy Code code as follows:

MaxMemory <bytes>



The point to note is that if your redis is the main redis (stating that your redis has from Redis), so when you set the memory use limit, you need to leave some memory space in the system to the synchronization 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: Remove key from expired collection using the LRU algorithm
2.ALLKEYS-LRU: Remove key using LRU algorithm
3.volatile-random: Removes a random key from the expired collection
4.allkeys-random: Remove the Random key
5.volatile-ttl: Remove those keys that have the lowest TTL value, that is, the key that has only recently expired.
6.noeviction: no removal. For write operations, only error messages are returned.

Regardless of which removal rule is used, Redis will return an error message for a write request if there is no suitable key to remove.

Copy Code code as follows:

Maxmemory-policy VOLATILE-LRU



Both the LRU algorithm and the minimum TTL algorithm are not exact algorithms, but are estimated values. So you can set the size of the sample. If Redis by default will check three keys and choose the one that LRU, then you can change the number of this key sample.


Copy Code code as follows:

Maxmemory-samples 3



Finally, we add a message that, until the current version (2.8.4), the written instructions supported by Redis include the following:


Copy Code code as follows:

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

"Teach you to read Redis configuration – Append Mode"

By default, Redis will persist data to disk asynchronously. This pattern has been validated in most applications, but when problems occur, such as a power outage, this mechanism can cause a few minutes of write requests to be lost.

As described in the top half of the blog post, the Append file (Append only) is a better way to keep data consistent. Even when the server is powered down, only 1 seconds of write requests are lost, and even a write request is lost when the Redis process issues 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 this article.

Copy Code code as follows:

AppendOnly No



We can also set the name of the AoF file:


Copy Code code as follows:

Appendfilename "Appendonly.aof"

Fsync () Called to tell the operating system to immediately write cached instructions to disk. Some operating systems will be "immediately", while others will be "as soon as possible".

The Redis supports three different modes:

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

The default is Everysec. The disclosure of data consistency can be referred to in this article.

Copy Code code as follows:

Appendfsync everysec



When the Fsync mode is set to always or everysec, if the background persistence process requires a large disk IO operation, then Redis may be stuck at the Fsync () call. This problem has not been fixed yet, because even if we were to execute Fsync () in another new thread, it would block the synchronous write call.

To alleviate this problem, we can use the following configuration items, so that when Bgsave or bgwriteaof run, Fsync () calls in the main process are blocked. This means that when the other process is refactoring the AoF file, the Redis persistence function fails, as if we set the "Appendsync none". If your redis is sometimes delayed, set the following option to Yes. Otherwise, keep No because this is the safest option to ensure data integrity.

Copy Code code as follows:

No-appendfsync-on-rewrite No



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

Redis works like this: Redis records the AOF size at the time of the last rewrite. If Redis has not been rewritten since its inception, the size of the AoF file at startup will be used as the base value. This benchmark value is compared to the current aof size. If the current aof size exceeds the set growth rate, the override is triggered. Also, you need to set a minimum size to prevent the rewrite from being triggered at aof hours.

Copy Code code as follows:

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



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

"Teach you to read Redis configure –lua script"

The maximum elapsed time of a LUA script is strictly limited, and the unit is milliseconds:

Copy Code code as follows:

Lua-time-limit 5000



If this value is set to 0 or negative, there is no error and no time limit.

"Teach you to read Redis configuration-Slow Log"

Redis Slow logging refers to a system that logs queries over a specified length of time. This length does not include IO operations, such as interaction with the client, sending response content, and so on, but only the time when the query command was actually executed.

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

The unit is microseconds, or 1000000 represents one second. A negative number disables the slow logging feature, while 0 indicates that each command is forced to be logged.

Copy Code code as follows:

Slowlog-log-slower-than 10000



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


Copy Code code as follows:

Slowlog-max-len 128

"Teach you to read Redis configuration – Event Notification"

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

"Teach you to read Redis configuration – Advanced Configuration"

Some configuration items about the hash data structure:

Copy Code code as follows:

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



Some configuration items about the list data structure:


Copy Code code as follows:

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



Configuration items about the collection data structure:


Copy Code code as follows:

Set-max-intset-entries 512



Configuration items for an ordered collection data structure:


Copy Code code as follows:

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



Configuration items about whether to hash again:


Copy Code code as follows:

activerehashing Yes



About control of client output buffering:


Copy Code code 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



Configuration Items About frequency:


Copy Code code as follows:

Hz 10



For configuration items that override AoF


Copy Code code as follows:

Aof-rewrite-incremental-fsync Yes



At this point, the beginning of redis content is over, the content is quite a lot, but relatively basic, this article does not involve Redis cluster, redis working principle, Redis source code, Redis related Lib library and other content, follow-up will be dedicated, everyone please look forward to:

Thank you!

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.