文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

为什么不建议生产用Redis主从模式?

2024-12-02 08:08

关注

Redis主从模式是最简单的一种集群模式,类似于MySQL等数据库的主从同步一样

Redis主从

原理

Redis实现主从复制(Master-Slave Replication)的原理:Slave从节点服务启动并连接到Master之后,它将主动发送一个SYNC命令,Master服务主节点收到同步命令后将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后台进程执行完毕后,Master将传送整个数据库文件到Slave,以完成一次完全同步。而Slave从节点服务在接收到数据库文件数据之后将其存盘并加载到内存中,此后Master主节点继续将所有已经收集到的修改命令,和新的修改命令一次传送给Slave,Slave将在本地执行这些数据修改命令,从而达到最终的数据同步。

如果Master和Slave之间的链接出现断链现象,Slave可以自动重连Master,但是在链接成功之后,一次完全同步将被自动执行

主从同步特点

主从优点

主从缺点

主从同步完整执行流程

1.当slave第一次启动连接master,或者是“被认为是第一次连接”(如主从之间断链后重连),则主从采用全量复制的方式进行数据同步

2.从库定时任务每秒检查是否有新的master需要连接,如果发现就与master建立socket连接

3.从库(slave)发送ping指令到master,master返回pong,则连接正常

4.从库(slave)发送auth认证信息给master,验证requirepass

5.认证通过后,从库(slave)发送sync命令给master请求数据同步

6.master接收到同步请求后向slave发送run_id和offset

7.slave会接收并保存master发过来的信息

8.master执行bgsave命令生成RDB文件,期间会创建复制缓冲区记录从现在开始执行的所有写命令

9.master向slave发送RDB数据,然后发送复制缓冲区记录的数据,slave会将RDB和缓冲区数据存放到磁盘中

10.slave清空原有数据,最后将磁盘中接收到的数据导入内存中

11.后续master收到的写命令都会通过之前建立的主从连接,增量发送给slave端

主从搭建实践

CentOS7默认源是安装Redis3.2版本的,先来看下3.x版本的Redis主从

# 安装redis
yum -y install redis
# 配置主redis
vim /etc/redis.conf
bind 172.22.29.87
protected-mode yes
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

# 配置从redis
bind 172.22.29.88
protected-mode yes
port 6379
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slaveof 172.22.29.87 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass 123456
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
# 启动redis
systemct start redis
# 登录redis,查看主从信息
redis-cli -h 172.22.29.87
172.22.29.87:6379> auth 123456
OK
# 主库信息
172.22.29.87:6379> info
……
# Stats
total_connections_received:2
total_commands_processed:15
instantaneous_ops_per_sec:1
total_net_input_bytes:478
total_net_output_bytes:184
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.01
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:757
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:1
slave0:ip=172.22.29.88,port=6379,state=online,offset=15,lag=0
master_repl_offset:15
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1

# 从库
# Stats
total_connections_received:1
total_commands_processed:33
instantaneous_ops_per_sec:0
total_net_input_bytes:609
total_net_output_bytes:11766
instantaneous_input_kbps:0.01
instantaneous_output_kbps:0.06
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:719
migrate_cached_sockets:0

# Replication
role:slave
master_host:172.22.29.87
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:449
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

接着在主库和从库进行set、get测试

# 从库执行set
172.22.29.88:6379> set name redis
(error) READONLY You can't write against a read only slave.'
# 因为从库是只读的,所以无法完成set

# 主库执行set、get
172.22.29.87:6379> set name redis
OK
172.22.29.87:6379> get name
"redis"

# 从库执行get
172.22.29.88:6379> get name
"redis"

主从搭建完成,测试主库挂掉

