What is it?
It is the Master / Slave mechanism that we often call Master-Slave replication. After the host data is updated, it is automatically synchronized to the standby machine according to the configuration and policy. The Master is mainly written and the Slave is mainly read
What can I do?
Read write separation
Disaster recovery
How do you play?
With slave (Library) but not master (Library)
Configuration from library
#Configure slave Library slaveof Main library ip Main library port #View master-slave information info replication
Every time you disconnect from the master, you need to reconnect unless you configure redis Conf file
Common master-slave mode
One master and two servants
Meaning: one Master and two Slave
View master-slave information through replication info
# Replication role:master connected_slaves:0 master_replid:f6baff9abfda12ca58048cfce4b0e2c1f4683da1 master_replid2:e8fe596d47d9d1d923d56d884b28128b78d2c1e0 master_repl_offset:0 second_repl_offset:1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
# Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:0 master_link_down_since_seconds:1585217521 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:adbec19afa734e84a333b07ea2f33c43c73fe743 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
be careful:
- The first entry point of slave1 and slave2 is full replication, followed by incremental replication
- The host can write, but the slave cannot write, and the slave can only read
- After the host shuts down, the slave is in standby mode. After the host returns, the new records of the host can be copied smoothly
- After the slave is shut down, it needs to be reconnected every time it is disconnected from the master, unless you configure redis Conf file
- The data copied from the local machine will be persisted by the local machine. Even if the shutdown is disconnected, there will still be data.
- Reconnecting or changing the master will clear the previous data and re-establish and copy the latest data
From generation to generation
Meaning: the previous slave can be the master of the next slave. The slave can also receive connection and synchronization requests from other slaves. Then the slave acts as the next master in the chain, which can effectively reduce the write pressure of the master.
The precautions are similar to that of one master and two servants, but note that although slave is a relative master, it is still slave [view] (#zhu yi)
turn from a guest into a host
SLAVEOF no one
Make the current database stop synchronizing with other databases and convert to the primary database
sentinel mode
The anti guest based automatic version can monitor whether the Master library fails in the background. If it fails, the slave library will be automatically converted to the main library according to the number of votes. A set of sentinel can
Monitor multiple masters at the same time.
Use steps:
- Corresponding to redis.com in the Master Create sentinel.conf under the same directory as conf Conf file, the name must not be wrong;
- Configure sentry at sentinel Fill the contents in the conf file (multiple can be configured):
#Note: the last number 1 means that after the host hangs up, the slave votes to see who will take over as the host and become the host after winning the number of votes. sentinel monitor Name of monitored database (self named) ip port 1
- Start sentinel mode (the path is configured according to your own needs):
redis-sentinel /myredis/sentinel.conf
be careful:
- When the master hangs up, the next master will be elected by ballot. And only sentinel Votes can only be opened when conf is started
- When the original master is later, it unfortunately becomes a slave.
Replication principle
- After Slave is successfully started and connected to the master, it will send a sync command;
- After receiving the command, the master starts the save process and collects all the commands received to modify the dataset. After the background process is executed, the master will transfer the whole data file to the slave to complete a complete synchronization;
- Full copy: the slave service saves the database file data and loads it into memory;
- Incremental replication: the Master continues to transmit all new collected modification commands to the slave in turn to complete the synchronization;
- However, as long as the master is reconnected, a full synchronization (full replication) will be performed automatically.
Disadvantages of replication
Delay: since all write operations are performed on the Master and then synchronously updated to the Slave, there is a certain delay in synchronizing from the Master to the Slave machine. When the system is very busy, the delay problem will be more serious, and the increase in the number of Slave machines will also make this problem more serious.
command
command | effect |
---|---|
slaveof main library ip main library port | Configure slave Library |
info replication | View the master-slave replication of redis |
slaveof no one | Make the current database stop synchronizing with other databases and convert to the primary database |
sentinel monitor name of monitored database (self named) 127.0.0.1 6379 1 | Configure sentry and monitor master |
redis-sentinel /myredis/sentinel.conf | Start redis in sentinel mode |