Redis master-slave configuration instance, precautions, and backup methods

Source: Internet
Author: User
Tags redis server

The following list clearly explains the features and advantages of redis replication.
1). the same master can synchronize multiple slaves.
2). Server Load balancer can also accept connections and synchronization requests from other Server Load balancer instances. This effectively distributes the synchronization pressure of the master node. Therefore, we can regard redis's replication architecture as a graph structure.
3) The master server provides services for slaves in a non-blocking manner. Therefore, during master-slave synchronization, the client can still submit query or modify requests.
4). The slave server also completes Data Synchronization in a non-blocking manner. During synchronization, if a client submits a query request, redis returns the data before synchronization.
5). In order to load the read operation pressure of the master, the slave server can provide the read-only operation service for the client, and the write service must still be completed by the master. Even so, the scalability of the system has been greatly improved.
6) The master can save the data to slaves, thus avoiding the need for an independent process in the master to complete this operation.
How replication works:

After slave is started and connected to the master, it will send a sync command. After that, the master will start the background storage process and collect all commands received for modifying the dataset. After the background process is executed, the master will transmit the entire database file to the slave, to complete a full synchronization. The slave server saves the database file data and loads it into the memory. After that, the master continues to send all the collected modification commands and the new modification commands to slaves in sequence. slave will execute these data modification commands this time to achieve the final data synchronization.
If the connection between the master and slave is disconnected, slave can automatically reconnect to the master, but after the connection is successful, a full synchronization will be automatically executed.

The following is my configuration. In master-slave mode, all persistence is disabled on the master and aof persistence is used on the slave:
$ CAT/opt/redis/etc/redis. conf

###### Master config ### general configuration daemonize yes # run the program in daemon mode. By default, pidfile/tmp/redis is run in non-daemon mode. pid # PID File Location port 6379 # use default port timeout 30 # client idle disconnection time loglevel warning # log record level, default is notice, I use warning here, to facilitate log monitoring. After using warning, logs are generated only when an alarm is triggered, which is very convenient for monitoring alarms by determining whether the log file is empty. Logfile/opt/logs/redis. log # location where logs are generated databases 16 # The default value is 0, that is, only one database is used. Here we set it to 16, so that multiple applications can use the same redis server. You can use the select n command to confirm the redis dB to be used, so that different applications will not have problems even if they use the same key. ### The following is a snapshot persistence policy. In order to ensure data security, the more frequent the changes in the following settings, the more frequent the snapshotting. That is to say, the more pressure, the more resources are spent on persistence. So I chose the master-slave mode and turned off the snapshotting on the master. # Save 900 1 # redis takes a snapshot to the disk if at least one modification occurs in redis within 900 seconds # Save 300 100 # within 300 seconds, redis takes a snapshot of at least 100 modifications to the disk. # Save 60 10000 # within 60 seconds, redis takes a snapshot to the disk for at least 10000 modifications. If yes, redis uses the compressed dbfilename dump. RDB # snapshoave file name DIR/opt/data/redis/# path of the snapshotting file ### replication settings, # slaveof # If this machine is a redis slave, you can enable this setting. If the master-slave mode is used, I will shut down the snapshotting on the master, so that I do not need to make persistence on the master, but on the slave, this greatly improves the master memory usage and system performance. # Slave-serve-stale-data yes # If slave cannot be synchronized with the master, can it be read? ### Security Settings # requirepass aaaaaaaaa # redis has good performance, it doesn't make much sense to use passwd # rename-command flushall "# You can use this method to turn off very dangerous commands, such as the flushall command, which clears the data of the entire redis server, no need to confirm and never fail ### limit set maxclients 0 # No client connection limit maxmemory 14 GB # Max memory available for redis, my server memory is 16 GB, if the persistent write mode of redis snapshow.copy-on-write is used, the memory will be used. In order to ensure the persistent operation, the system VM will not be used, which will degrade the performance of the redis server, we recommend that you keep up to half of the memory used by redis for persistence. I personally think it is a waste. I didn't do persistence on the master node. I used the master-slave maxmemory-policy volatile-LRU # used the LRU algorithm to delete the key with the expiration time set, however, if the program write time does not have the expiration time for key writing, we recommend that you use allkeys-LRU to ensure that redis will not be writable at least. ### Append only mode: appendonly no # aof is not used. aof is another persistence mode, I did not use this method because it does not guarantee data availability when the server or disk is damaged. Appendfsync everysec no-appendfsync-on-Rewrite noauto-Aof-rewrite-percentage 100auto-aof-rewrite-min-size 64 MB ### slow log set slowlog-log-slower-than 10000 # If the operation time exceeds 0.001, record the slow log, which is recorded in the memory, you can run the redis-cli slowlog GET command to view the maximum length of slowlog-max-len 1024 # Slow log ### virtual memory setting VM-enabled no # No virtual memory is used, in redis 2.4, we do not recommend using VMS. VM-Swap-file/tmp/redis. swapvm-max-memory 0vm-page-size 32vm-pages 134217728vm-max-threads 4 ### advanced config settings. The following settings are mainly used to save memory, I didn't modify the hash-max-zipmap-entries 512 hash-max-zipmap-value limit 512list-max-ziplist-value limit 128zset-max-ziplist-value 64 activerehashing Yes ### set using the following configuration, you can configure other settings, such as slave configuration # include/path/to/local. conf # include/path/to/Other. conf # include/opt/redis/etc/slave. if the conf file is an slave server, open this comment.

Slave Configuration:
$ CAT/opt/redis/etc/slave. conf

###### Slave config ### replication setting, slaveof redis01 6397 # If this machine is a redis slave instance, you can enable this setting. If the master-slave mode is used, I will shut down the snapshotting on the master, so that I do not need to make persistence on the master, but on the slave, this greatly improves the master memory usage and system performance. Slave-serve-stale-data no # If slave cannot be synchronized with the master, it is set to slave unreadable, so that the monitoring script can detect problems. ### Append only mode: Set appendonly yes # Use aof on slave to ensure data availability.

Other subsequent data backup work
1. Use the redis-cli bgsave command to persist data on the master redis once every morning and CP the data to other backup servers.
2. Use the redis-cli bgrewriteaof command to persist the data on slave redis every 30 minutes and CP the data to other backup servers.
3. Write a script and regularly get the keys on the master and slave to check whether the two are synchronized. If the two are not synchronized, issue an alarm in time.


This article is from the blog "Zhang xiaoyuer_linux" and will not be reproduced!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.