# 停掉主库
systemctl stop redis
# 主库查询
172.22.29.87:6379> get name
Could not connect to Redis at 172.22.29.87:6379: Connection refused
# 从库查询
172.22.29.88:6379> get name
"redis"
# 查看从库日志
1758:S 03 Feb 12:17:59.848 * Connecting to MASTER 172.22.29.87:6379
1758:S 03 Feb 12:17:59.848 * MASTER <-> SLAVE sync started
1758:S 03 Feb 12:17:59.849 # Error condition on socket for SYNC: Connection refused
# 接着启动主库
systemctl start redis
# 查看从库日志
1758:S 03 Feb 12:18:49.918 * MASTER <-> SLAVE sync started
1758:S 03 Feb 12:18:49.918 * Non blocking connect for SYNC fired the event.
1758:S 03 Feb 12:18:49.918 * Master replied to PING, replication can continue...
1758:S 03 Feb 12:18:49.919 * Trying a partial resynchronization (request 4cd802976b4c445f06389c15bb7720effab38107:773).
1758:S 03 Feb 12:18:49.920 * Full resync from master: 438afef01ffe3e55a681d89dfd699c1a6eb25e5b:1
1758:S 03 Feb 12:18:49.920 * Discarding previously cached master state.
1758:S 03 Feb 12:18:49.928 * MASTER <-> SLAVE sync: receiving 94 bytes from master
1758:S 03 Feb 12:18:49.928 * MASTER <-> SLAVE sync: Flushing old data
1758:S 03 Feb 12:18:49.928 * MASTER <-> SLAVE sync: Loading DB in memory
1758:S 03 Feb 12:18:49.928 * MASTER <-> SLAVE sync: Finished with success
1758:S 03 Feb 12:18:49.930 * Background append only file rewriting started by pid 11523
1758:S 03 Feb 12:18:49.952 * AOF rewrite child asks to stop sending diffs.
11523:C 03 Feb 12:18:49.952 * Parent agreed to stop sending diffs. Finalizing AOF...
11523:C 03 Feb 12:18:49.953 * Concatenating 0.00 MB of AOF diff received from parent.
11523:C 03 Feb 12:18:49.953 * SYNC append only file rewrite performed
11523:C 03 Feb 12:18:49.953 * AOF rewrite: 4 MB of memory used by copy-on-write
1758:S 03 Feb 12:18:50.019 * Background AOF rewrite terminated with success
1758:S 03 Feb 12:18:50.019 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
1758:S 03 Feb 12:18:50.019 * Background AOF rewrite finished successfully

# 查看主库日志
1869:M 03 Feb 12:18:49.526 * DB loaded from append only file: 0.000 seconds
1869:M 03 Feb 12:18:49.526 * The server is now ready to accept connections on port 6379
1869:M 03 Feb 12:18:49.919 * Slave 172.22.29.88:6379 asks for synchronization
1869:M 03 Feb 12:18:49.919 * Partial resynchronization not accepted: Runid mismatch (Client asked for runid '4cd802976b4c445f06389c15bb7720effab38107', my runid is '438afef01ffe3e55a681d89dfd699c1a6eb25e5b')
1869:M 03 Feb 12:18:49.919 * Starting BGSAVE for SYNC with target: disk
1869:M 03 Feb 12:18:49.920 * Background saving started by pid 1872
1872:C 03 Feb 12:18:49.922 * DB saved on disk
1872:C 03 Feb 12:18:49.922 * RDB: 2 MB of memory used by copy-on-write
1869:M 03 Feb 12:18:49.928 * Background saving terminated with success
1869:M 03 Feb 12:18:49.928 * Synchronization with slave 172.22.29.88:6379 succeeded

主从搭建及测试完成

CentOS7要安装Redis最新版本,需要安装remi软件源

# 安装remi源
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# 用remi源安装redis
yum --enablerepo=remi install redis -y
# 安装完后是redis6.2.6版本

Redis6.x版本的主从配置和3.x版本的区别,主要是将slaveof指令变为replicaof

bind 127.0.0.1 -::1 172.22.29.89
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /var/lib/redis
replicaof 172.22.29.90 6379
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

主从可优化的一些点

总结

Redis主从可以看到,搭建很简单,但是实际在生产环境中,很少使用,也不建议在生产环境中使用Redis主从模式来提供服务,从前面的缺点部分可以看出来,在数据量达到一定量级后,主从模式的不稳定性会极具增加,但是主从原理是其他集群模式的基础,所以原理要了解,后面接着介绍另外两种集群模式


来源:运维研习社内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