search
HomeDatabaseRedisA brief analysis of the principle of cluster master-slave replication in Redis

This article will give you an in-depth understanding of the master-slave replication principle of Redis cluster. I hope it will be helpful to you!

A brief analysis of the principle of cluster master-slave replication in Redis

# 1. First of all, think about a question, why does redis need a distributed solution when its performance is so high?

1. To achieve higher performance: High-concurrency applications will have an impact on single-machine performance. More redis servers are needed to share the pressure and achieve load balancing

2. To achieve high availability: If Single machine to prevent downtime/hardware failure

3. Achieve scalability: Single machine memory and hardware are limited, realize horizontal expansion

redundant or sharded storage to achieve the above features.

2. Master-slave replication-replication configuration

Like Kafka, Mysql, and Rocketmq, redis supports cluster deployment. Cluster nodes are divided into master and slave. The master node It is the master, and the slave node is the slave (the latest is called a replica). The slave will synchronize the latest data from the master through the replication mechanism. Redis provides a very convenient command to enable master-slave replication. [Related recommendations: Redis Video Tutorial]

How to configure and enable master-slave replication?

Take building a pseudo cluster locally as an example. Port 6379 is the slave node and port 6378 is the master node.

1. Configure replicaof masterip masterport in redis.conf of the slave node. After starting the slave node, it will automatically connect to the master node and start synchronizing data.

A brief analysis of the principle of cluster master-slave replication in Redis

If a new master node is replaced, this configuration will be rewritten.

2, or specify

./redis-server --replicaof masterip masterport

3 when starting the redis-server program, or log in to the client and execute the following command

slaveof masterip masterport

Note that this method is modified during operation , can achieve failover

Note: A slave node can also be the master node of other nodes, forming a cascade replication relationship. But other nodes also synchronize data from the top-level master node.

After configuring the cluster, check the cluster status through info replication

A brief analysis of the principle of cluster master-slave replication in Redis

You can check the role information of the node in the cluster through the role command

A brief analysis of the principle of cluster master-slave replication in Redis

Note that slave nodes are read-only. An error will be reported when writing the command.

A brief analysis of the principle of cluster master-slave replication in Redis

How does slave exit the cluster? You can execute the following command:

slaveof no one

3. Master-slave replication process

1. First, the replica-replica joins the cluster

A brief analysis of the principle of cluster master-slave replication in Redis

2. Establish a connection with the master, and check whether the data needs to be synchronized from the master node through a timer.

A brief analysis of the principle of cluster master-slave replication in Redis

Source code description:

//每1s执行这个方法
void replicationCron(void) {
    ...
    //检查是否需要连接到master 如果是REPL_STATE_CONNECT状态,必须连接到master
    //#define REPL_STATE_CONNECT 1  Must connect to master 
    if (server.repl_state == REPL_STATE_CONNECT) {
        serverLog(LL_NOTICE,"Connecting to MASTER %s:%d",
            server.masterhost, server.masterport);
        //和master创建连接    
        if (connectWithMaster() == C_OK) {
            serverLog(LL_NOTICE,"MASTER <-> REPLICA sync started");
        }
    }
    
    //发送ping命令给slave 
    if ((replication_cron_loops % server.repl_ping_slave_period) == 0 &&
        listLength(server.slaves))
    {
        /* Note that we don&#39;t send the PING if the clients are paused during
         * a Redis Cluster manual failover: the PING we send will otherwise
         * alter the replication offsets of master and slave, and will no longer
         * match the one stored into &#39;mf_master_offset&#39; state. */
        int manual_failover_in_progress =
            server.cluster_enabled &&
            server.cluster->mf_end &&
            clientsArePaused();

        if (!manual_failover_in_progress) {
            ping_argv[0] = createStringObject("PING",4);
            replicationFeedSlaves(server.slaves, server.slaveseldb,
                ping_argv, 1);
            decrRefCount(ping_argv[0]);
        }
    }
    
    //发送换行符到所有slave,告诉slave等待接收rdb文件
    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        client *slave = ln->value;

        int is_presync =
            (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START ||
            (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&
             server.rdb_child_type != RDB_CHILD_TYPE_SOCKET));

        if (is_presync) {
            if (write(slave->fd, "\n", 1) == -1) {
                /* Don&#39;t worry about socket errors, it&#39;s just a ping. */
            }
        }
    }
    ...
}

3. Full copy process - supports diskless copy or rdb persistent copy

A brief analysis of the principle of cluster master-slave replication in Redis

