Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis master-slave synchronization and read-write separation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. principle

The master-slave replication function of Redis is very powerful, a master can have multiple slave, and a slave can have multiple slave, thus forming a powerful multi-level server cluster architecture. Here are some features of redis master-slave replication:

1.master can have more than one slave.

two。 In addition to multiple slave connected to the same master, slave can also connect other slave to form a graph structure.

3. Master-slave replication does not block master. That is, when one or more slave synchronizes data with master for the first time, master can continue to process requests from client. In contrast, when slave first synchronizes data, it blocks requests that cannot be processed by client.

4. Master-slave replication can be used to improve the scalability of the system, we can use multiple slave specifically for client read requests, for example, sort operations can be handled using slave. It can also be used for simple data redundancy.

5. You can disable data persistence on master by commenting out all save configurations in the master configuration file and then configuring data persistence only on slave.

Second, the master-slave replication process

When the slave server is set up, slave establishes a connection to master and sends the sync command. Whether it is a connection established at the first synchronization or a reconnection after the connection is disconnected, master starts a background process to save the database snapshot to a file, while the master main process begins to collect new write commands and cache them. After the background process finishes writing the file, master sends the file to slave,slave to save the file to disk, and then loads it into the memory recovery database snapshot to slave. Master then forwards the cached command to slave. And all subsequent write commands received by master are sent to slave through the connection that was initially established. Commands for synchronizing data from master to slave use the same protocol format as commands sent from client. When the connection between master and slave is disconnected, slave can automatically re-establish the connection. If master receives synchronous connection commands from multiple slave at the same time, only one process is started to write database mirroring and then sent to all slave.

III. Configuration and application

1) redis installation

(1) download redis

# wget http://download.redis.io/releases/redis-3.0.7.tar.gz

(2) decompression and installation

# tar zxvf redis-3.0.7.tar.gz# cd redis-3.0.7# make PREFIX=/data/server/redis install

(3) configure redis

①: create the redis configuration file directory and copy the configuration file to this directory

# mkdir-p / data/server/redis/etc# cp redis.conf / data/server/redis/etc

②: set redis to run in the background

# sed-I 's/daemonize no/daemonize yes/g' / data/server/redis/etc/redis.conf

③: download the redis init startup file to the init.d directory and grant permissions.

# wget-c http://soft.vpser.net/lnmp/ext/init.d.redis-O / etc/init.d/redis# chmod 755 / etc/init.d/redis

④: create redis log directory and log storage files

# mkdir / data/server/redis/logs# touch / data/server/redis/logs/redis.log

⑤: create a pid directory and a data storage directory

# mkdir / data/server/redis/run# mkdir / data/server/redis/db

⑥: modify the file path in the vim / etc/init.d/redis startup file

2) redis master-slave synchronization configuration

(1) Master server:

# grep-v'^ # 'redis.conf | grep-v' ^ $'daemonize yes pidfile / data/server/redis/run/redis.pid# definition port port 123 "bind IPbind 10.10.10.2timeout 300 loglevel notice# specified log directory logfile / data/server/redis/logs/redis.logdatabases" configuration persistence (rdb mode) save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yes# definition data text Parts and directories dbfilename master.rdbdir / data/server/redis/dbslave-serve-stale-data yesslave-read-only yesslave-priority 100appendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zsetMurveziplist- Value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb60client-output-buffer-limit pubsub 32mb 8mb 60

(2) slave server

# grep-v'^ # 'redis.conf | grep-v' ^ $'daemonize yes pidfile / data/server/redis/run/redis.pidport 1234 bind 10.10.10.3timeout 300 loglevel noticelogfile / data/server/redis/logs/redis.log databases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename slave.rdbdir / data/server/redis/db slaveof 10.10.10.2 1234 # specify the address slave-serve-stale-data yesslave-read-only yesslave- of master Priority 100appendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 00 0client-output-buffer-limit slave 256mb 64mb60client-output-buffer-limit pubsub 32mb 8mb 60

(3) start the redis service

Master:

# / data/server/redis/bin/redis-server / data/server/redis/etc/redis.conf# tail-fn100 / data/server/redis/logs/redis.log / / check whether there is an error in the log

Slave:

# / data/server/redis/bin/redis-server / data/server/redis/etc/redis.conf# tail-fn100 / data/server/redis/logs/redis.log / / check whether there is an error in the log

(4) check redis master-slave synchronization

Master:

Redis-m > set data zhangsanredis-m > get data

Slave:

Redis-s > get data

Note: because slave-read-only yes is configured in the configuration file, the slave node is read-only and cannot operate and change the database.

Attachment: http://down.51cto.com/data/2368174

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report