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

How to use Master-Slave replication Master/slave in redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how redis uses master-slave copy Master/slave". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is it

Jargon: this is what we call master-slave replication. After the host data is updated, it is automatically synchronized to the standby master/slaver mechanism according to configuration and policy. Master is write-based and Slave is read-based.

What can I do?

Separation of reading and writing

Disaster recovery

Recommended (free): redis

How to play?

Matching slave (library) does not match master (library)

Slave library configuration command: slaveof master library IP master library port

Every time    is disconnected from master, it needs to reconnect unless you configure it into the redis.conf file (specific location: redis.conf search # REPLICATION #)

   info replication

Modify configuration file details

   copies multiple redis.conf files and renames them by pressing 'redi [port] .conf'

   enables daemonize yes

   pid file name

   designated port

   log file name

   dump.rdb name

Replication the United States [repl the United States [repl repl'ke the United States] n.

Copy of a painting, etc; repeat (an experiment), esp an answer to a reply

Three common tricks

One master and two servants (one host, two slaves)

Init

Set up the servant machine through slaveof 127.0.0.1 6379d

At this time, one master and two servants are set up.

Set K4 v4 in the Lord, get K4 in both slave.

1. If I set K1 and K2 before slave. So can we find out its value from the function?

Yes, only after receiving the slave machine, you can roll it from beginning to end. Some of the mainframe will do it all over.

two。 At this time on the host set K6 V6. And the two slaves also have set K6 V66. What happens at this point. First-come-first-served, the latter overrides, can the slave and the host execute the same command?

The host is mainly written and the slave is read by the machine.

At this time, the slave machine can not write, reported an exception.

3. The mainframe is dead? What are the circumstances from the opportunity. From the plane? Stand by?

It's slave again. Stand by. Will not rise to the top, the leader has come back, everything is business as usual.

4. Died from the plane? Can't recover from the function?

When you recover from a crash, it becomes master, because every time you disconnect from master, you need to reconnect, unless you configure it into redis.conf. If you want to keep up with the army, you can use the slaveof command. Do it all over again, and roll the mainframe from beginning to end.

The fire is handed down from generation to generation

Core idea: decentralization

The Master,Slave in which the previous Slave can be the next slave can also receive connection and synchronization requests from other slaves, so the slave acts as the next master in the chain, which can effectively reduce the writing pressure of master (slave or slave)

Midway change direction: erases the previous data and re-establishes and copies the latest

Slaveof new main library IP new main library port

For example, 79 is the host of 80 and 80 is the host of 81. In 79, set K9 v9 can be queried between 80 and 81, using info republication on 80 hosts, which is slave, but also connected to a slaver.

turn from a guest into a host

SLAVEOF no one

Make the current database stop synchronizing with other databases and turn it into the primary database

After the mainframe is down, the two slaves need to choose a boss. At this time, use a command for 80, slaveof no one, and let 80 be called leader. For 81, if you use slaveof 80, you will recognize 80-bit hosts and modify 80% of the data. 81 can also be queried. If the original host 79 comes back, it becomes an independent individual at this time.

Principle of replication

When slave starts successfully connecting to master, it sends a sync command.

Master receives orders to start the background save process and collects all commands received to modify the dataset. After the background process finishes execution, master will transfer the entire data file to slave to complete a full synchronization

Full copy: after receiving the database file data, the slave service saves it and loads it into memory.

Incremental copy: Master continues to send all new collected modification commands to slave in turn to complete synchronization

But as long as you reconnect to master, a full synchronization (full copy) will be performed automatically

The first is full replication, followed by incremental replication.

Sentinel mode (sentinel)

A group of sentinel can monitor multiple master at the same time (patrol sentry)

What is it

The automatic version, which is mainly anti-customer, can monitor the failure of the host in the background, and if it fails, it will automatically convert the slave database to the master database according to the number of votes.

How to play (use steps)

1. Adjust the structure, 6379 with 6380, 6381

two。 Create a new sentinel.conf file with the wrong name

3. Configure sentinels and fill in the contents.

    1.sentinel monitor monitored database name (self-named) 127.0.0.1 6379 1

two。 The last number above, 1, means that after the host is hung up, salve votes to see who will take over as the host, and how many votes will become the host (PS. There is a discrepancy between the description on the official website and the official documentation below)

4. Activate the sentinel.

    1. Redis-sentinel / sentinel.conf (the above directories are configured according to their actual conditions, and may be different)

5. Normal master-slave demonstration

6. The original master is dead.

7. Vote for a new election

8. Resume the master and slave and continue to work, check it out by info replication.

Question: if the previously hung master is rebooted, will there be a double master conflict?

A: no, the original master became slave.

The shortcomings of replication

Replication delay

Since all write operations are first performed on Master and then synchronously updated to slave, there is a certain delay from Master synchronization to Slave machines. When the system is very busy, the delay problem will be more serious, and the increase in the number of Slave machines will also aggravate this problem.

This is the end of the introduction to "how to use master-slave copy Master/slave for redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Database

Wechat

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

12
Report