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

Redis builds the actual operation of master-slave synchronization and read-write separation

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

Share

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

This article mainly explains the actual operation of "Redis master-slave synchronization and read-write separation". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the actual operation of Redis master-slave synchronization and read-write separation".

Master-slave synchronization of Redis distributed infrastructure

Earlier we introduced the basis of master-slave synchronization, today we start to build a Redis master-slave server, and then to read-write separation.

First of all, let's prepare the materials, the Redis5.0Windows version and the management tools of Redis. If you can't find them online, you can follow the official account below and reply: redis5.0.

Note: our operation and presentation are carried out under the Windows operating system, students who want to play under the Linux operating system do not panic, although slightly different, but the idea and configuration are the same.

Configure server IP and port

We copy the Redis to these three folders, without explaining the name, we can know that I am going to do an one-master-multi-slave structure.

We go to this folder to find the file [redis.windows.conf]. It is suggested to open it through notepad. Here we modify the ip and port number of the master server.

# modify ipbind 192.168.1.2 "modify port port 6000

After saving, we run this [redis-server.exe]. After starting the service, we find that the port number has not changed. Here, I will teach you a trick. Select "redis.windows.conf" directly and drag it to [redis-server.exe]. Then the running redis service port becomes 6000.

Follow this step to configure the port number of slave server A to 7001 and the port of server B to 7002

Then log in to the two servers on the RedisManage tool, and now that we have done the basic preparatory work, let's officially enter.

Configure master-slave synchronization

Synchronize server information from server A, this configuration item is: slaveof [primary IP] [primary port]

After slaveof 192.168.1.219 6000 is configured, we restart slave server A, then add a key > set name mango to the master server and check whether the master and slave have completed synchronization.

Add child nod

Then we also configure the slave server B to start slaveof. Here, in the principle section, add a child node, and the master server will start a child thread to save a snapshot and share it to the child node synchronously through socket.

Separation of reading and writing

Let's first think of a question, first add a name to the master server, and then quickly synchronize to other slave servers, so do I have to synchronize to other servers if I write a new key from the slave server? This will lead to several problems. If you hang up before writing a key from the server to another server, and after restarting, we find that the key on the slave server is different from that on the master server; for example, every server can write, then our server is very chaotic, either in the process of synchronization or on the way of synchronization, which increases the pressure on the server and the performance consumption of IO read and write.

In order to reduce this complexity, we only allow one server to do one thing at design time, that is, the master server is responsible for writing data, and the slave server is responsible for providing read data, so the read-write separation comes out.

# configure read-only slave-read-only yes from the server

After the configuration, let's try to write a key.

> set name li "READONLY You can't write against a read only replica."

Configure the log file

How can we troubleshoot if the Redis is abnormal? By the way, here we need to configure the log file, and none of the abnormal information is left behind.

# Log level loglevel notice# log file logfile "redis.log" configuration incremental synchronization

If our slave server is disconnected and restarted, the slave server will compare the data with the master server. If key is not written, then synchronization will not be performed, instead, the entire cache snapshot will be synchronized. If the amount of data is very large, then synchronizing the snapshot is hell, so Redis provides a replication buffer.

This structure is a ring structure that holds the latest copied commands. In this way, when the slave server is offline, there is no need to completely copy the data of the master server. if partial synchronization can be performed, only part of the data in the buffer needs to be copied to the slave server to restore the normal replication state. The larger the buffer size, the longer it takes to go offline from the server, and the replication buffer allocates memory only when there is a slave connection. Memory will be released without a period of time from the server. The default is 1m.

Repl-backlog-size 1mb

Configure local compression

As the running time gets longer and longer, the database on Redis's local disk becomes larger and larger, so we can compress the database. Redis uses LZF compression, but takes up a little bit of CPU time.

Rdbcompression yes

At this point, the master-slave synchronization and read-write separation system is almost completed, and there are still some detailed configurations that need to be tried by the friends themselves. The whole configuration process is not very long, but the principle needs to be clarified. The main purpose of the master and slave is to prevent the stand-alone Redis from hanging up, and the data request goes directly to the database, resulting in the server being unavailable.

Thank you for your reading. the above is the content of "Redis master slave synchronization and read-write separation of actual operation". After the study of this article, I believe you have a deeper understanding of the problem of Redis master slave synchronization and read-write separation of actual operation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report