Redis cluster -- master-slave

Redis master-slave

 

Features

 

    1. Master-slave structure, one is for pure redundant backup, and the other is to improve read performance. For example, the sort that consumes a lot of performance can be undertaken by the slave database.       2. The master-slave synchronization is asynchronous, which means that the master-slave synchronization is performed asynchronously. Slave synchronization does not affect the main logic, nor will it reduce the performance of redis       3. In the master-slave architecture, the data persistence function of the master server can be turned off, and only the slave server can be persisted, thus improving the processing performance of the master server       4. Master-slave In the architecture, the slave server is usually set to the system mode, which can avoid the data of the slave server from being modified by mistake. However, the slave server can also receive config commands, so the slave server should not be directly exposed to an insecure network environment. If this is necessary, consider renaming important commands to avoid mis-execution by outsiders

 

 

 

The benefits of master-slave synchronization

 

  • Separation of reading and writing, sharing the pressure
  • Just in case, the main database is down, you can top it from the database

 

 

Master-slave synchronization type

 

  • Full sync
  • Incremental synchronization

 

 

Full sync

It usually occurs in the initialization phase of the Slave. At this time, the Slave needs to copy all the data on the Master.

 

step

    1. Link the main database server from the database and send the SYNC command

    2. The master server receives the SYNC command, starts executing the BGSAVE command, generates the rdb file and uses the buffer to record all commands executed thereafter

    3. After the master server finishes executing BGSAVE, it sends the snapshot file to the slave server, and continues to record commands in the buffer during sending

    4. After receiving the snapshot file from the server, discard all old data and load the received snapshot

    5. After the master server sends the snapshot, it starts to send the write command in the buffer to the slave server

    6. After the slave server completes the loading of the snapshot, it starts to receive command requests and executes the write command from the master server buffer

 

 

 

Incremental synchronization

Refers to the process of synchronizing the write operation of the master server to the slave server when the slave starts to work normally after initialization.

 

step

    1. When the master server executes a write command, it will send the same write command to the slave server

    2. Receive and execute the received write command from the server

 

 

Master-slave synchronization strategy

 

       主从刚刚连接时,进行全量同步,同步结束后进行增量同步。同样,如果有需要,Slave在任何时候都可以发起全量同步。redis的策略是,首先尝试增量同步,如果不成功再进行增量同步。

 

 

主从配置

Redis主从结构支持一主多从

主节点:192.168.33.130

从节点:192.168.33.131

注意:所有从节点的配置都一样

 

方式1:手动修改配置文件

只需要额外修改从节点中redis的配置文件中的slaveof属性即可

slaveof 192.168.33.130 6379  



 

1、192.168.33.130主机:启动130主节点上面的redis,查看redis的info信息



 

2、192.168.33.131主机:启动131从节点上面的redis,查看redis的info信息



 

 

 

方式2:动态设置

通过redis-cli 连接到从节点服务器,执行下面命令即可。

slaveof 192.168.33.130 6379

演示结果和手动方式一致。

 

 

注意事项

1. 如果你使用主从复制,那么要确保你的master激活了持久化,或者确保它不会在当掉后自动重启。

原因:

slave是master的完整备份,因此如果master通过一个空数据集重启,slave也会被清掉。

 

2. 在配置redis复制功能的时候如果主数据库设置了密码,需要在从数据的配置文件中通过masterauth参数设置主数据库的密码,这样从数据库在连接主数据库时就会自动使用auth命令认证了。相当于做了一个免密码登录。

 

 

参考

http://blog.csdn.net/u011204847/article/details/51307044

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326543456&siteId=291194637