Redis is a distributed NoSQL Database System Oriented to "key/value" pairs of data types. It features high performance and persistent storage, and is suitable for high-concurrency application scenarios. It started late and developed rapidly. It has been adopted by many large organizations, such as google, taobao, and sina Weibo. This article does not provide an in-depth description of its data structure, but only introduces its installation and configuration of the master-slave structure. For more information, see the official website. Redis Installation
Currently, Redis only supports Linux, BSD, and other systems. It does not support Windows installation. The Installation Process in Linux is as follows:
$ Wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz $ Tar xzf redis-2.2.12.tar.gz $ Redis-2.2.12 cd $ Make |
The prompt after successful installation is:
Hint: To run 'make test' is a good idea ;) Make [1]: Leaving directory '/usr/alibaba/install/redis-2.2.12/src' |
After the installation is complete, you can start the service under src of the project:
Perform the test using the built-in build-cli tool. The test process is as follows:
$ Src/redis-cli Redis> set foo bar OK Redis> get foo "Bar" Redis> getset foo 123 "Bar" Redis> get foo 123 |
Distribution of master-slave configuration machines in Redis
The machines involved in the configuration of the master-slave structure and the functions completed by each machine are as follows:
IP address |
Role |
10.20.150.205 |
Master Node |
10.20.150.208 |
Slave Node 1 |
10.20.150.209 |
Slave Node 2 |
Master-slave Structure Configuration
Before configuring the master-slave structure, make sure that the Redis service has been installed on the above three machines. The configuration of the Redis master-slave structure is relatively simple. You only need to modify the redis. conf file and configure the IP address and port corresponding to the master node. The details are as follows:
Port 6380 Bind 10.0000150.205 Slaveof 10.0000150.205 6379 |
L port 6380 indicates the port number corresponding to the current Redis service;
L bind 10.0000150.205 indicates the IP address of the master node bound to the current Redis;
L slaveof 10.0000150.205 6379 indicates the slave node of the current Redis master node, including the IP address and port number.
When the master node is started
When only one master node is started, the following information is displayed:
[29028] 26 Sep 18:39:12 * Server started, Redis version 2.2.12 [29028] 26 Sep 18:39:12 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect. [29028] 26 Sep 18:39:12 * DB loaded from disk: 0 seconds [29028] 26 Sep 18:39:12 * The server is now ready to accept connections on port 6379 [29028] 26 Sep 18:39:12-0 clients connected (0 slaves), 790584 bytes in use |
When a slave node 10.20.150.208 is started
The master node automatically detects that a new slave node is started and added. The following information is displayed:
[29028] 26 Sep 18:40:31-Accepted 10.0000150.208: 44779 [29028] 26 Sep 18:40:31 * Slave ask for synchronization [29028] 26 Sep 18:40:31 * Starting BGSAVE for SYNC [29028] 26 Sep 18:40:31 * Background saving started by pid 29091 [29091] 26 Sep 18:40:31 * DB saved on disk [29028] 26 Sep 18:40:31 * Background saving terminated with success [29028] 26 Sep 18:40:31 * Synchronization with slave succeeded [29028] 26 Sep 18:40:32-0 clients connected (1 slaves), 798752 bytes in use [29028] 26 Sep 18:40:37-0 clients connected (1 slaves), 798752 bytes in use [29028] 26 Sep 18:40:42-0 clients connected (1 slaves), 798752 bytes in use [29028] 26 Sep 18:40:47-0 clients connected (1 slaves), 798752 bytes in use |
The slave node displays the following information:
# [597] 26 Sep 18:40:29 * Server started, Redis version 2.2.12 [597] 26 Sep 18:40:29 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect. [597] 26 Sep 18:40:29 * The server is now ready to accept connections on port 6379 [597] 26 Sep 18:40:29-0 clients connected (0 slaves), 790616 bytes in use [597] 26 Sep 18:40:29 * Connecting to MASTER... [597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync started: SYNC sent [597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: Starting ing 10 bytes from master [597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: Loading DB in memory [597] 26 Sep 18:40:29 * MASTER <-> SLAVE sync: Finished with success [597] 26 Sep 18:40:34-1 clients connected (0 slaves), 798744 bytes in use |
When another slave machine is mounted 10.20.150.209
When another slave machine is mounted to the host, the host displays the following information:
[29028] 26 Sep 18:42:52-0 clients connected (1 slaves), 798752 bytes in use [29028] 26 Sep 18:42:57-0 clients connected (1 slaves), 798752 bytes in use [29028] 26 Sep 18:42:58-Accepted 10.0000150.209: 47650 [29028] 26 Sep 18:42:58 * Slave ask for synchronization [29028] 26 Sep 18:42:58 * Starting BGSAVE for SYNC [29028] 26 Sep 18:42:58 * Background saving started by pid 29287 [29287] 26 Sep 18:42:58 * DB saved on disk [29028] 26 Sep 18:42:59 * Background saving terminated with success [29028] 26 Sep 18:42:59 * Synchronization with slave succeeded [29028] 26 Sep 18:43:02-0 clients connected (2 slaves), 806920 bytes in use |
The following information can be seen from another slave machine:
[Intranet root @ xasc-150-209/usr/alibaba/install/redis-2.2.12/src] #./Redis-server ../redis. conf [15034] 26 Sep 18:42:56 * Server started, Redis version 2.2.12 [15034] 26 Sep 18:42:56 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect. [15034] 26 Sep 18:42:56 * The server is now ready to accept connections on port 6379 [15034] 26 Sep 18:42:56-0 clients connected (0 slaves), 790616 bytes in use [15034] 26 Sep 18:42:56 * Connecting to MASTER... [15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync started: SYNC sent [15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: Starting ing 10 bytes from master [15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: Loading DB in memory [15034] 26 Sep 18:42:56 * MASTER <-> SLAVE sync: Finished with success [15034] 26 Sep 18:43:01-1 clients connected (0 slaves), 798744 bytes in use |
When the primary node goes down
When the master node suddenly becomes unavailable, the two slave nodes report an error because they cannot find the master node. The information is as follows:
[597] 26 Sep 18:46:09 * Connecting to MASTER... [597] 26 Sep 18:46:09 # Unable to connect to MASTER: Connection refused [597] 26 Sep 18:46:10 * Connecting to MASTER... [597] 26 Sep 18:46:10 # Unable to connect to MASTER: Connection refused [597] 26 Sep 18:46:11 * Connecting to MASTER... [597] 26 Sep 18:46:11 # Unable to connect to MASTER: Connection refused [597] 26 Sep 18:46:12 * Connecting to MASTER... [597] 26 Sep 18:46:12 # Unable to connect to MASTER: Connection refused [597] 26 Sep 18:46:13 * Connecting to MASTER... [597] 26 Sep 18:46:13 # Unable to connect to MASTER: Connection refused [597] 26 Sep 18:46:14-0 clients connected (0 slaves), 790616 bytes in use |
When the master node is not started
When the master node is not started, the slave node is started first. Because the corresponding master node cannot be found, the following error message is returned:
[Intranet root @ xasc-150-208/usr/alibaba/install/redis-2.2.12/src] #./Redis-server ../redis. conf [1842] 26 Sep 18:48:50 * Server started, Redis version 2.2.12 [1842] 26 Sep 18:48:50 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. to fix this issue add'vm. overcommit_memory = 1' to/etc/sysctl. conf and then reboot or run the command 'sysctl vm. overcommit_memory = 1' for this to take effect. [1842] 26 Sep 18:48:50 * DB loaded from disk: 0 seconds [1842] 26 Sep 18:48:50 * The server is now ready to accept connections on port 6379 [1842] 26 Sep 18:48:50-0 clients connected (0 slaves), 790616 bytes in use [1842] 26 Sep 18:48:50 * Connecting to MASTER... [1842] 26 Sep 18:48:50 # Unable to connect to MASTER: Connection refused [1842] 26 Sep 18:48:51 * Connecting to MASTER... [1842] 26 Sep 18:48:51 # Unable to connect to MASTER: Connection refused [1842] 26 Sep 18:48:52 * Connecting to MASTER... [1842] 26 Sep 18:48:52 # Unable to connect to MASTER: Connection refused [1842] 26 Sep 18:48:53 * Connecting to MASTER... [1842] 26 Sep 18:48:53 # Unable to connect to MASTER: Connection refused [1842] 26 Sep 18:48:54 * Connecting to MASTER... [1842] 26 Sep 18:48:54 # Unable to connect to MASTER: Connection refused [1842] 26 Sep 18:48:55-0 clients connected (0 slaves), 790616 bytes in use |
When a slave machine is unavailable
When a slave machine becomes unavailable, the host displays the following information:
32-1 clients connected (0 slaves), 798744 bytes in use [16848] 26 Sep 18:50:37-1 clients connected (0 slaves), 798744 bytes in use [16848] 26 Sep 18:50:42-1 clients connected (0 slaves), 798744 bytes in use |
When both slave machines go down
When both slave machines become unavailable, the host displays the following information:
[29736] 26 Sep 18:52:57-Client closed connection [29736] 26 Sep 18:53:02-0 clients connected (0 slaves), 790584 bytes in use [29736] 26 Sep 18:53:07-0 clients connected (0 slaves), 790584 bytes in use |
Test the master-slave structure to write data to the master node.
Run the write command 1 on the host.
The information displayed on each slave is as follows:
[2, 2528] 26 Sep 18:57:01-DB 0: 1 keys (0 volatile) in 4 slots HT. [2528] 26 Sep 18:57:01-1 clients connected (0 slaves), 798952 bytes in use [2, 2528] 26 Sep 18:57:06-DB 0: 1 keys (0 volatile) in 4 slots HT. [2528] 26 Sep 18:57:06-1 clients connected (0 slaves), 798936 bytes in use |
Run the get foo command on the host. The following information is displayed:
Redis 127.0.0.1: 6379> get foo "Bar" |
Run the get foo command on the slave machine 10.20.150.208. The following information is displayed:
Redis 127.0.0.1: 6379> get foo "Bar" |
Similarly, run the get foo command on the slave machine 10.20.150.209. The following information is displayed:
Redis 127.0.0.1: 6379> get foo "Bar" |
Write Data to the host when one slave fails.
When a slave machine (10.20.150.208) goes down, run the setfoo1 write command on the host.
The following information is displayed when get foo1 is executed on the host:
Redis 127.0.0.1: 6379> get foo1 "Test" |
The following information is displayed when getfoo1 is executed on another slave machine:
Redis 127.0.0.1: 6379> get foo1 "Test" |
When 10.0000150.208 is started from the machine, run the getfoo1 command. The following information is displayed:
Redis 127.0.0.1: 6379> get foo1 "Test" |
It can be seen that, even if the slave fails and starts again, all the modifications made by the host will be synchronized to the restarted slave machine.