How does master-replica (master-slave) replication work in Redis?
Jul 13, 2025 am 12:10 AMRedis master-slave replication achieves data consistency through full synchronization and incremental synchronization. During the first connection, the slave node sends a PSYNC command, the master node generates an RDB file and sends it, and then sends the write command in the cache to complete the initialization; subsequently, incremental synchronization is performed by copying the backlog buffer to reduce resource consumption. Its common uses include read and write separation, failover preparation and data backup analysis. Notes include: ensuring network stability, reasonably configuring timeout parameters, enabling the min-slaves-to-write option according to needs, and combining Sentinel or Cluster to achieve high availability.
The Master-Replica (Master-Slave) mechanism of Redis is the basis for realizing data redundancy, read-write separation and high availability. Its core logic is actually not complicated: copy the data of the master node from the slave node and maintain consistency . Let’s take a look at how it works from several key perspectives.
Full synchronization during the first connection
When a slave node connects to the master node for the first time, full Resynchronization is triggered. This process is roughly divided into several steps:
- The slave node sends a
PSYNC
command to the master node and requests synchronization. - The master node executes
BGSAVE
to create an RDB snapshot file and caches subsequent write commands. - The master node sends the generated RDB file to the slave node.
- Load RDB data from the node and complete initialization.
- The master node then sends the write command in the cache to the slave node to ensure that the two are consistent.
This step usually takes a long time, especially when the data volume is large. Therefore, if you frequently disconnect and reconnect, you have to synchronize it in full every time, which will affect performance.
Incremental synchronization of subsequent updates
Once the initial synchronization is completed, the master and slave will enter a "normal working state". At this time, the master node will not send full data every time, but will update the slave node through Partial Resynchronization .
Specifically:
- The master node maintains a replication backlog buffer (repl_backlog_buffer) to record the most recently sent write operations.
- The slave node periodically sends its own offset to the master node.
- The master node determines whether part of the data can be retrieved from the backlog buffer and sent to it based on the offset.
- If possible, only the difference part is sent; otherwise, the full synchronization must be done again.
This method is efficient, but depends on the size of the backlog buffer. If your data is updated very frequently, it is recommended to increase the buffer appropriately.
Common uses of master-slave replication
Master-slave replication is not just for backup, it has many uses in practical applications:
- Read and write separation : The master node processes write requests, and multiple slave nodes share read requests, improving overall performance.
- Failover preparation : In conjunction with sentinel or cluster mechanism, quickly switch when the master node goes down.
- Data backup and analysis : Slave nodes can be used for offline data analysis without affecting the performance of the master node.
For example, if you build an e-commerce system, users place orders and go to the main nodes, and browse products and go to the slave nodes, it can effectively alleviate the pressure on the main node.
Notes and configuration suggestions
Although the Redis master-slave replication mechanism is practical, there are some details that are easy to overlook:
- Ensure the network stability. Frequent disconnection will lead to repeated full synchronization.
- Adjust
repl-timeout
andrepl-ping-slave-period
appropriately to avoid misjudgment of timeout. - If the data consistency requirements are high, you can enable
min-slaves-to-write
configuration to prevent the master from continuing to write when there is no slave node available.
In addition, master-slave replication does not provide automatic failover function itself. To achieve true high availability, it also needs to be combined with Redis Sentinel or Cluster mode.
Basically that's it. Master-slave replication seems simple, but there are still many details that need to be paid attention to in actual deployment, especially the selection of network environment and configuration parameters.
The above is the detailed content of How does master-replica (master-slave) replication work in Redis?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the rapid development of the Internet, Web applications are increasingly integrating database operations. MySQL is a world-renowned relational database system that is widely used. In highly concurrent web applications, MySQL master-slave replication is an important way to improve database performance and availability. This article will introduce how to use PHP to implement master-slave replication of MySQL database. 1. What is MySQL master-slave replication? MySQL master-slave replication refers to copying data from one MySQL database server to another server.

Building a Highly Available MySQL Cluster: Best Practice Guide for Master-Slave Replication and Load Balancing In recent years, with the rapid development of the Internet, the database has become one of the core data storage and processing engines for most web applications. In this scenario, high availability and load balancing have become important considerations in database architecture design. As one of the most popular open source relational databases, MySQL's cluster deployment solution has attracted much attention. This article will introduce how to implement a highly available database cluster through MySQL master-slave replication and load balancing.

MySQL database is a very popular relational database management system that supports a variety of data replication technologies, among which the more commonly used is master-slave replication technology. This article will introduce the data master-slave replication technology in MySQL, including principles, implementation methods, common problems and countermeasures. 1. Principle of master-slave replication technology The master-slave replication technology in MySQL can copy the data of a MySQL database to other servers to achieve data backup, load balancing, read-write separation and other functions. Its basic principle is to convert the main database

Redis is an open source memory-based key-value storage system that is commonly used in scenarios such as caching, queuing, and real-time data processing. In large-scale applications, in order to improve the availability and performance of Redis, it is often necessary to adopt a distributed architecture, in which master-slave replication is a commonly used mechanism. This article will introduce the master-slave replication function of Redis, including definition, principle, configuration and application scenarios. 1. Definition of Redis master-slave replication refers to automatically synchronizing the data of one Redis node (i.e. master node) to other nodes (i.e. slave node).

How to configure master-slave replication of MySQL database? Master-slave replication of MySQL database is a common data backup and high availability solution. By configuring master-slave replication, you can synchronize data from one MySQL server (master server) to another (slave server), thereby improving database availability and performance. The following describes how to configure master-slave replication in a MySQL database and provides corresponding code examples. Make sure the MySQL server is installed and started. First, make sure MySQL is installed on your system.

Master-slave replication and high-availability architecture in MySQL As Internet applications and data volumes continue to grow, the high availability and scalability of the database are becoming more and more important. As a widely used open source relational database, MySQL provides master-slave replication and high-availability architecture solutions. Master-slave replication refers to the process of using a MySQL database instance as the master database and replicating its data to one or more slave databases (slave). This replication method can achieve redundant backup of data and separation of reading and writing.

In mysql, master-slave replication means that data can be copied from a MySQL database server master node to one or more slave nodes. Asynchronous replication is used by default. The benefits of using master-slave replication: 1. Let the master database be responsible for writing and the slave database be responsible for reading. When the master database locks the table, the normal operation of the business can be ensured by reading from the slave database; 2. Hot backup of data can be done; 3. Expanding the architecture can reduce the frequency of disk I/O access and improve the I/O performance of a single machine.

Memcached is an open source, high-performance distributed memory object caching system that can be used to speed up web applications and performs particularly well in large-scale data caching. For this system, master-slave replication is a very important function, which can be used to ensure data reliability and high availability. This article will introduce how to use PHP to implement master-slave replication of Memcached database. Introduction to the master-slave mode The master-slave mode is a distributed structure of the Memcached server. It consists of at least two servers: one
