redis-master-slave

How Redis replication works:

1. If a Slave is set up, it will issue a SYNC command whether it is the first connection or reconnection to the Master;
2. When the Master receives the SYNC command, it will do two things:
a) The Master executes BGSAVE, namely Save data to disk in the background (rdb snapshot file);
b) The Master also stores the newly received commands to write and modify the dataset into the buffer (non-query class);
3. When the Master saves the data to the snapshot in the background After the file is completed, the Master will transfer the snapshot file to the Slave, and the Slave will load the file into the memory after emptying the memory;
4. The Master will also send the commands previously collected into the buffer through the Reids command protocol. Forwarded to the Slave, the Slave executes these commands to achieve synchronization with the Master;
5. The Master/Slave will continue to synchronize the commands asynchronously after that to achieve the synchronization of the final data;
6. It should be noted that once the Master and Slave Any reconnection will trigger a full synchronization operation. But in versions after 2.8, it may also be a partial synchronization operation.
 
Target:

Build a redis cluster (one master and three slaves)

 

master 下挂 slaveA,slaveB

Among them: slaveB hangs slaveC

 

Prepare
sudo wget http://download.redis.io/releases/redis-3.2.6.tar.gz
sudo tar -xzvf redis-2.8.13.tar.gz
cd redis-2.8.13/src

 

 

Generate executable:
sudo make

 

 

Copy the instance and configure it to a directory (this is usually the startup directory)
sudo cp /home/q/www/redis/redis-2.8.13/src/redis-server.
sudo cp /home/q/www/redis/redis-2.8.13/src/redis-cli .
sudo cp /home/q/www/redis/redis-2.8.13/src/redis-benchmark .
sudo cp /home/q/www/redis/redis-2.8.13/redis.conf .

 

 

复制(多份配置,用于启动不同的端口号和主从关系等):

sudo cp redis.conf redis_A.conf 

sudo cp redis.conf redis_B.conf 

sudo cp redis.conf redis_C.conf 

 

修改各自实例的端口号:

A:port 6380

B: port 6381

C: port 6382

 

修改主从配置:

A:slaveof 127.0.0.1 6379

B: slaveof 127.0.0.1 6379

C: slaveof 127.0.0.1 6380

 

启动实例:
nohup sudo ./redis-server redis.conf &
nohup sudo ./redis-server redis_A.conf &
nohup sudo ./redis-server redis_B.conf &
nohup sudo ./redis-server redis_C.conf &

 

 

测试:
sudo ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set name xinchun.wang
OK
[xinchun.wang@l-*.f.dev.cn6 ~/redis]$ sudo ./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get name
"xinchun.wang"
127.0.0.1:6380> 
[xinchun.wang@l-*.f.dev.cn6 ~/redis]$ sudo ./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> get name
"xinchun.wang"
127.0.0.1:6381> 
[xinchun.wang@l-*.f.dev.cn6 ~/redis]$ sudo ./redis-cli -h 127.0.0.1 -p 6382
127.0.0.1:6382> get name
"xinchun.wang"

 

 

主从配置完毕!

 

继续了解:

127.0.0.1:6379> info

# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=976,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=976,lag=0
 

 注意:

role:master

connected_slaves:2

 

 

 

 

127.0.0.1:6380> info

# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1046
slave_priority:100
slave_read_only:1
connected_slaves:0

 

 

 

127.0.0.1:6381> info

 

 

# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:2768
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6382,state=online,offset=2754,lag=0

 

关闭实例:
 sudo ./redis-cli -n  6379 shutdown

  

 

其他:

改为 yes # 是否后台运行 (还可以通过文中的nohup & 的方式,不建议使用)

daemonize yes  

 

#数据文件的位置

dbfilename dump.rdb

 

#当前实例启动的端口号

port 6379

 

#配置从

slaveof 127.0.0.1 6379

 

#数据目录:

dir .  # 数据目录

maxmemory项,设最大占用内存为50MB:maxmemory 50mb
有6种内存过期策略,通过maxmemory-policy修改,一般使用默认值或allkeys-lru:
volatile-lru:只对设置了过期时间的key进行LRU(默认值)
allkeys-lru : 是从所有key里 删除 不经常使用的key
volatile-random:随机删除即将过期key
allkeys-random:随机删除
volatile-ttl : 删除即将过期的

noeviction : 永不过期,返回错误 

 

 

以下是关于 Redis 复制功能的几个重要方面:
1. 一个Master可以有多个Slave;
2. Redis使用异步复制。从2.8开始,Slave会周期性(每秒一次)发起一个Ack确认复制流(replication stream)被处理进度;
3. 不仅主服务器可以有从服务器, 从服务器也可以有自己的从服务器, 多个从服务器之间可以构成一个图状结构;
4. 复制在Master端是非阻塞模式的,这意味着即便是多个Slave执行首次同步时,Master依然可以提供查询服务;
5. 复制在Slave端也是非阻塞模式的:如果你在redis.conf做了设置,Slave在执行首次同步的时候仍可以使用旧数据集提供查询;你也可以配置为当Master与Slave失去联系时,让Slave返回客户端一个错误提示;
6. 当Slave要删掉旧的数据集,并重新加载新版数据时,Slave会阻塞连接请求(一般发生在与Master断开重连后的恢复阶段);
7. 复制功能可以单纯地用于数据冗余(data redundancy),也可以通过让多个从服务器处理只读命令请求来提升扩展性(scalability): 比如说, 繁重的 SORT 命令可以交给附属节点去运行。
8. 可以通过修改Master端的redis.config来避免在Master端执行持久化操作(Save),由Slave端来执行持久化。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326719649&siteId=291194637