Home > Article > Backend Development > PHP development: Implementing HA mode and failover using Redis and Sentinel
In the traditional single-node architecture, all requests for an application will be sent to the same server. Although this architecture is relatively simple, it presents the risk of a single point of failure. If the server where the application is located is down, all requests will fail.
To solve this problem, we need to deploy the application on multiple servers to achieve high availability (HA) mode and failover. In this article, we'll cover how to implement high availability and failover using Redis and Sentinel.
Redis is a fast in-memory data storage system that is commonly used for caching, queuing, real-time analytics, and messaging. Redis features a single-threaded design and uses memory as storage. This gives Redis excellent performance and scalability.
Sentinel is a high availability solution for Redis. It can monitor Redis instances and automatically switch to backup nodes when the primary node fails. This automatic failover ensures that your Redis instance is always available.
Here are the steps to achieve high availability and failover using Redis and Sentinel.
First step, install Redis and Sentinel
On CentOS, you can use the following command to install Redis and Sentinel:
yum install redis sentinel
On Ubuntu, you can use the following command to install Redis And Sentinel:
apt-get install redis sentinel
The second step is to modify the Redis configuration file
On the primary node and backup node, you need to modify the Redis configuration file. On CentOS, the configuration file is located at /etc/redis.conf
, and on Ubuntu, the configuration file is located at /etc/redis/redis.conf
.
In the configuration file, the bind
parameter needs to be set to the IP address of the node. If you want to allow clients from any IP to connect to Redis, you can set the bind
parameter to 0.0.0.0.
You also need to set the requirepass
parameter to a password to ensure that only authorized users can access Redis.
Finally, set the daemonize
parameter to yes to ensure that Redis is running in the background.
bind 192.168.0.1 requirepass mypassword daemonize yes
For the backup node, you also need to set the slaveof
parameter to the IP address and port number of the primary node.
slaveof 192.168.0.1 6379
The third step, modify the Sentinel configuration file
On CentOS, the Sentinel configuration file is located at /etc/redis-sentinel.conf
, on Ubuntu, Sentinel configuration The file is located at /etc/redis/sentinel.conf
.
In the Sentinel configuration file, the bind
parameter needs to be set to ensure that Sentinel receives requests from the client. If you want to allow clients from any IP address to connect to Sentinel, you can set the bind
parameter to 0.0.0.0.
You also need to set the sentinel monitor
parameter to monitor the Redis instance. This parameter contains the name, IP address, port number, and failover threshold of the Redis instance.
bind 192.168.0.2 sentinel monitor mymaster 192.168.0.1 6379 2
In this example, Sentinel will monitor the Redis instance named mymaster
. If the primary node fails, Sentinel will automatically start a new Redis instance on the backup node and promote it to the primary node.
The fourth step, start Redis and Sentinel
On each node, start Redis and Sentinel:
systemctl start redis systemctl start redis-sentinel
After the master node is started, Sentinel will start monitoring the Redis instance . If the primary node fails, Sentinel will automatically start a new Redis instance on the backup node and promote it to the primary node. During this process, the client will automatically switch to the new master node and the application will continue to work normally.
You can use the following command to check the status of the Redis instance:
redis-cli -h 192.168.0.1 -p 6379 ping
You can use the following command to check the status of Sentinel:
redis-cli -h 192.168.0.2 -p 26379 sentinel slaves mymaster
In this example, we connect Sentinel to 192.168.0.2 on port 26379 and check the backup node for the Redis instance named mymaster
.
Summary
In this article, we introduced how to achieve high availability and failover using Redis and Sentinel. By deploying the application on multiple servers and using automatic failover, you can ensure that the application continues to work properly if the primary node fails.
In this way, you increase the availability of your application and reduce the risk of single points of failure. We hope you found this article helpful and wish you the best of luck in using Redis and Sentinel.
The above is the detailed content of PHP development: Implementing HA mode and failover using Redis and Sentinel. For more information, please follow other related articles on the PHP Chinese website!