PHP Redis Client and Replica

PHP Redis Client and Replica

We can configure the replica connection client like this:
$timeout_parameters = "&timeout=10&read_write_timeout=10";

$parameters = array(
     'tcp://' . $redisHostMaster . "?alias=master&$timeout_parameters",
     'tcp://' . $redisHostSlave1 . "?alias=slave1&$timeout_parameters",
     'tcp://' . $redisHostSlave2 . "?alias=slave2&$timeout_parameters"
);

$this->clientMaster = new Client($parameters, array('replication' => true));

If we create a new Client every time we use it, it will be good. It can send all the writes to master, all the reads to replica. But if we use a single object of that connection, once it connect to master, it will be master forever. That is what I get from the testings.

So finally, my solution change to this, I will keep 2 singleton clients, one for all writes, one for all reads.
$timeout_parameters = "&timeout=10&read_write_timeout=10";

$parameters = array(
     'tcp://' . $redisHostMaster . "?alias=master&$timeout_parameters",
     'tcp://' . $redisHostSlave1 . "?alias=slave1&$timeout_parameters",
     'tcp://' . $redisHostSlave2 . "?alias=slave2&$timeout_parameters"
);

$this->clientMaster = new Client($parameters, array('replication' => true));
$this->clientSlaves = new Client($parameters, array());

And we upgrade the client to v.1.1.1 to support all these. composer.json is as follow:
{
"autoload" : {
        "psr-0": {
            "JobProducerPHP": "src/"
        }
    },
    "require": {
        "predis/predis": "1.1.1",
        "aws/aws-sdk-php": "3.0.6",
        "katzgrau/klogger": "dev-master",
        "pimple/pimple": "3.0",
        "solarium/solarium": "^3.6",
        "phpFastCache/phpFastCache": "^4.3"
    },
    "require-dev": {
        "php": ">=5.3.0",
        "phpunit/phpunit": "~4.5.1",
        "phpunit/dbunit": ">=1.2",
        "phpunit/php-invoker": "*"
    }
}

We need to execute command as follow:
> ./composer.phar update

References:
https://github.com/nrk/predis/tree/v1.1/examples
https://github.com/nrk/predis/blob/v0.7.1/examples/MasterSlaveReplication.php

https://github.com/nrk/predis/issues/21
https://github.com/nrk/predis/wiki/Connection-Parameters

cluster
https://www.zybuluo.com/phper/note/248555

猜你喜欢

转载自sillycat.iteye.com/blog/2357352