Current location - Quotes Website - Collection of slogans - High-performance and high-concurrency website architecture, teaching you to build Redis5 cache cluster.
High-performance and high-concurrency website architecture, teaching you to build Redis5 cache cluster.
I. Introduction of Redis cluster

Redis is really an excellent technology. It is a NoSQL memory database with key values, which is written by ANSI C, abides by BSD protocol, supports the network, can be based on memory, can be persisted, and provides a log-based and key-value-based database with API in multiple languages. The biggest feature of Redis is that it will put all the data in memory, so the reading and writing speed is very good. Redis is based on memory and has high performance, which can solve the instantaneous concurrency of websites to some extent, such as snapping up goods and killing seconds.

While the website is under the pressure of high concurrent access, it also needs to query qualified data from massive data, which requires fast response. The front end sends requests, and the back end interacts with mysql database for sql query operation, which is slow to read and write. At this time, Redis is introduced, and the data in mysql is cached in Redis, and the performance will be improved when reading next time. Of course, it also supports the persistence of the data in the memory to the hard disk in the form of snapshots and logs, so that even in abnormal situations such as power failure and machine failure, the data will not be lost, and Redis can restore the snapshot data from the hard disk to the memory.

Redis released a stable version of version 5.0, giving up Ruby's cluster mode and using redis-cli written in C language, which greatly reduced the complexity of cluster construction. Redis-Cluster adopts a non-central structure, each node stores data and the whole cluster state, and each node is connected with all other nodes.

In order to ensure the high availability of data, the master-slave mode is added. A master node corresponds to one or more slave nodes. The master node provides data access and the slave nodes pull data backup from the master node. When the master node hangs, a slave node will be selected as the master node, thus ensuring that the cluster will not hang.

Redis-cluster voting: fault tolerance. The voting process involves all the hosts in the cluster. If more than half of the master nodes communicate with the master node for more than (cluster-node-timeout), it is considered that the current master node is suspended.

There should be at least an odd number of nodes in the cluster, so there are at least three nodes, and each node has at least one backup node, so the following six nodes are used (the primary node and the backup node are determined by redis-cluster). Six nodes are distributed on one machine, adopting the mode of three masters and three slaves. In practical application, it is best to use multiple machines, such as distributing six nodes to three machines. When building a cluster, redis automatically assigns master nodes and slave nodes to different machines.

Second, the independent redis model.

Download the source code redis5.0 and decompress and compile it.

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

TalxZF redis-5.0.0.tar.gz

cd redis-5.0.0

manufacture

Redis front-end startup needs to be changed to background startup.

Modify the redis.conf file and change the daemon number->; yes

vim redis.conf

Start redis

/www/server/redis/src/redis-server/www/server/redis/redis . conf

See if redis is running.

ps aux|grep redis

Now the stand-alone redis mode is completed.

Thirdly, redis cluster mode:

1. Create 6 Redis configuration files.

cd /usr/local/

Mkdir redis_cluster // Create cluster directory

cd redis_cluster

Mkdir 7000 70017002 7003 7004 7005//represent 6 nodes respectively.

It corresponds to port 700070017002 70037004 7005.

2. Copy the configuration file to each directory.

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/7000/

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/700 1/

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/7002/

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/7003/

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/7004/

CP/www/server/redis/redis . conf/usr/local/redis _ cluster/7005/

3. Modify the configuration files respectively

vim/usr/local/redis _ cluster/7000/redis . conf

vim/usr/local/redis _ cluster/700 1/redis . conf

vim/usr/local/redis _ cluster/7002/redis . conf

vim/usr/local/redis _ cluster/7003/redis . conf

vim/usr/local/redis _ cluster/7004/redis . conf

vim/usr/local/redis _ cluster/7005/redis . conf

as follows

Port 7000 # port

Enable cluster is # Enable cluster mode.

The configuration file of cluster-config-filenodes _ 7000.conf # is automatically generated for the first time.

Cluster node timeout 5000 # timeout 5 seconds.

Appendonly yes # Open logging. It will record a log for each write operation.

Background operation

Protection mode number # No protection mode

pidfile /var/run/redis_7000.pid

//Don't write below.

# If the password is set, both the master and the slave need to configure the following two parameters:

Masterauth "jijiji" # Password to connect to the host.

Ask for your own password.

The number corresponding to Cluster-config-file, port and pidfile.

4. Start the node

cd /www/server/redis/src/

. /redis-server/usr/local/redis _ cluster/7000/redis . conf

. /redis-server/usr/local/redis _ cluster/700 1/redis . conf

. /redis-server/usr/local/redis _ cluster/7002/redis . conf

. /redis-server/usr/local/redis _ cluster/7003/redis . conf

. /redis-server/usr/local/redis _ cluster/7004/redis . conf

. /redis-server/usr/local/redis _ cluster/7005/redis . conf

View redis operation

ps aux|grep redis

5. Start the cluster

/www/server/redis/src/redis-CLI-cluster create 127 . 0 . 0 . 1:7000 127 . 0 . 0 . 1 127 . 0 . 0 . 1 : 7002 1 27.0.0.1:70000.0.1:7004127.0.0.1:7005-cluster replica/kloc.

The command used here is create, because we want to create a new cluster. This option -cluster-replicate 1 means that we want each created master server to have a slave server.

Input yes

At this point, the Reids5 cluster is completed.

6. Check the status of Reids5 cluster.

You can execute redis-CLI-cluster checkhost: port to check the detailed allocation of cluster state slots.

Redis-cli-cluster information 127.0.0. 1:7000

7. Stop the Reids5 cluster.

(1). Because Redis can properly handle SIGTERM signals, it is also possible to directly kill -9. You can kill multiple times at the same time and then start in turn.

kill-9 PID PID PID PID

(2).redis5 provides a tool to close the cluster, and modifies the file:/www/server/redis/utils/create-cluster/create-cluster.

When the port PROT is set to 6999 and the node is set to 6, the tool will generate 7000-7005 nodes for operation.

After modification, execute the following command to shut down the cluster:

/www/server/redis/utils/create-cluster/create-cluster stop

Restart the cluster:

/www/server/redis/utils/create-cluster/create-cluster start

8. Help information

Execute redis-cli-cluster help to see more help information.

Redis-cli-Cluster Help

Ji Haibo