Redis-master/slave (master/slave mode)

Source: Internet
Author: User
Tags redis redis cluster


Like MySQL's master-slave mode, Redis's master-slave can improve the usability of the system, and the master node is automatically synced to slave when it is written to the cache.



Environment:



Master node:10.6.144.155:7030



Slave node:10.6.144.156:7031






First, the configuration



You only need to modify the configuration on slave node:



Find slaveof This line, refer to the following changes (fill in the IP and port of master node is finished)



Slaveof 10.6.144.155 7030



Also note under Slave-read-only yes this line, this means that slave read-only not write, is also recommended settings






Second, verification



Start Master/slave The Redis on these two machines and add a cache entry to master






Then remove the cache entry on the slave






This indicates that the cache on master is automatically copied to the slave node.






Third, Redisson client use


1 @Test
 2 public void testRedisson () {
 3 Config config = new Config ();
 4
 5 config.useMasterSlaveConnection (). SetMasterAddress ("10.6.144.155:7030")
 6 .addSlaveAddress ("10.6.144.156:7031");
 7
 8 Redisson redisson = Redisson.create (config);
 9 
10 String key = "test";
11
12 RBucket <String> myObj = redisson.getBucket (key);
13 if (myObj! = Null) {
14 myObj.delete ();
15}
16
17 myObj.set ("hello World");
18
19 System.out.println (myObj.get ()); // The breakpoint stops here, and then the master is down without affecting the reading
20
21 myObj.set ("new Value"); // Resume the master, down the slave, without affecting the write
twenty two         
23 System.out.println (myObj.get ()); // Resume the slave, and then the master can be read normally
twenty four         
25 myObj.set ("can not write to master"); // master is down and cannot be written
26
27 System.out.println (myObj.get ()); // Resume the master, down the slave, and cannot read
28
29
30 redisson.shutdown ();
31
32
33} 


Note: The actual test, the Redisson client in the set write operation, the connection is the master Node,get read operation is connected to slave, and encountered an operation failure, will be synchronous blocking the subsequent processing (wait until timeout)



That is, when set operation, at least one can connect the master,get operation, must have at least one to be able to connect the slave



Fourth. Jedis Client Use


 1     @Test
 2     public void testJedis() throws InterruptedException {
 3         
 4         Jedis jedisMaster = new Jedis("10.6.144.155", 7030);
 5         
 6         String key = "a";
 7         
 8         String a= jedisMaster.get(key);
 9         if (a!=null){
10             jedisMaster.del(key);
11         }
12         jedisMaster.set(key, "hi!");//向master写入
13         jedisMaster.close();
14         
15 
16         Jedis jedisSlave = new Jedis("10.6.144.156", 7031);// 连接slave
17         
18         jedisSlave.slaveof("10.6.144.155", 7030);// 指定master        
19 
20         System.out.println(jedisSlave.get(key));
21         
22         jedisSlave.slaveofNoOne();//master如果down挂,可以用代码将slave提升为master(即:可写)
23 
24         jedisSlave.set(key, "new");
25 
26         System.out.println(jedisSlave.get(key));
27 
28         jedisSlave.close();
29 
30     }1 @Test
  2 public void testJedis () throws InterruptedException {
  3
  4 Jedis jedisMaster = new Jedis ("10.6.144.155", 7030);
  5
  6 String key = "a";
  7
  8 String a = jedisMaster.get (key);
  9 if (a! = Null) {
10 jedisMaster.del (key);
11}
12 jedisMaster.set (key, "hi!"); // Write to master
13 jedisMaster.close ();
14
15
16 Jedis jedisSlave = new Jedis ("10.6.144.156", 7031); // Connect the slave
17
18 jedisSlave.slaveof ("10.6.144.155", 7030); // specify the master
19
20 System.out.println (jedisSlave.get (key));
twenty one         
22 jedisSlave.slaveofNoOne (); // If the master is down, you can use the code to promote the slave to master (that is, writable)
twenty three 
24 jedisSlave.set (key, "new");
25
26 System.out.println (jedisSlave.get (key));
27
28 jedisSlave.close ();
29
30}





PostScript: Personal feeling Master-slave mode function is limited, because from two kinds of client test results, in the absence of other monitoring mechanism, whether it is the master or slave hangs, you need to manually adjust the configuration or modify the code to ensure that read/write continue to work properly, Ideally, master hangs up, and it is best to slave automatically (without human intervention) to elect one to serve as master. The cluster feature of Redis is currently in beta, and it is estimated that after the cluster feature is officially released, the Redis cluster can be built by Custer function.


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.