Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of persistence, Master-Slave synchronization and Sentinel in redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the persistence of redis, master-slave synchronization and Sentinel example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1.0 redis persistence

Redis is a kind of memory database, once the server process exits, the data of the database will be lost. In order to solve this problem, Redis provides two persistence solutions to save the data in memory to disk to avoid data loss.

1 | 1RDB persistence

Redis provides the RDB persistence function, in the specified time interval to generate a point-in-time snapshot of the dataset (point-in-time snapshot) this function can save the state of redis in memory to the hard disk, the RDB file generated by RDB persistence is a compressed binary file, this file is saved on the hard disk, redis can restore the state of the database through this file.

It can be executed manually.

It can also be configured in redis.conf and executed on a regular basis.

Advantages: high speed, suitable for backup, master-slave replication is based on RDB persistence function

Rdb triggers rdb by using the save command in redis

Rdb configuration parameters:

Port 6379 daemonize yes pidfile / data/6379/redis.pidloglevel notice logfile "/ data/6379/redis.log" dir / data/6379 protected-mode yes dbfilename dbmp.rdbsave 6379 1save 6379 10 save 60 10000

One operation is persisted every 900 seconds

Save 900s 1 operation to modify the class

Save 300 seconds 10 operations

Save 10000 operations in 60 seconds

two。 Trigger rdb persistence, or you can manually save command to generate dump.rdb persistence file

3. Restart redis so that data is no longer lost

4.rdb data files are binary files that are artificially incomprehensible.

| 1 | AOF of 2redis persistence |

AOF (append-only log file)

Record all change operation commands executed by the server (such as set del, etc.) and restore the dataset by re-executing these commands when the server starts

All the commands in the AOF file are saved in the redis protocol format, and the new command is appended to the end of the file.

Advantages: the maximum program ensures that data is not lost

Cons: logging is very large

Configuration mode

1. In the configuration file, add the aof parameter

Add parameters to redis-6379.conf to enable aof function

Appendonly yesappendfsync everysec

two。 Restart redis database and load aof function

3. Check whether the redis data directory / data/6379/ produces aof files

[root@web02 6379] # lsappendonly.aof dbmp.rdb redis.log

4. Log in to redis-cli, write data, and monitor aof file information in real time

Tail-f appendonly.aof

5. Set up the new key, check the aof information, and then close redis to check whether the data is persisted

Redis-cli-a redhat shutdownredis-server / etc/redis.confredis-cli-a redhat

Switch rdb data to aof data without restarting redis

1. Configure redis to support rdb persistence

two。 Start the redis client and temporarily switch to aof mode through the command

127.0.1 OK 6379 > CONFIG set appendonly yesOK127.0.0.1:6379 > CONFIG SET save ""

3. Check whether the data persistence mode at this time is rdb or aof, check the appendonly.aof file, data changes

Tail-f appendonly.aof

4. Aof is not permanently effective at this time. Write parameters to the configuration file.

Edit redis-6379.conf to add the following parameters

Appendonly yes

Appendfsync everysec

| 2 | 0 master-slave synchronization |

Implementation of master-slave synchronization in redis

1. Prepare three redis databases. Redis supports multiple instances.

Three configuration files, just different ports

Add the parameters of master-slave synchronization in the three configuration files

Redis-6379.conf

Port 6379 daemonize yes pidfile / data/6379/redis.pidloglevel notice logfile "/ data/6379/redis.log" dir / data/6379 protected-mode yes dbfilename dbmp.rdbsave 6379 1save 6379 10 save 60 10000

Redis-6380.conf

Port 6380 daemonize yes pidfile / data/6380/redis.pidloglevel notice logfile "/ data/6380/redis.log" dir / data/6380 protected-mode yes dbfilename dbmp.rdbsave 300 1save 6380 10 save 60 10000slaveof 127.0.0.1 6379

Redis-6381.conf

Port 6381 daemonize yes pidfile / data/6381/redis.pidloglevel notice logfile "/ data/6381/redis.log" dir / data/6381 protected-mode yes dbfilename dbmp.rdbsave 300 1save 6381 10 save 60 10000slaveof 127.0.0.1 6379

two。 Start three database instances to view the master-slave synchronization identity

Redis-cli-p 6379 info replication redis-cli-p 6380 info replication redis-cli-p 6381 info replication

3: make sure to view the information as follows and check whether it is synchronized

4. What if my main library fails?

Solution:

1. Manually switch between master and slave identities and elect a new master library

1. Kill 6379 of the main library.

two。 Turn off your slave identity on 6380

Slaveof no one

3. Give a new master identity on 6381

Salveof 127.0.0.1 6380

4. After the modification, you have to modify the configuration file to take effect permanently.

2. Automatically elect a new master with a sentinel

2 | 1redis Sentinel:

Sentinel function:

The Sentinel checks whether the master-slave architecture is normal. If the master library dies, the Sentinel will automatically modify the redis.conf and add / delete slaveof instructions.

Redis Sentinel installation configuration:

1. Prepare three redis instances, one master and two slaves

For details, please see redis master-slave configuration above.

2. Prepare three database instances and start three database instances

Redis-server redis-6379.confredis-server redis-6380.confredis-server redis-6381.conf

3, prepare three sentinels and start monitoring the master-slave architecture

Prepare three configuration files, Sentinel files

Redis-26379.conf

Port 26379 dir / var/redis/data/logfile "26379.log" sentinel monitor sbmaster 127.0.0.1 6379 2sentinel down-after-milliseconds qsmaster 30000sentinel parallel-syncs sbmaster 1sentinel failover-timeout sbmaster 180000daemonize yes

Redis-26380.conf

Port 26380 dir / var/redis/data/logfile "26380.log" sentinel monitor sbmaster 127.0.0.1 6379 2sentinel down-after-milliseconds qsmaster 30000sentinel parallel-syncs sbmaster 1sentinel failover-timeout sbmaster 180000daemonize yes

Redis-26381.conf

Port 26381 dir / var/redis/data/logfile "26381.log" sentinel monitor sbmaster 127.0.0.1 6379 2sentinel down-after-milliseconds sbmaster 30000sentinel parallel-syncs sbmaster 1sentinel failover-timeout sbmaster 180000daemonize yes

4. Start three Sentinel instances

Redis-sentinel redis-26380.conf redis-sentinel redis-26379.conf redis-sentinel redis-26381.conf

Check whether the Sentinel is in good condition.

It is normal only to find the following information, which is consistent with the following.

Redis-cli-p 26379 info sentinel

# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=sbmaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3

5, carry out the Sentinel automatic master-slave switch.

1. Kill 6379 of the redis database.

two。 Check the identity information of 6380 and 6381 to see whether the master-slave switch is performed automatically (you need to wait 30 seconds to switch)

3. Manually start the 6379 failed database to see if it will be added to the master-slave cluster of information by the sentry.

Attention! If you find it unsuccessful, delete all Sentinel profiles and start all over again.

Attention! If you find it unsuccessful, delete all Sentinel profiles and start all over again.

Attention! If you find it unsuccessful, delete all Sentinel profiles and start all over again.

Thank you for reading this article carefully. I hope the article "persistence of redis, Master-Slave synchronization and sample Analysis of Sentinel" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report