Table of Contents
slave状态变迁
master状态变迁
主备同步时序
主备同步状态维护
Home Database Mysql Tutorial Redis主备同步

Redis主备同步

Jun 07, 2016 pm 04:41 PM
redis change Synchronize state

slave状态变迁 #define REDIS_REPL_NONE 0 /* No active replication */#define REDIS_REPL_CONNECT 1 /* Must connect to master */#define REDIS_REPL_CONNECTING 2 /* Connecting to master */#define REDIS_REPL_RECEIVE_PONG 3 /* Wait for PING reply

slave状态变迁

<code>#define REDIS_REPL_NONE 0 /* No active replication */
#define REDIS_REPL_CONNECT 1 /* Must connect to master */
#define REDIS_REPL_CONNECTING 2 /* Connecting to master */
#define REDIS_REPL_RECEIVE_PONG 3 /* Wait for PING reply */
#define REDIS_REPL_TRANSFER 4 /* Receiving .rdb from master */
#define REDIS_REPL_CONNECTED 5 /* Connected to master */
</code>
Copy after login

server.repl_state储存slave当前的复制状态

* redis启动时初始化为REDIS_REPL_NONE

* 当接受到slaveof命令后,该redis转换为slave,同时状态变为REDIS_REPL_CONNECT

周期性执行的replicationCron会检查slave当前的状态,来执行不同的操作

  • 如果slave处于REDIS_REPL_CONNECT,调用connectWithMaster建立到master的连接,状态转换为REDIS_REPL_CONNECTING
  • 连接建立后,向master发送PING,状态转换为REDIS_REPL_RECEIVE_PONG
  • 接受到PONG后,如果master配置了需要认证,则slave发送AUTH消息,AUTH通过后;slave发送REPLCONF listening-port消息到master告知自身监听的端口;最后向master发送SYNC命令来请求同步rdb文件,同时状态转换为REDIS_REPL_TRANSFER。
  • 当master向slave发送rdb后,slave就开始读取rdb文件(master发送rdb前会先告知rdb文件的总长度),slave接受完rdb文件后,会emptyDb清空数据库,然后调用rdbLoad加载从master接受到的rdb文件,加载完成后状态变为REDIS_REPL_CONNECTED,接下来slave会继续接受从master同步过来的新操作,以保证主备的一致性。

master状态变迁

<code>#define REDIS_REPL_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
#define REDIS_REPL_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
#define REDIS_REPL_SEND_BULK 8 /* Sending RDB file to slave. */
#define REDIS_REPL_ONLINE 9 /* RDB file transmitted, sending just updates. */
</code>
Copy after login

server.slaves里维护所有的slave列表,slave.replstate里记录着每个slave当前的同步状态(从master的视角看)。

  • 当master接受到slave发来的sync/psync命令时,将该slave的状态转换为REDIS_REPL_WAIT_BGSAVE_START
  • master为该slave后台启动rdbSaveBackground,并将该slave的状态转换为REDIS_REPL_WAIT_BGSAVE_END (如果master己收到sync时,master正在为某个slave转储rdb文件,则新的slave可以直接进入REDIS_REPL_WAIT_BGSAVE_END状态)
  • 当rdbSaveBackground完成后,slave的状态转换为REDIS_REPL_SEND_BULK,master开始将rdb文件发送给slave,发送前会先发送rdb文件的长度信息。
  • 当rdb文件发送完成后,slave的状态转换为REDIS_REPL_ONLINE。

master在为slave启动rdbSaveBackground后,master上的更新会累积到slave的连接缓冲区,等到slave接受完rdb文件之后,将缓冲区里累积的更新同步到slave上

主备同步时序

  1. 管理员向server发送slaveof master-ip master-port,将该server变为master的slave
  2. slave建立到master的网络连接
  3. slave向master发送PING检测网络状态,master回复+PONG
  4. slave向master发送AUTH进行认证(可选),master恢复+OK
  5. slave向master发送REPLCONF listening-port,master恢复+OK
  6. slave向master发送sync/psync,master开始后台转储rdb文件
  7. master转储rdb完成后,向slave发送rdb文件,slave接受rdb文件并清空数据库,load从master接受到的rdb文件
  8. master将rdb转储及传输期间累积的更新操作同步到slave
  9. master与slave数据到达一致,接下来master接受到的所有更新操作都会同步到slave

主备同步状态维护

master周期性的ping slave(默认10s),当ping不通slave超过60s(默认)后,就会认为slave挂掉了,便会断开与该slave的连接;同理,slave如果超过一定时间没有接受到master的PING,会认为master挂掉了,便会断开与master的连接。

当master、slave连接断开后,slave需要重新向master请求同步rdb文件;2.8版本里redis一定程度支持增量同步,master将同步数据同步给slave时,会同时存储到一个环形缓冲区(默认1M大小),这个缓冲区永远存储最新的同步数据;另外master、slave都会记录当前同步的offset。

当slave与master断开后,slave会先尝试进行PSYNC增量同步,如果连接断开期间,master没有重启过,并且slave的offset在master的环形缓冲区内,则可直接进行增量同步,比如:

假设环形缓冲区的长度为1000, master的同步offset为10000,此时缓冲区里记录的是9001-10000的同步数据,slave同步到9500时,与master断开了连接;slave重新建立与master连接后,会带上offset 9500,请求增量同步,master发现该slave的offset在9001-10000之间,可以进行增量同步,将9501-10000的同步数据发送给slave,以达到主备一致的状态。

The post Redis主备同步 appeared first on Yun Notes.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
How to build the redis cluster mode How to build the redis cluster mode Apr 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

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to configure Lua script execution time in centos redis How to configure Lua script execution time in centos redis Apr 14, 2025 pm 02:12 PM

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

How to use the redis command line How to use the redis command line Apr 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 implement redis counter How to implement redis counter Apr 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 set the redis expiration policy How to set the redis expiration policy Apr 10, 2025 pm 10:03 PM

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

See all articles