Home  >  Article  >  Database  >  Construction and use of Redis cluster

Construction and use of Redis cluster

WBOY
WBOYOriginal
2023-05-10 15:16:361519browse

Redis is a non-relational, high-performance key-value database with features such as fast reading and writing, support for multiple data structures, and data persistence. It is widely used in caches, counters, message queues, and other scenarios. middle. In actual applications, the performance of a single Redis instance has certain limitations, so a Redis cluster needs to be used to achieve horizontal expansion and high availability. This article will introduce the construction and use of Redis cluster.

1. What is Redis cluster

Redis cluster is a distributed system composed of multiple Redis nodes. Each node can handle client requests, and the nodes are replicated and distributed through slice data to ensure data consistency and availability. Redis cluster adopts the sharding mode to realize distributed storage of data. The data is dispersed among multiple nodes. Each node only stores part of the data. Different keys are mapped to different nodes through a certain algorithm.

Redis cluster mainly includes two node types: master node and slave node. The master node is responsible for receiving write requests from clients, replicating the data of the master node from slave nodes, and receiving read requests from clients. A master-slave replication relationship can also be formed between slave nodes to achieve node redundancy and failover. When the master node fails, one of the slave nodes will automatically become the master node to ensure the availability of the cluster.

2. Construction of Redis cluster

1. Environment preparation

Before starting to build the Redis cluster, you need to prepare the following environment:

(1) Linux server: It can be built using virtual machines or cloud hosts. It is recommended to prepare at least 3 servers.

(2) Redis installation package: Download the latest stable version of the Redis installation package from the Redis official website (https://redis.io/download).

2. Install Redis

Extract the downloaded Redis installation package to the server, compile and install Redis:

tar xvzf redis-6.0.7.tar.gz
cd redis-6.0.7
make
make install

After the installation is complete, you can use the redis-server command to start the Redis server, and use the redis-cli command to connect to the Redis server for testing.

3. Configure and start the Redis cluster

The configuration of the Redis cluster is completed through a configuration file redis.conf. Create configuration files for multiple Redis nodes on the server, named redis_7000.conf, redis_7001.conf, etc. The content of the configuration file for each node is as follows:

port 7000
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 5000
daemonize yes
logfile "/var/log/redis/redis.log"
pidfile "/var/run/redis /redis.pid"
dir "/var/lib/redis"

Among them, port specifies the port number of the node, cluster-enabled means turning on the cluster mode, and cluster-config-file specifies the configuration file of the node. , cluster-node-timeout specifies the timeout for communication between nodes, daemonize specifies whether to run in daemon mode, logfile and pidfile specify the paths of the log and pid files respectively, and dir specifies the path of the data file.

After modifying the configuration file of each node, use the redis-server command to start the Redis server of each node.

4. Create a cluster and add nodes

Use the redis-cli command to connect to any Redis node, and use the cluster meet command to add other nodes:

redis-cli -c -p 7000
cluster meet 127.0.0.1 7001
cluster meet 127.0.0.1 7002

Then use the cluster addslots command to add slots to each node, for example:

cluster addslots 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Finally, use the cluster replicate command to specify a master node for the slave node, for example:

cluster replicate

Replace with the ID of the corresponding master node, and use the cluster nodes command to view the information and status of all nodes.

3. Use of Redis cluster

1. Read and write operations

Read and write operations in the Redis cluster are the same as ordinary Redis operations. You can use redis-cli to connect to Any node, and then execute set and get commands, for example:

redis-cli -c -p 7000
set name "Redis Cluster"
get name

2. Expansion and reduction

You can use the cluster addslots and cluster delslots commands to add or delete slots for the Redis cluster, for example:

Add slots

cluster addslots 1024

Delete slots

cluster delslots 0

After adding or deleting slots, you need to execute the cluster rebalance command to reallocate the slots.

3. Failover and recovery

Redis cluster supports the function of automatically switching the slave node to the master node after the master node fails. You can use the cluster failover command to simulate the master node failure or manually switch the master-slave relationship. , for example:

Simulate master node failure

cluster failover

Manually switch the master-slave relationship

cluster replicate # Modify the master node of the slave node Node
cluster failover

When the master node recovers, the slave node will automatically recover as a slave node and synchronize data with the master node.

4. Summary

This article introduces the construction and use of Redis cluster. Redis cluster can achieve horizontal expansion and high availability, providing a good solution for large-scale data storage and high concurrent access. However, the establishment and operation and maintenance of the Redis cluster are also relatively complex, and data backup and fault tolerance need to be done well. In actual use, it is necessary to choose an appropriate Redis cluster solution based on the business scenario and system scale.

The above is the detailed content of Construction and use of Redis cluster. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn