You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Credis also includes a way for developers to fully utilize the scalability of Redis with multiple servers and [consistent hashing](http://en.wikipedia.org/wiki/Consistent_hashing).
82
-
Using the [Credis_Cluster](Cluster.php) class, you can use Credis the same way, except that keys will be hashed across multiple servers.
83
-
Here is how to set up a cluster:
81
+
Credis also includes a way for developers to fully utilize the [scalability of Redis cluster](https://redis.io/docs/latest/operate/oss_and_stack/management/scaling/) by using Credis_Cluster which is an adapter for the RedisCluster class from [the Redis extension for PHP](https://github.com/phpredis/phpredis). This also works on [AWS ElastiCatch clusters](https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/Clusters.html).
82
+
This feature requires the PHP extension for its functionality. Here is an example how to set up a cluster:
The consistent hashing strategy stores keys on a so called "ring". The position of each key is relative to the position of its target node. The target node that has the closest position will be the selected node for that specific key.
103
-
104
-
To avoid an uneven distribution of keys (especially on small clusters), it is common to duplicate target nodes. Based on the number of replicas, each target node will exist *n times* on the "ring".
105
-
106
-
The following example explicitly sets the number of replicas to 5. Both Redis instances will have 5 copies. The default value is 128.
The Credis_Cluster constructor can either take a cluster name (from redis.ini) or a seed of cluster nodes (An array of strings which can be hostnames or IP address, followed by ports). RedisCluster gets cluster information from one of the seeds at random, so we don't need to pass it all the nodes, and don't need to worry if new nodes are added to cluster.
104
+
Many methods of Credis_Cluster are compatible with Credis_Client, but there are some differences.
123
105
124
-
##Master/slave replication
106
+
### Differences between the Credis_Client and Credis_Cluster classes
125
107
126
-
The [Credis_Cluster](Cluster.php) class can also be used for [master/slave replication](http://redis.io/topics/replication).
127
-
Credis_Cluster will automatically perform *read/write splitting* and send the write requests exclusively to the master server.
128
-
Read requests will be handled by all servers unless you set the *write_only* flag to true in the connection string of the master server.
129
-
130
-
### Redis server settings for master/slave replication
131
-
132
-
Setting up master/slave replication is simple and only requires adding the following line to the config of the slave server:
The following example illustrates how to disable reading on the master server. This will cause the master server only to be used for writing.
159
-
This should only happen when you have enough write calls to create a certain load on the master server. Otherwise this is an inefficient usage of server resources.
[Redis Sentinel](http://redis.io/topics/sentinel) is a system that can monitor Redis instances. You register master servers and Sentinel automatically detects its slaves.
176
-
177
-
When a master server dies, Sentinel will make sure one of the slaves is promoted to be the new master. This autofailover mechanism will also demote failed masters to avoid data inconsistency.
178
-
179
-
The [Credis_Sentinel](Sentinel.php) class interacts with the *Redis Sentinel* instance(s) and acts as a proxy. Sentinel will automatically create [Credis_Cluster](Cluster.php) objects and will set the master and slaves accordingly.
180
-
181
-
Sentinel uses the same protocol as Redis. In the example below we register the Sentinel server running on port *26379* and assign it to the [Credis_Sentinel](Sentinel.php) object.
182
-
We then ask Sentinel the hostname and port for the master server known as *mymaster*. By calling the *getCluster* method we immediately get a [Credis_Cluster](Cluster.php) object that allows us to perform basic Redis calls.
183
-
184
-
```php
185
-
<?php
186
-
require 'Credis/Client.php';
187
-
require 'Credis/Cluster.php';
188
-
require 'Credis/Sentinel.php';
189
-
190
-
$sentinel = new Credis_Sentinel(new Credis_Client('127.0.0.1',26379));
echo 'Writing to master: '.$masterAddress[0].' on port '.$masterAddress[1].PHP_EOL;
195
-
$cluster->set('key','value');
196
-
echo $cluster->get('key').PHP_EOL;
197
-
```
198
-
### Additional parameters
199
-
200
-
Because [Credis_Sentinel](Sentinel.php) will create [Credis_Cluster](Cluster.php) objects using the *"getCluster"* or *"createCluster"* methods, additional parameters can be passed.
201
-
202
-
First of all there's the *"write_only"* flag. You can also define the selected database and the number of replicas. And finally there's a *"selectRandomSlave"* option.
203
-
204
-
The *"selectRandomSlave"* flag is used in setups for masters that have multiple slaves. The Credis_Sentinel will either select one random slave to be used when creating the Credis_Cluster object or to pass them all and use the built-in hashing.
205
-
206
-
The example below shows how to use these 3 options. It selects database 2, sets the number of replicas to 10, it doesn't select a random slave and doesn't allow reading on the master server.
207
-
208
-
```php
209
-
<?php
210
-
require 'Credis/Client.php';
211
-
require 'Credis/Cluster.php';
212
-
require 'Credis/Sentinel.php';
213
-
214
-
$sentinel = new Credis_Sentinel(new Credis_Client('127.0.0.1',26379));
* RedisCluster currently has limitations like not supporting pipeline or multi. This may be added in the future. See [here](https://github.com/phpredis/phpredis/blob/develop/cluster.md) for details.
109
+
* Many methods require an additional parameter to specify which node to run on, and only run on that node, such as saveForNode(), flushDbForNode(), and pingForNode(). To specify the node, the first argument will either be a key which maps to a slot which maps to a node; or it can be an array of ['host': port] for a node.
110
+
* Redis clusters do not support select(), as they only have a single database.
111
+
* RedisCluster currently has buggy/broken behaviour for pSubscribe and script. This appears to be a bug and hopefully will be fixed in the future.
219
112
220
-
## About
113
+
### Note about tlsOptions for Credis_Cluster
114
+
Because of weirdness in the behaviour of the $tlsOptions parameter of Credis_Cluster, when a seed is defined with a URL that starts with tls:// or ssl://, if $tlsOptions is null, then it will still try to connect without TLS, and it will fail. This odd behaviour is because the connections to the nodes are gotten from the CLUSTER SLOTS command and those hostnames or IP address do not get prefixed with tls:// or ssl://, and it uses the existance of $tlsOptions array for determining which type of connection to make. If you need TLS connection, the $tlsOptions value MUST be either an empty array, or an array with values. If you want the connections to be made without TLS, then the $tlsOptions array MUST be null.
Copy file name to clipboardExpand all lines: Sentinel.php
+1-72Lines changed: 1 addition & 72 deletions
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,6 @@
6
6
* Implements the Sentinel API as mentioned on http://redis.io/topics/sentinel.
7
7
* Sentinel is aware of master and slave nodes in a cluster and returns instances of Credis_Client accordingly.
8
8
*
9
-
* The complexity of read/write splitting can also be abstract by calling the createCluster() method which returns a
10
-
* Credis_Cluster object that contains both the master server and a random slave. Credis_Cluster takes care of the
11
-
* read/write splitting
12
-
*
13
9
* @author Thijs Feryn <thijs@feryn.eu>
14
10
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
15
11
* @package Credis_Sentinel
@@ -25,6 +21,7 @@ class Credis_Sentinel
25
21
26
22
/**
27
23
* Contains an active instance of Credis_Cluster per master pool
24
+
* @deprecated no longer used
28
25
* @var array
29
26
*/
30
27
protected$_cluster = array();
@@ -250,74 +247,6 @@ public function getSlaveClients($name)
250
247
return$this->_slaves[$name];
251
248
}
252
249
253
-
/**
254
-
* Returns a Redis cluster object containing a random slave and the master
255
-
* When $selectRandomSlave is true, only one random slave is passed.
256
-
* When $selectRandomSlave is false, all clients are passed and hashing is applied in Credis_Cluster
257
-
* When $writeOnly is false, the master server will also be used for read commands.
258
-
* When $masterOnly is true, only the master server will also be used for both read and write commands. $writeOnly will be ignored and forced to set to false.
0 commit comments