A single node instance

Single node instance is relatively simple, usually do a test, write a small program if you need to use cache, start one

Redis is still very easy, as a key/value database can also be competent

My official group click here

2. Master/Slaver mode

Redis master/slave mode configuration

Master-slave mode:

In the master-slave mode of Redis, the slave node asynchronously replicates data from the master node, master

Nodes provide read and write services, while slave nodes only provide read services (this is the default configuration and can be modified by modifying the configuration file)

Slave-read-only Control). A master node can have multiple slave nodes. To configure a slave node, only the

Conf file to slaveof master-ip master-port.

There are three ways to enable master/slave replication on a secondary node:

The configuration file

Add: slaveof<masterip><masterport> to the configuration file of the slave server

Start the command

Redis-server: slaveof<masterip><masterport>

Client command

After the Redis server is started, run slaveof<masterip><masterport> on the client

The instance becomes a slave node.

The above three methods are equivalent. The following uses the client command as an example to look at Redis after slaveof is executed

Changes to primary and secondary nodes.

In this example, a master node has two slave nodes

Configuration:

1, CD/usr/local/redis/redis – 4.0.2

Switch to the current Redis installation path

2, the mkdir config

Create a new folder to store the redis configuration files

3. In config, create three configuration files as follows:

cd config


vi master-6739.conf


The bind 0.0.0.0


port 6379


logfile “6379.log”


dbfilename “dump-6379.rdb”


daemonize yes


rdbcompression yes


Vi slave – 6380. Confbind 0.0.0.0


port 6380


logfile “6380.log”


dbfilename “dump-6380.rdb”


daemonize yes


rdbcompression yes


Slaveof 192.168.81.135 6379


vi slave-6381.conf


The bind 0.0.0.0


port 6381


logfile “6381.log”


dbfilename “dump-6381.rdb”


daemonize yes


rdbcompression yes


Slaveof 192.168.81.135 6379

Master-6739. Conf indicates the configuration file of the master node, slave-6380.conf indicates the configuration file of the slave node

Use: slaveof in the slave node configuration file to specify the master node

4. Start three REids services

[root @ localhost redis - 4.0.2]# ./src/redis-server config/master-6379.conf  [root @ localhost redis - 4.0.2]# ./src/redis-server config/slave-6380.conf  [root @ localhost redis - 4.0.2]# ./src/redis-server config/slave-6381.confCopy the code

Check out the Redis service

Test master/slave mode:

A, first connect three Redis services respectively, obtain the value of key as name, and specify the Redis service connected to that port through -p

