Summary of PHP in redis sentinel mode


In the near future, the cache of the company’s projects wants to be migrated from Memcache to redis. The redis usage mode configured for operation and maintenance is sentinel (sentinel mode), which has not been used before. Please record here.

Briefly introduce redis sentinel:
Redis sentinel is a monitoring management, notification and instance failure backup service for redis instances, and a management tool for redis clusters. In the general distributed central node database, redis sentinel is used to monitor the work of the central node, monitor the work of other nodes and perform fault recovery, so as to improve the high availability of the cluster
(introduction from…)

My own understanding is that if there are three available redis servers: A, B and C, where a is the primary server and B and C is the secondary server.
In sentinel mode, if there is a problem with a, you can automatically remove or demote a server to a slave server, and then select a primary server from B and C according to a certain policy, and then a will be used as a slave server after recovery, avoiding affecting the entire redis service due to the problem of a server.

The redis sentinel mode is undoubtedly a good support for redis services, but it will be a little bit troublesome when developers use it. Because the more commonly used mode is to write a configuration file directly in the program. The master-slave database of redis is determined. The program reads the configuration file for redis operation. If there is a problem with the main library, you must manually modify the configuration file to continue normal use. If it is found later, it may cause unnecessary losses.

In sentinel mode, the program needs to obtain the IP of the master-slave library dynamically. Even if there is a problem with a redis service, it will not affect the program, thus avoiding the problem of manually modifying the configuration. The key problem is to dynamically obtain the IP and port information of the master-slave database.
Looking up related materials on the Internet, most of them are about how to configure sentinel service, while PHP is not used in many ways. After reading the official redis sentinel document, I learned that there is a corresponding API for getting sentinel information, just some native commands.

The PHP redis library supports native commands as follows:

     * Send arbitrary things to the redis server.
     * @param   string      $command    Required command to send to the server.
     * @param   mixed       ...$arguments  Optional variable amount of arguments to send to the server.
     * @return  mixed
     * @example
     * <pre>
     * $redis->rawCommand('SET', 'key', 'value'); // bool(true)
     * $redis->rawCommand('GET", 'key'); // string(5) "value"
     * </pre>
    public function rawCommand( $command, $arguments ) {}

The general process of development and use is as follows
1. Connect sentinel according to the sentinel configuration information given by O & M to get the required information (Note: here is the configuration information of sentinel service, which is not consistent with redis). There will be multiple configuration information (IP, port). The redis master-slave configuration information returned by each sentinel is consistent, so the information returned by one of the services can be used for operation.

//Initialize redis object
$redis = new Redis();
//Connect sentinel service host as IP, port as port
$redis->connect($host, $port);

//Some commands that may be used can be viewed in official documents

//Get the main library list and its status information
$result = $redis->rawCommand('SENTINEL', 'masters');

//Obtain the corresponding information according to the configured main database redis name
//Master "name should be informed by operation and maintenance (it can also be obtained from the information in the previous step)
$result = $redis->rawCommand('SENTINEL', 'master', $master_name);

//Obtain the list of corresponding slave databases and their information according to the configured main database redis name
$result = redis->rawCommand('SENTINEL', 'slaves', $master_name);

//Get the address of the redis main database with a specific name
$result = $redis->rawCommand('SENTINEL', 'get-master-addr-by-name', $master_name)

//This method can parse the information returned by the above sentinel into an array
function parseArrayResult(array $data)
    $result = array();
    $count = count($data);
    for ($i = 0; $i < $count;) {
        $record = $data[$i];
        if (is_array($record)) {
            $result[] = parseArrayResult($record);
        } else {
            $result[$record] = $data[$i + 1];
            $i += 2;
    return $result;

2. Through the above operations, you can get the information of the redis master-slave database. The redis password is fixed, and you can write it yourself. After that, you can use redis according to the singleton mode. Pay attention to the separation of master and slave during operation.

The above is only a general use process, which may be adjusted in different situations.

I’ve just mentioned how to use this mode in the PHP program. Please move to the next step for the specific working principle and configuration……

Recommended Today

Windows server side programming – Chapter 2 device IO and inter thread communication – 8 – the architecture of ports around I / O

Architecture of ports around I / O When the service application is initialized, I / O completion ports are created through functions such as createnewcompletionport. The application also needs to create a thread pool to handle client requests. Now the question is: how many threads should there be in the thread pool? This question is […]