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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 May 08, 2024 pm 03:50 PM

1. Start the [Start] menu, enter [cmd], right-click [Command Prompt], and select Run as [Administrator]. 2. Enter the following commands in sequence (copy and paste carefully): SCconfigwuauservstart=auto, press Enter SCconfigbitsstart=auto, press Enter SCconfigcryptsvcstart=auto, press Enter SCconfigtrustedinstallerstart=auto, press Enter SCconfigwuauservtype=share, press Enter netstopwuauserv , press enter netstopcryptS

Golang API caching strategy and optimization Golang API caching strategy and optimization May 07, 2024 pm 02:12 PM

The caching strategy in GolangAPI can improve performance and reduce server load. Commonly used strategies are: LRU, LFU, FIFO and TTL. Optimization techniques include selecting appropriate cache storage, hierarchical caching, invalidation management, and monitoring and tuning. In the practical case, the LRU cache is used to optimize the API for obtaining user information from the database. The data can be quickly retrieved from the cache. Otherwise, the cache can be updated after obtaining it from the database.

Caching mechanism and application practice in PHP development Caching mechanism and application practice in PHP development May 09, 2024 pm 01:30 PM

In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.

How to upgrade Win11 English 21996 to Simplified Chinese 22000_How to upgrade Win11 English 21996 to Simplified Chinese 22000 How to upgrade Win11 English 21996 to Simplified Chinese 22000_How to upgrade Win11 English 21996 to Simplified Chinese 22000 May 08, 2024 pm 05:10 PM

First you need to set the system language to Simplified Chinese display and restart. Of course, if you have changed the display language to Simplified Chinese before, you can just skip this step. Next, start operating the registry, regedit.exe, directly navigate to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlNlsLanguage in the left navigation bar or the upper address bar, and then modify the InstallLanguage key value and Default key value to 0804 (if you want to change it to English en-us, you need First set the system display language to en-us, restart the system and then change everything to 0409) You must restart the system at this point.

How to use Redis cache in PHP array pagination? How to use Redis cache in PHP array pagination? May 01, 2024 am 10:48 AM

Using Redis cache can greatly optimize the performance of PHP array paging. This can be achieved through the following steps: Install the Redis client. Connect to the Redis server. Create cache data and store each page of data into a Redis hash with the key "page:{page_number}". Get data from cache and avoid expensive operations on large arrays.

How to find the update file downloaded by Win11_Share the location of the update file downloaded by Win11 How to find the update file downloaded by Win11_Share the location of the update file downloaded by Win11 May 08, 2024 am 10:34 AM

1. First, double-click the [This PC] icon on the desktop to open it. 2. Then double-click the left mouse button to enter [C drive]. System files will generally be automatically stored in C drive. 3. Then find the [windows] folder in the C drive and double-click to enter. 4. After entering the [windows] folder, find the [SoftwareDistribution] folder. 5. After entering, find the [download] folder, which contains all win11 download and update files. 6. If we want to delete these files, just delete them directly in this folder.

PHP Redis caching applications and best practices PHP Redis caching applications and best practices May 04, 2024 am 08:33 AM

Redis is a high-performance key-value cache. The PHPRedis extension provides an API to interact with the Redis server. Use the following steps to connect to Redis, store and retrieve data: Connect: Use the Redis classes to connect to the server. Storage: Use the set method to set key-value pairs. Retrieval: Use the get method to obtain the value of the key.

The key concept of C++ multi-threaded programming is how to synchronize threads? The key concept of C++ multi-threaded programming is how to synchronize threads? Jun 03, 2024 am 11:55 AM

Key concepts of C++ multi-thread synchronization: Mutex lock: ensure that the critical section can only be accessed by one thread. Condition variables: Threads can be awakened when specific conditions are met. Atomic operation: A single uninterruptible CPU instruction ensures the atomicity of shared variable modifications.

See all articles