[root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6379 
127.0.0.1:6379> get name 
(nil) 
[root@localhost redis-4.0.2]# ./src/redis-cli -p 6380 
127.0.0.1:6380> get name 
(nil) 
[root@localhost redis-4.0.2]# ./src/redis-cli -p 6381 127.0.0.1:6381 > get name (nil)The values obtained are nullCopy the code

B, set a key for the master node

[root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6379127.0.0.1:6379 >setName cmy OK 127.0.0.1:6379> get name"cmy"Copy the code

C. The slave node directly reads the value whose key is name

[root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6380127.0.0.1:6380 > get the name"cmy"[root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6381127.0.0.1:6381 > get the name"cmy"Copy the code

D. The slave node only provides read services and cannot perform write operations

127.0.0.1:6381 >set age 23
(error) READONLY You can't write against a read only slave.Copy the code

Pay attention to

When using master-slave mode, pay attention to the persistence operation of the Matser node, and the details of the situation when the matser node does not use persistence

If the server is down and the service is automatically restarted, data on the secondary server will be lost.

First, disable the matser service persistence

127.0.0.1:6379 > CONFIG SET save"" Copy the code

OK

Set a value on the master node

127.0.0.1:6379 >set age 23 Copy the code

OK

The slave node can get the age value

127.0.0.1:6380 > get the age"23" Copy the code

Disable the master node service

127.0.0.1:6379 > shutdown not connected >Copy the code

The slave node can still get the age value

127.0.0.1:6380 > get the age"23" Copy the code

When the master service is restarted, the age value cannot be obtained

[root @ localhost redis - 4.0.2]# ./src/redis-server config/master-6379.conf  [root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6379 127.0.0.1:6379 > get the age (nil)Copy the code

The value of age obtained by the slave node is null, causing data loss

[root @ localhost redis - 4.0.2]# ./src/redis-cli -p 6380 127.0.0.1:6380 > get the age (nil)Copy the code

Cause of data loss: After the master service is suspended, the slave node communicates with the master node after the service is restarted

A complete resynchronization operation, so since the master node is not persisted, the data on the slave node will also be

Lost. Therefore, when configuring the master/slave mode of Redis, you should turn on the persistence function of the master server.

At this point, the Master-slave mode of Redis is complete

Talk about what I see as the need for a master-slave model:

One of the purposes of the master-slave mode is to back up data so that when a node is damaged (i.e. unrecoverable hardware damage), the data is stored

Because there is backup, it can be easily restored.

Another function is load balancing. All clients accessing a node will definitely affect the working efficiency of Redis

Later, the query operation can be done by querying the slave node.

Essential understanding of the master-slave pattern (conclusions have been verified and can be verified by yourself) :

A Master can have multiple Slaves

By default, the master node can perform read and write operations, while the slave node can only perform read operations and write operations are prohibited

Do not modify the configuration to support write operations on the slave node. For one reason, data written to the slave node will not be synchronized to other nodes

Node; Cause two: After the master node modifies the same data, the slave node overwrites the data

If the slave node is down, the read and write of other slave nodes and master nodes are not affected. After the slave node is restarted, data is removed from the slave node

The master node is synchronized

If the master node is down, the read on the slave node is not affected. Redis no longer provides the write service and the master node is started

After Redis will provide external write services again.

If the master node fails, the slave node does not select another master

For example, if there is a password on the master node:

The client needs a password to access the master

The password is required to enable the slave service, which can be configured in the configuration

The client does not need a password to access the slave

Disadvantages of master and slave nodes

The disadvantages of master-slave mode can be seen from the above description:

When the master node is down, Redis cannot provide write services because the remaining slaves cannot become masters

This disadvantage impact is very big, especially for the production environment, is not a moment to stop the service, so the general health

The production environment is not just a master-slave model. Hence the following Sentinel model. 3. Sentinel model

The Redis Sentry mode, in popular parlour, is a “sentry robot”, and phase the “sentry robot”

With the right configuration, this “robot” can work 24 hours a day, it can automatically help you do some things, such as monitoring

Control, remind, automatic troubleshooting and so on.

Redis – sentinel profile

Redis-sentinel is the author of Redis, Antirez, because the Redis cluster is used by large companies, each company needs to write its own cluster management tool, so Antirez spent several weeks to write redis-Sentinel.

Redis Sentinel system is used to manage multiple Redis servers (instance), and Redis Sentinel provides high availability for Redis. Use the Sentinel pattern to create a Redis deployment that can handle various failures without human intervention.

The system performs the following three tasks:

Monitoring: Sentinel continuously checks whether your primary and secondary servers are allowed to run properly.

Notification: Sentinel can use the API to send notifications to managers or other applications when a monitored Redis server has a problem.

Automatic failover: (1) When a primary server does not work properly, Sentinel is enabled

Starting an automatic failover operation, which upgrades one of the secondary servers of the failed master to the new master,

And make the other secondary servers of the failed master to replicate the new master server; (2) The client fails to connect to the master server

The cluster also returns the address of the new master server to the customer service, so the cluster can use the new master server instead of failing

The server.

Distributed characteristics of Sentinel

Redis Sentinel is a distributed system,

You can run multiple Sentinel processes in a schema that use gossip Protocols to receive information about whether the main server is offline, Agreement Protocols are used to determine whether automatic failover is performed and which slave server is selected as the new master server.

A single Sentinel process is not reliable to monitor the Redis cluster, and when the Sentinel process goes down (sentinel itself has a single point of failure) the entire cluster system will not behave as expected. So it is necessary to put

Sentinel cluster, which has several benefits:

  • Some Sentinel processes are down, but the redis cluster can still be switched over.
  • If there is only one Sentinel process, redis will not be implemented if the process runs incorrectly or if the network is clogged

Active/standby cluster switchover (single point);

If there are multiple Sentinels, a Redis client can connect to any sentinel at will to get information about the redis cluster

A robust deployment requires at least three sentinel instances.

The three sentinel instances should be placed on computers or virtual machines that the customer has identified as failing in a separate way. Different physics, for example

Machines or VMS in different availability zones. This explanation is a machine to build, and multi-level background is a truth

Recently, I came into contact with the construction of Redis for project requirements. I simply recorded the pits encountered in the construction process

The overall configuration

192.168.1.100:6379 -> master 
192.168.1.101:6379 -> slave 
192.168.1.102:6379 -> slave 
192.168.1.100:26379 -> sentinel 
192.168.1.101:26379 -> sentinel 
192.168.1.102:26379 -> sentinel Copy the code

Set up steps

1. Install redis

# decompression
tar -xvf /usr/local/ redis - 3.2.11. Tar. Gz mkdir -p/usr /local/redis/bin 
cp  
/usr/local/redis/src/{redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redi 
s-server,redis-trib.rb} /usr/local/redis/bin 
mkdir -p /u01/redis/{6379/{log,data,pid,conf},26379/{log,data,pid,conf} 
Add environment variables
echo "export PATH=/usr/local/redis/bin:$PATH" >> /etc/profile 
source /etc/profile Copy the code

2. Configure redis – 6379

The redis node collocation base is as follows, and cp is divided into three virtual machines

/u01/redis/6379/conf/redis_6379.conf 
bind 0.0.0.0 
protected-mode no 
daemonize yes 
pidfile "/u01/redis/6379/pid/redis_6379.pid" 
port 6379 
tcp-backlog 511 
timeout 0tcp-keepalive 0 
loglevel notice 
logfile "/u01/redis/6379/log/redis_6379.log" 
databases 16 
stop-writes-on-bgsave-error yes 
rdbcompression yes 
rdbchecksum yes 
dbfilename "dump.rdb" 
dir "/u01/redis/6379/data"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 min-slaves-to-write 1 min-slaves-max-lag 10 appendonly no appendfilename"appendonly.aof"appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0  notify-keyspace-events"" 
hash-max-ziplist-entries 512 
hash-max-ziplist-value 64 
list-max-ziplist-entries 512 Copy the code

Start the service

Run this command on three VMS
redis-server /u01/redis/6379/conf/redis_6379.conf Copy the code

Establish a master-slave relationship

# in 192.168.1.101Redis -cli -p 6379 SLAVEOF 192.168.1.100 6379# in 192.168.1.102Redis -cli -p 6379 SLAVEOF 192.168.1.100 6379 View Replication 192.168.1.101:6379> Info Replication# Replication role:master connected_slaves:2 min_slaves_good_slaves:2 = 192.168.1.102 slave0: IP and port = 6379, state = online, offset = 9577826, lag = 1 = 192.168.1.103 slave1: IP and port = 6379, state = online, offset = 9577965, lag = 0 master_repl_offset: 9577965 repl_backlog_active: 1 Repl_backlog_size :1048576 repl_backlog_first_byte_offset:8529390 Repl_backlog_HISTlen :1048576 192.168.1.102:6379> Info replication# Replication Role :slave master_host:192.168.1.101 master_port:6379 master_link_status:up master_last_io_SECONds_ago :0 master_sync_in_progress:0 slave_repl_offset:9600220 slave_priority:100 slave_read_only:1 connected_slaves:0 min_slaves_good_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 Repl_backlog_first_byte_offset :0 repl_backlog_HISTlen :0 192.168.1.103:6379> Info replication# Replication Role :slave master_host:192.168.1.101 master_port:6379 master_link_status:up master_last_io_SECONds_ago :0 master_sync_in_progress:0 slave_repl_offset:9612675slave_priority:100 slave_read_only:1 connected_slaves:0 min_slaves_good_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0Copy the code

3. The sentinel – 6379 configuration

The sentinel node collocation base is as follows, and cp is divided into three virtual machines

/u01/redis/26379/conf/sentinel_26379.conf Copy the code

Sentinel Monitor myMaster monitors the master node in Redis, that is, 192.168.1.100

This file is the same on all three machines

port 26379 
bind 0.0.0.0 
daemonize yes 
protected-mode no 
dir "/u01/redis/26379/tmp" 
logfile "/u01/redis/26379/log/sentinel_26379.log"Sentinel Monitor MyMaster 192.168.1.100 6379 1Copy the code

Waiting to start after the observation/u01 / redis / 26379 / conf/sentinel_26379. Conf file changes

Run the info sentinel command to check the sentinel status

Redis -cli -h 192.168.1.100 -p 26379 info sentinel# Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 Sentinel_simulate_failure_flags: 0 master0: name = zhuanche01, status = ok, address = 192.168.1.100:6379, slaves = 2, sentinels = 3Copy the code

conclusion

When I was setting up, I encountered a problem with sentinel on 192.168.1.101 and 192.168.1.102.

It turns out there was no monitoring master

And then there’s the problem of looking at the log

The emergence of cluster is to solve the problem of limited capacity of single Redis and allocate Redis data according to certain rules

To multiple machines. Some understanding of cluster:

Cluster can be said to be the combination of Sentinel and master-slave mode. Through cluster, the master-slave and master re-selection can be realized

Yes, so if you configure two replicas and three shards, you need six Redis instances.

Because Redis data is allocated to different machines in the cluster according to certain rules, it can be added when the amount of data is too large

Machine capacity expansion

This mode is suitable for large data cache requirements, and sentinel can be used when the data volume is not large.


Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


Screenshots of some materials:





And limited-time premium benefits:

Tencent Senior PHP engineer written test topic

★ Deal with orders of 100 million level PV with high concurrency

★ Laravel develops tmall component services

Combat FLAG TV live video architecture project combat

Scan the qr code below to get it


Qm.qq.com/cgi-bin/qm/… (Qr code automatic recognition)


For those who are interested in PHP backend technology and PHP architecture technology, my official group click here to learn and discuss with each other.

The group has been managed to organize the knowledge system (source code, learning video and other information), welcome to receive free.

This course is deeply standardized to Tencent T3-T4 standard, and builds a learning plan for web developers to advance middle and senior level and architects to improve technology, and for their own value-added and salary increase! If you join BAT special training camp, you can also get the quota and GO language learning permission!!