Home > Database > Redis > How does Redis implement master-slave replication?

How does Redis implement master-slave replication?

PHPz
Release: 2023-05-29 14:21:35
forward
1731 people have browsed it

    1. What is Master&Slave?

    This is what we call master-slave replication. After the host data is updated, it is automatically synchronized to the master/slaver mechanism of the standby machine according to the configuration and policy. The Master is mainly for writing, and the Slave is mainly for reading.

    2. What can it do?

    1. Separation of reading and writing;

    2. Disaster recovery.

    3. How to play?

    1. Equipped with slave (library) but not master (library);

    2. Slave library configuration: slaveof [main library IP] [main library port];

    • Supplement: Every time the slave is disconnected from the master, it needs to be reconnected, unless you configure the redis.conf file;

    • Type info replication to view the redis master-slave information.

    3. Modify configuration file details

    - Copy multiple redis.conf files

    How does Redis implement master-slave replication?

    - Specify Port

    How does Redis implement master-slave replication?

    -Open daemonize yes

    How does Redis implement master-slave replication?

    ##-Pid file name

    How does Redis implement master-slave replication?

    -Log file name

    How does Redis implement master-slave replication?

    -Dump.rdb name

    How does Redis implement master-slave replication?

    4. 3 common tricks

    - One Master and Two Servants

    How does Redis implement master-slave replication?

    One Master, two Slaves, the Slave can only read but not write; when the Slave is disconnected from the Master, the slave of needs to be reconnected. Only then can the previous master-slave relationship be established; after the Master hangs up, the Master relationship still exists and can be restored by restarting the Master.

    - Passing on the Fire

    How does Redis implement master-slave replication?

    The previous Slave can be the Master of the next Slave, and the Slave can also receive connection and synchronization requests from other slaves, then the The slave serves as the Master of the next slave in the chain, which can effectively reduce the write pressure of the Master. If the slave server changes the master server for data synchronization midway, the old data will be cleared and synchronization with the latest master server will be re-established.

    - Turn against the customer

    When the Master hangs up, the Slave can type the command slaveof no one to stop the current redis from synchronizing data with other Master redis and turn it into Master redis.

    4. Copy Principle

    1. After Slave starts and successfully connects to the master, it will send a sync command;

    2. After the Master receives the command to start the save process, at the same time Collect all received commands for modifying the data set. After the background process is executed, the master will transfer the entire data file to the slave to complete a complete synchronization;

    3. Full replication: The slave service is in the database After file data is saved, it is saved and loaded into memory;

    4. Incremental copy: Master continues to pass all new collected modification commands to slave in turn to complete synchronization;

    5. But as long as the master is reconnected, a full synchronization (full replication) will be automatically performed.

    5. Sentinel mode (sentinel)

    An anti-customer-oriented automatic version that can monitor whether the Master database is faulty in the background. If it fails, the slave database will be automatically converted to the master database based on the number of votes. A group of sentinels can monitor multiple Masters at the same time.

    Usage steps:

    1. Create a new sentinel.conf file in the same directory as the Master's corresponding redis.conf. The name must not be wrong;

    2 , configure the sentinel, fill in the content in the sentinel.conf file:

    • sentinel monitor monitored database name (name it yourself) ip port 1

    • Explanation: The last number 1 above means that after the host hangs up, the slave will vote to see who will take over as the host. The slave will become the host after getting the number of votes.

    3. Start sentinel mode:

    • Command input: redis-sentinel /myredis/sentinel.conf

    • Note: The above sentinel.conf paths are configured according to their actual conditions

    6. Disadvantages of replication

    Delay, because all write operations are The operation is performed on the Master and then synchronized to the Slave, so there is a certain delay in synchronizing from the Master to the Slave machine. When the system is very busy, the delay problem will be more serious. The increase in the number of Slave machines will also make this problem more serious.

    Redis master, slave node deployment details

    1. Create two redis.conf files, one master and one slave

    redis_6379.conf
    redis_6380.conf
    Copy after login

    2. Master conf main configuration

    bind 127.0.0.1
    port 6379
    protected-mode yes
    daemonize yes
    pidfile /var/run/redis_6379.pid
    logfile "/Data/apps/redis-3.2.12/logs/redis_6379.log"
    dbfilename dump_6379.rdb
    dir /Data/apps/redis-3.2.12/workplace
    requirepass paopao
    Copy after login

    3. Main configuration of slave conf

    bind 127.0.0.1
    port 6380
    protected-mode yes
    daemonize yes
    pidfile /var/run/redis_6380.pid
    logfile "/Data/apps/redis-3.2.12/logs/redis_6380.log"
    dbfilename dump_6380.rdb
    dir /Data/apps/redis-3.2.12/workplace
    slaveof 127.0.0.1 6379
    masterauth paopao
    Copy after login

    4. Start the master-slave redis-server process

    redis-server /Data/apps/redis-3.2.12/conf/redis_6379.conf
    redis-server /Data/apps/redis-3.2.12/conf/redis_6380.conf
    Copy after login

    5. Other related configurations

    # 当从库同主机失去连接或者复制正在进行,从机库有两种运行方式:
    # 1) 如果slave-serve-stale-data设置为yes(默认设置),从库会继续相应客户端的请求
    # 2) 如果slave-serve-stale-data是指为no,除去INFO和SLAVOF命令之外的任何请求都会返回一个
    #    错误"SYNC with master in progress"
    slave-serve-stale-data yes
    slave-read-only yes
    # 从库会按照一个时间间隔向主库发送PINGs.可以通过repl-ping-slave-period设置这个时间间隔,默认是10秒
    repl-ping-slave-period 10
    # repl-timeout 设置主库批量数据传输时间或者ping回复时间间隔,默认值是60秒
    # 一定要确保repl-timeout大于repl-ping-slave-period
    repl-timeout 60
    # slave节点磁盘慢, 网速快时, 置为yes, master上的rdb文件将以socket方式传输, 跳过磁盘存储
    repl-diskless-sync no
    # 传输前等待秒数, 为了等待更多的slave节点加入
    repl-diskless-sync-delay 5
    # 传输时,是否禁用tcp nodelay
    repl-disable-tcp-nodelay no
    # 复制缓冲区大小
    repl-backlog-size 1mb
    # 复制缓冲区释放周期
    repl-backlog-ttl 3600
    # 为了sentinal模式, 选举master准备, 数值越小, 优先级越高
    slave-priority 100
    # require at least 3 slaves with a lag <= 10 seconds use
    min-slaves-to-write 3
    min-slaves-max-lag 10
    # 可覆盖bind, port 配置, 对外ip port
    slave-announce-ip 5.5.5.5
    slave-announce-port 1234
    Copy after login

    The above is the detailed content of How does Redis implement master-slave replication?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template