master-Slave configuration
To install and configure two Redis services first , you need to add the following configuration on master:
Port Port
LogFile logfile
which Port is the port of the master server,logfile is the log output address of master and the log file name.
Then modify the configuration file redis.conf of the Redis that you want to configure as the slave service and add the following configuration:
Port Port1
slaveof ip2 Port2
Masterauth pwd
which Port1 for slave server ports,ip2 and port2 are the IP of the master server Address and port,pwd is the connection password for master ( does not need to be configured if Master does not have a connection password set).
Instance:
1. Installation of two Redis services (available on different machines) such as:
Redis1:
Redis2:
2, the redis1 as the master Redis Service, the configuration file needs to modify the following parameters:
# Set the port of the primary service
Port 6379
# Set redis connection timeout (in milliseconds)
Timeout 30000
# Set redis Service log output address (full path + file name)
LogFile /data/cachedb1/redis1.log
# Turn off Snapshot on the main service (cancel setting of the save parameterin config file )
# Save 900 1
# Save 300 10
# Save 60 10000
# Turn off dump file output (remove the dump output file name and directory settings in the configuration file)
# dbfilename Dump1.rdb
# dir /data/cachedb1/
# Close AOF
AppendOnly No
# Cancel AOF output file name setting
#appendfilename appendonly1.aof
# Set AOF mode to No
Appendfsync No
the following parameters need to be modified in the configuration file as Redis2 from the Redis service:
# Set the port of the primary service
Port 6380
# Set redis connection timeout (in milliseconds)
Timeout 30000
# Set redis Service log output address (full path + file name)
LogFile /data/cachedb2/redis2.log
# Set the Snapshot time on the master service
Save 1
Save
Save 10000
# Set dump file output file name
# dbfilename Dump2.rdb
# Set dump file output file directory
# dir /data/cachedb2/
# Set the primary service IP, Port
slaveof 127.0.0.1 6379
# Open AOF
AppendOnly Yes
# Set AOF output file name
Appendfilename appendonly2.aof
# Set the AOF mode to one time per second
Appendfsync everysec
3. Start the main service:
Start from service:
4. Verify that the service is up and running:
To verify the primary service:
Verify from service:
Verify the main service output file:
Verify the output file from the service:
Appendix:Disaster tolerance Strategy
The basic Redis Disaster Recovery strategy is:
1, adopt Master-slave Way
2, in order to get good read and write performance,Master does not do any persistence
3,slave simultaneously open Snapshot and AOF to carry on the persistence, guarantees the data security
4.when master hangs up, modify the slave to master(or use the third-party master-Slave Auto-switch tool to complete)
5, copy the Rdb file on the slave to the original master to restore the original master data, modify the original master to Slave , start slave
6, the original Master recovery and then restore it to master, and restore the original slave to slave
7. If both master and slave are hung, the call command is resumed via aof and snapshot
Close the Snapshot method is to cancel the configuration of all "save xxx xxx" in redis.conf. where xxx is the specific value.
Close the AOF method is to add the following configuration to the redis.conf:
appendonly No
Redis 's master-slave auto-switching feature also requires third-party support, which can be implemented using the KeepAlive tool, but is relatively cumbersome and requires a shell script to be written .
Distributed Storage
Currently, Redis itself does not support cluster functionality,and Redis 's own cluster functionality is still in development, and is officially expected to be included in the 3.0 version. Therefore, in the distributed cluster, only the client-side tool can be used to realize the consistent hash distribution storage, that is, the key shard storage. Jedis is recommended here, andJedis is the official Redis-preferred Java Client Development package.
Project Address:Https://github.com/xetorthio/jedis
Architecture:
/-Redis (node 1)
Client 1---\/--Redis (Node 2)
Jedis---Redis (node 3)
Client 2---/\--Redis (node 4)
\-Redis (node 5)
Use Jedis Distributed storage needs to be appropriately packaged for Jedis based on business scenarios and data characteristics .
Scenarios and Analysis
at present, the main use There are two Redis locations: device on-line interface and log receive interface
Device on-line interface:
Storage type in Redis: Hash
Features: The data volume is limited and relatively stable, each data content is small, the read operation is greater than the write operation, the stored data may need to be kept in Redis for a long time , the data reliability requirement is high.
In view of the above features , Redis has high availability and fast read response capability.
The design cluster deployment scenario is as follows:
Description
1, by Jedis to complete the Redis distributed storage, and packaging for the client to provide a unified interface.
2. Build a cluster ina master-slave group. Master does not persist to meet high response requirements, only Snapshot and AOF are enabled on the slave side to ensure data reliability.
3, in each master-slave with keepalive to achieve automatic takeover, to ensure the high availability of Redis services.
Log Receive interface:
Storage type in Redis: Hash
Features: Data volume is not fixed, each data content is large, read and write average, stored data does not need to be kept in Redis for a long time , after the outage to allow the loss of some data.
in view of the above features , redis requirements are low, ensuring stability and scalability.
The design cluster deployment scenario is as follows:
Description
by Jedis completes the distributed storage of Redis and wraps it to provide a unified interface to the client. Each Redis service turns on Snapshot and AOF to ensure data reliability.
other
some of the following Redis monitoring tools are recommended:
Monitoring Services
-sentinel
Sentinel is a redis -brought tool that monitors Redis master- slave replication and implements automatic failover after the main hang-out. In the process of transfer, it can also be configured to execute a user-defined script, in the script we can implement alarm notification and other functions.
-redis Live
Redis Live is a more general Redis Monitoring scheme, which is the principle of periodically executing the monitor command on Redis to get the current Redis The currently executing command, and generates a visual analysis report of the Web page through statistical analysis.
-redis Faina
Redis Faina is a redis monitoring service developed by the famous image sharing app Instagram, which is similar in principle to Redis Live, and is MONITOR to do that.
Data distribution
Understand data storage distribution in Redis is difficult, for example, you want to know which type of key value takes up the most memory. Here are some tools that can help you analyze Redis datasets.
-redis-sampler
Redis-sampler is a tool developed by Redis authors that allows you to understand the approximate type, data, and distribution of data in the current Redis by means of sampling.
-redis-audit
Redis-audit is a script that allows us to know the amount of memory each type of key uses. It can provide data such as: the frequency of access to a certain type of key value, how many values set the expiration time, a certain type of key value to use the size of memory, it is convenient for us to find out which keys are not used or at all.
-redis-rdb-tools
Redis-rdb-tools is similar to the Redis-audit function, but it is obtained by analyzing the RDB file for statistical data.
Redis Master-Slave configuration