When the slave is connected to the master, use the psync (previously the sync command, which does not allow partial resynchronization, so now use PSYNC instead) command to initialize replication, and the master node replication id and processing Send to the master after exceeding the maximum offset.

The master node has the following two attributes, a replication id (mark instance), and an offset (mark written to the stream of the slave node)

Replication ID, offset

If there is not enough backlog in the master node buffer work, or if the replica references a historical record (replication ID) that is no longer known, a full resynchronization will occur

Source code description:

    //没有在rdb进程,没有aof重写进程
    if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) {
        time_t idle, max_idle = 0;
        int slaves_waiting = 0;
        int mincapa = -1;
        listNode *ln;
        listIter li;

        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            client *slave = ln->value;
            //判断slave是否是等待bgsave状态
            if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {
            //多久没有发送心跳或查询数据了 空闲时间间隔
                idle = server.unixtime - slave->lastinteraction;
                if (idle > max_idle) max_idle = idle;
                slaves_waiting++;
                mincapa = (mincapa == -1) ? slave->slave_capa :
                                            (mincapa & slave->slave_capa);
            }
        }

        if (slaves_waiting &&
            (!server.repl_diskless_sync ||
             max_idle > server.repl_diskless_sync_delay))
        {
            /* Start the BGSAVE. The called function may start a
             * BGSAVE with socket target or disk target depending on the
             * configuration and slaves capabilities. */
             //bgsave rdb生成
            startBgsaveForReplication(mincapa);
        }
    }

During the replication process, slave state transition process.

A brief analysis of the principle of cluster master-slave replication in Redis

#4. In the command propagation stage, after full synchronization is performed, the master and slave will propagate commands to achieve data consistency.

A brief analysis of the principle of cluster master-slave replication in Redis

4. Understanding replication ids

Every time an instance is restarted from scratch as the primary instance, or the replica is promoted to primary instance, a new replication ID will be generated for this instance. If two replicas have the same replication ID, they may have the same data at different times. For a given history (replication ID) that holds the latest data set, the offset is understood as a logical time. It needs to be judged by the two data of Replication ID and offset. Used to determine where the slave node has synchronized data.

5. Master-slave replication FAQ

1. What will happen if the slave itself has data?

slave first deletes its own data and then loads it with the rdb file.

2. During the process of generating rdb files, how to deal with client writing commands?

Save to the memory cache, and send it to the slave after the rdb is sent.

3. How does Redis replication handle key expiration?

1. The copy will not expire the key, but will wait for the host to expire the key. When the master expires a key (or evicts it due to LRU), it synthesizes a DEL command that is transmitted to all replicas.

2. However, due to the expiration of the host driver, sometimes the replica may still have a logically expired memory key because the master server cannot provide the DEL command in time. To handle this, the replica uses its logical clock to report that a key does not exist, only for read operations that do not violate the consistency of the data set (because new commands from the master will arrive)

3. Key expiration is not performed during Lua script execution. When a Lua script is run, time is conceptually frozen in the master node, so a given key will exist or not exist for all the time the script is running. This prevents the key from expiring in the middle of a script and requiring the key to send the same script to the replica in a way that guarantees the same effect in the dataset.

Once a replica is promoted to primary, it will start expiring keys independently and does not require any help from the old primary.

6. Master-slave replication summary

1. The problem of data backup is solved, but the RDB file is large, and the recovery time is long when transferring large files

2. If the master is abnormal, you need to manually elect the replica as the master

3. In the case of 1 master and multiple slaves, 1 master and 1 slave, there is still a single point problem

4. Redis Version 2.8.18 and later supports diskless replication with higher performance.

7. Replication instructions

1. Asynchronous replication is used by default, and the number of synchronized commands is confirmed through asynchronous asynchronous execution

2. One master can have multiple Each copy

3. A copy can also have its own copy. Starting from redis4.0, the copy will receive exactly the same replication stream from the master node

4. Replication can be used for scalability property, and can also be used for multiple copies of read-only queries

For more programming-related knowledge, please visit:Introduction to Programming! !

The above is the detailed content of A brief analysis of the principle of cluster master-slave replication in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Redis: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Why Use Redis? Benefits and AdvantagesWhy Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AM

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Understanding NoSQL: Key Features of RedisUnderstanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AM

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

Redis: Identifying Its Primary FunctionRedis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AM

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis: A Guide to Popular Data StructuresRedis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AM

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

How to implement redis counterHow to implement redis counterApr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

How to use the redis command lineHow to use the redis command lineApr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

How to build the redis cluster modeHow to build the redis cluster modeApr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor