Redis persistence, master-slave replication and sentinel mode

Table of contents

1. Type

1. RDB mode

2. AOF mode

2. Configuration file and operation

1. The above is the mode of RDB (save the data value, open by default)

2. AOF

3. Redis master-slave replication

1. Concept

Two, the role

3. Disadvantages

4. Process

 5. Master-slave service construction

6. Sentinel mode

1. Concept

Two, the role

3. Disadvantages

Four, build

7. Verification

Eight, build and verify the simulation operation experiment

1. View the configuration file

 2. View and copy, there are several slaves to copy

3. Enter the configuration file

4. After modification, start

 5. Kill all processes

6. Start all processes

 7. Test the master-slave replication after startup to check whether it is normal (open the second terminal page)

8. Start the sentinel and check the status

Nine, enter the third terminal to view the log

10. Kill the process of 6379

11. Verify again whether it is successful


1. Type

1. RDB mode

default persistence

dump.rdb read when the database starts

Trigger conditions When the redis process exits, it will crash/redis program crashes

2. AOF mode

off by default

appendonly.aof is read when the default process starts

Turn on
        appendonly yes
        and use it with RDB at the same time

2. Configuration file and operation

Still in vim /etc/redis.conf

Description of the save storage mechanism

Change one in 15 minutes

Ten articles changed in 5 minutes

10,000 changes in 1 minute

will store

Stop writing when an error occurs

Whether to allow compression

Which file to store data in

Storage path

 Check

 Log in

 

 view is empty

 create and save

 Open a new terminal to view

vim enter

1. The above is the mode of RDB (save the data value, open by default)

2. AOF

is off by default

turn it on

Three data synchronization modes

Additional synchronization adopts always

sync every second

out of sync

Whether to retry when no synchronization is successful

Change the config file and restart the service

Toggle input command monitoring

tail - f appendonly.aof is the watch file

 Nothing is empty now

 Go back to new terminal verification

It means that select is 0, set is lisi, and the value is 123.com

* represents how many $ are between two * 

The number behind $ represents how many characters are below 

When creating lisi1, only lisi changed from 4 to 5 in the verification 

 Exit and restart the service and verify

 Do not read dump.rdb when appendonly.aof is enabled

when so created

then the blank line of this line

 After adding the content, you can delete it after deleting it

3. Redis master-slave replication

1. Concept

It refers to copying the data of one Redis server to other Redis servers. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, only from the master node to the slave node.

Two, the role

 Data redundancy: master-slave replication implements hot backup of data, which is a data redundancy method other than persistence.

Fault recovery: When there is a problem with the master node, the slave node can provide services to achieve rapid fault recovery; it is actually a kind of service redundancy.

Load balancing: On the basis of master-slave replication, combined with read-write separation, the master node can provide write services, and the slave nodes can provide read services (that is, the application connects to the master node when writing Redis data, and the application connects to the slave node when reading Redis data) , to share the server load; especially in the scenario of writing less and reading more, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.

 High availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters, so master-slave replication is the basis for high availability of Redis.

3. Disadvantages

Failure recovery cannot be automated;

Write operations cannot be load balanced;

Storage capacity is limited by a single machine.

4. Process

Step 1: If a Slave machine process is started, it will send a "sync command" command to the Master machine to request a synchronous connection.

Step 2: Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (execute rdb operation), and the Master will also record all commands for modifying the data and cache them in the data in the file. 

Step 3: After the background process completes the cache operation, the Master machine will send the data file to the Slave machine, the Slave machine will save the data file to the hard disk, and then load it into the memory, and then the Master machine will modify the data file All operations are sent to the Slave end machine together. If the Slave fails and causes a downtime, it will automatically reconnect after returning to normal.

Step 4: After the Master machine receives the connection from the Slave-side machine, it sends its complete data file to the Slave-side machine. If the Mater receives synchronization requests from multiple Slaves at the same time, the Master will start a process in the background to Save the data file, and then send it to all Slave-side machines, make sure all Slave-side machines are normal.

 5. Master-slave service construction

 Enter the configuration file, rectify according to the content 

After changing 6379, we are changing No. 6380

Restart after changing

The main configuration is complete

 who is this from 

View listening port

kill process

If the re-monitoring port is displayed as empty, the process is successfully killed

The Redis server will /opt/redis_6380.confbe started with the configuration file at path . This will start the Redis server and act according to the settings of this configuration file.

verify again

 The master-slave configuration is complete

6. Sentinel mode

1. Concept

It is a distributed system for monitoring each server in the master-slave structure. When a failure occurs, a new Master is selected through a voting mechanism and all Slaves are connected to the new Master. So the number of clusters running Sentinels must not be less than 3 nodes.

Relying on the master-slave mode

Two, the role

Monitoring: Sentry constantly checks that the master and slave nodes are functioning properly.

Automatic failover: When the master node fails to work normally, Sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to a new master node, and let other slave nodes replicate the new master node instead.

Notification (reminder): Sentinel can send the result of failover to the client.

3. Disadvantages

Write operations cannot be load balanced

Storage capacity is limited by a single machine

Sentinel cannot automatically fail over the slave node. In the scenario of read-write separation, the failure of the slave node will cause the read service to be unavailable, and additional monitoring and switching operations need to be performed on the slave node.

Four, build

Build the changed configuration

7. Verification

After stopping the master, the slave will elect a new master

The sentinel configuration file will automatically modify the address of the listening master node to the new master node address

Eight, build and verify the simulation operation experiment

1. View the configuration file

 Multiple 2 in front of the listening port

 Change here to host ip

"sentinel monitor mymaster 192.168.115.4 6379 2" is an example of a Redis Sentinel command.

This command means to tell Redis Sentinel to monitor the Redis master server named "mymaster", whose IP address is "192.168.115.4" and port number is "6379". The number "2" specifies that at least 2 Sentinel instances need to agree that the primary server is unavailable in order to perform failover operations.

Redis Sentinel is a system for monitoring and automatic failover of Redis master and slave servers. This command is used to add a monitoring master server configuration to Sentinel. Once the primary server becomes unavailable, Sentinel triggers a failover and elects a new primary server.

 2. View and copy, there are several slaves to copy

3. Enter the configuration file

 Change the content here to the corresponding port number

Add a line to the sentinel configuration file (put it in the background to start)

Enter the main configuration file and the sentinel configuration file to comment out the password (you can also not set the password, but there is a password in the production environment)

4. After modification, start

Enter netstat -anptu | grep redis to check whether the listening port is successfully started

 5. Kill all processes

Check

6. Start all processes

 7. Test the master-slave replication after startup to check whether it is normal (open the second terminal page)

 Verify that there is no problem with the master and slave

8. Start the sentinel and check the status

Nine, enter the third terminal to view the log

Start the Sentinels of 6380 and 6381 again

The port number has been successfully enabled

View the log cat sentinel.log again

 show result translation

10. Kill the process of 6379

 Kill 80381

 

11. Verify again whether it is successful

cat sentinel.log

The meaning of the result is

Sentry authentication succeeded

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Mapinyi666/article/details/132174940