Redis tutorial of ix: master slave replication configuration instance

  • 2020-05-06 11:58:24
  • OfStack

Replication:

The first thing to note here is that configuring Master-Slave mode in Redis is really easy. I'm sure you can do that easily after reading Blog. Here we will first list some theoretical knowledge and then give some practical cases.

The following list clearly explains the features and advantages of Redis Replication.

      1). The same Master can synchronize multiple Slaves.
      2). Slave can also accept connection and synchronization requests from other Slaves to effectively distribute the synchronization pressure of Master. So we can think of Redis's Replication architecture as a graph structure.
      3). Master Server serves Slaves in a non-blocking manner. So during Master-Slave synchronization, the client can still submit queries or modify requests.
      4). Slave Server also performs data synchronization in a non-blocking manner. During synchronization, if a client submits a query request, Redis returns the data before synchronization.
      5). In order to share the read operation pressure of Master, the Slave server can provide the service of read-only operation for the client. The write service must still be done by Master. Even so, the scalability of the system has been greatly improved.
      6). Master can delegate the data saving operation to Slaves, thus avoiding the need for a separate process to complete the operation in Master.
     
How works:

      after Slave starts and connects to Master, it sends an SYNC command on its own initiative. After that, Master starts the background save process and collects all the commands it receives to modify the data set. After the background process completes, Master transfers the entire database file to Slave to complete a complete synchronization. The Slave server saves the database file data and loads it into memory after receiving it. After that, Master continues to transmit all the collected change commands and new change commands to Slaves in turn, and Slave will execute these data change commands this time to achieve the final data synchronization.
      if the link between Master and Slave breaks, Slave can automatically reconnect to Master, but after a successful connection, a full synchronization is automatically performed.
     
3. How to configure Replication:

See the following steps for      :
      1). Start two Redis servers at the same time. Consider starting two Redis servers on the same machine to listen to different ports, such as 6379 and 6380.
      2). On Slave server, execute the following command:
 


    /> redis-cli -p 6380   # So let's assume that Slave The port number of 6380
    redis 127.0.0.1:6380> slaveof 127.0.0.1 6379 # We assume that the Master and Slave On the same mainframe, Master The port is 6379
    OK
 

The above approach simply ensures that      , redis_6380 becomes slave to redis_6379 after the slaveof command is executed, and that the replication relationship between them will terminate once the service (redis_6380) is restarted.
If you want to guarantee the long-term Replication relationship between the two servers, you can make the following changes in the configuration file of redis_6380:
 

    /> cd /etc/redis  # switch Redis The directory where the server configuration files are located.
    /> ls
    6379.conf  6380.conf
    /> vi 6380.conf
    will
    # slaveof <masterip> <masterport>
    Instead of
    slaveof 127.0.0.1 6379
 

      save exit.
      this ensures that the Redis_6380 service program actively establishes an Replication connection to Redis_6379 after each startup.
     
4. Examples of application:

      here we assume that Master-Slave has been established.
 


    # Start the master The server.
    [root@Stephen-PC redis]# redis-cli -p 6379
    redis 127.0.0.1:6379>
    # situation Master All in the current database Keys .
    redis 127.0.0.1:6379> flushdb
    OK
    # in Master Create a new Keys As test data.
    redis 127.0.0.1:6379> set mykey hello
    OK
    redis 127.0.0.1:6379> set mykey2 world
    OK
    # To view Master What is in Keys .
    redis 127.0.0.1:6379> keys *
    1) "mykey"
    2) "mykey2"
   
    # Start the slave The server.
    [root@Stephen-PC redis]# redis-cli -p 6380
    # To view Slave In the Keys Whether and Master From the result, they are equal.
    redis 127.0.0.1:6380> keys *
    1) "mykey"
    2) "mykey2"
   
    # in Master Delete one of the tests in Key And view the deleted results.
    redis 127.0.0.1:6379> del mykey2
    (integer) 1
    redis 127.0.0.1:6379> keys *
    1) "mykey"
   
    # in Slave To see if mykey2 Have been in Slave Is deleted.
    redis 127.0.0.1:6380> keys *
    1) "mykey"
 


Related articles: