• 框架——缓存框架——redis——功能——主从(replication)


    1、概念

      It allows replica Redis instances to be exact copies of master instances. The replica will automatically reconnect to the master every time the link breaks, and will attempt to be an exact copy of it regardless of what happens to the master.

    允许从服务器完全备份主服务器的数据,从服务器自动连接主服务器,并尝试同步主服务器的数据。需要注意的是同步是单向的,即主到从,不是双向的。

     

    主从的三种场景:

    当主从正常连接时,主服务器将修改数据集的命令发送给从服务器

    发送的是命令, 不是数据集。

    当主从连接异常时,从服务器尝试重连,并发起partial resynchronization(部分同步)

    此时发送的还是命令,不是数据集。

    无法进行部分同步时,发起full resynchronization(完全同步)

    主服务器会生成rdb文件,并发送给从服务器。此时发送的是数据集文件。不是命令。

    Redis uses asynchronous replication, with asynchronous replica-to-master acknowledges of the amount of data processed.

    同步数据的过程是异步的。

    A master can have multiple replicas

    一个主服务器可以有多个从服务器。

       Replicas are able to accept connections from other replicas. Aside from connecting a number of replicas to the same master, replicas can also be connected to other replicas in a cascading-like structure. Since Redis 4.0, all the sub-replicas will receive exactly the same replication stream from the master

    服务器既可以是从,也可以是主,这样可以构建一颗树。树的根只扮演主服务器的角色。

     

    Redis replication is non-blocking on the master side. This means that the master will continue to handle queries when one or more replicas perform the initial synchronization or a partial resynchronization

    同步过程不会阻塞主服务器执行其他命令。

     

    Replication is also largely non-blocking on the replica side

    同步过程也不会阻塞从服务器。

     

    Replication can be used both for scalability, to have multiple replicas for read-only queries (for example, slow O(N) operations can be offloaded to replicas), or simply for improving data safety and high availability

    从服务器可以只接收查询命令,与主服务器一起构建成集群。可以提升性能,可以保证数据的安全性。

     

      You can use replication to avoid the cost of having the master writing the full dataset to disk. this setup must be handled with care, since a restarting master will start with an empty dataset: if the replica tries to sync with it, the replica will be emptied as well.

    replication功能可以避免主服务器的持久化,但是这样做必须格外小心,重启之后主服务会丢失所有数据集,从服务器同步时,它的数据也会丢失。

    假设A作为主服务器且关闭了持久化功能,A1,A2作为从服务器。重启时

    主服务器A重启之后,其数据集是空的。从服务器A1,A2同步,从服务器A1, A2上的数据也会丢失

    2、数据同步

    部分同步:

    第一步,主服务器在每次启动之后生成一个replication ID,当每次给从服务器同步数据时,会生成一个自增offset。

    第二步,从服务器发送PSYNC指令或者部分同步时,携带replication ID,以及offset,主服务器进行比对,若replication ID相同, 则将自offset之后增量的命令发送给从服务器。

    引用原著:

      However if there is not enough backlog in the master buffers, or if the replica is referring to an history (replication ID) which is no longer known, then a full resynchronization happens

    当主服务器中的缓存没有足够的backlog,或者从服务器携带的replication ID不一致时,全量同步。

    从服务器的replication ID总是来自于主服务器,这样保证在主服务器重启时,会进行部分同步,而不是全量同步。

    全量同步:

    第一步,主服务器在后台生成rdb文件,并保存到磁盘中,生成过程的同时,缓存所有的写命令。

    第二步,将rdb文件发送给从服务器,并将缓存的命令也发送给从服务器。

    将repl-diskless-sync设置为yes时,不会保存到磁盘,直接发送文件流。写入磁盘这个过程非常耗性能。

    3、配置

    在从服务器的redis.conf中,

    // 设置主从关系
    replicaof <masterip> <masterport>
    // 主服务器有密码时,连接的密码
    masterauth <master-password>
    // 设置为只读的从服务器,默认值
    replica-read-only yes | no
    // 主服务器部署在Docker或者其他网络代理工具,会使主服务器的ip,端口改变时,可以在配置// 文件中声明主服务器原始的ip和端口
    replica-announce-ip 原始ip
    replica-announce-port 原始端口
    

      在主服务器的redis.conf中,

    // 不在磁盘上生成rdb
    repl-diskless-sync
    // 传输等待从服务器时间
    repl-diskless-sync-delay
    // 当从服务器的数量满足一定数量时,在同步过程中拒绝写入,配置这两个值的目的就是为了尽最大程度保存主从服务器的数据同步,当连接的从服务器过少时,同步过程次数增多。当网络延迟高时,同步过程会比较漫长。
    min-replicas-to-write
    // 当网络延迟小于多少秒时,
    min-replicas-max-lag
    

    4、特殊情况

    4.1   key过期

    有以下三种情况,引用原著:

      Replicas don't expire keys, instead they wait for masters to expire the keys. When a master expires a key (or evict it because of LRU), it synthesizes a DEL command which is transmitted to all the replicas.

      主服务器会将key过期视为一条del key指令同步给从服务器。

      

      However because of master-driven expire, sometimes replicas may still have in memory keys that are already logically expired, since the master was not able to provide the DEL command in time. To deal with that the replica uses its logical clock to report that a key does not exist only for read operations that don't violate the consistency of the data set (as new commands from the master will arrive)

    若在同步未完成的过程中,进行访问key值操作,从服务器需要根据自己的时间计算,若访问时间大于过期时间,返回错误。

     

      During Lua scripts executions no key expiries are performed. As a Lua script runs, conceptually the time in the master is frozen, so that a given key will either exist or not for all the time the script runs. This prevents keys expiring in the middle of a script, and is needed to send the same script to the replica in a way that is guaranteed to have the same effects in the data set.

    在执行脚本时,主服务器处于frozen状态,不会有key在执行期间过期。执行完之后,将脚本同步给从服务器。

  • 相关阅读:
    大数据应用之双色球算奖平台总体设计数据规模估算篇
    正能量之项目经理的自我修养
    从郭美美霸气侧漏看项目管理之项目经理防身术
    虚拟化技术纲要之虚拟化技术发展简史
    从国足1:5输泰国看项目管理之项目失败责任该有谁负
    分享一个有趣的代码,调用电脑中的api语音
    EXTJS组件的XTYPE属性列表
    FileSaver.js 浏览器导出Excel文件
    c#简单自定义异常处理日志辅助类
    批处理bat命令--获取当前盘符和当前目录和上级目录
  • 原文地址:https://www.cnblogs.com/rain144576/p/16792920.html
Copyright © 2020-2023  润新知