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
Copy file name to clipboardExpand all lines: arrays.markdown
+62-3Lines changed: 62 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ There are several ways of creating Redis arrays; they can be pre-defined in red
17
17
18
18
#### Declaring a new array with a list of nodes
19
19
<pre>
20
-
$ra = new RedisArray(array("host1", "host2:63792, "host2:6380"));
20
+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"));
21
21
</pre>
22
22
23
23
@@ -26,15 +26,27 @@ $ra = new RedisArray(array("host1", "host2:63792, "host2:6380"));
26
26
function extract_key_part($k) {
27
27
return substr($k, 0, 3); // hash only on first 3 characters.
28
28
}
29
-
$ra = new RedisArray(array("host1", "host2:63792, "host2:6380"), array("function" => "extract_key_part"));
29
+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("function" => "extract_key_part"));
30
30
</pre>
31
31
32
32
#### Defining a "previous" array when nodes are added or removed.
33
33
When a new node is added to an array, phpredis needs to know about it. The old list of nodes becomes the “previous” array, and the new list of nodes is used as a main ring. Right after a node has been added, some read commands will point to the wrong nodes and will need to look up the keys in the previous ring.
34
34
35
35
<pre>
36
36
// adding host3 to a ring containing host1 and host2. Read commands will look in the previous ring if the data is not found in the main ring.
37
-
$ra = new RedisArray(array('host1', 'host2', 'host3'), array('previous' => array('host1', 'host2')));
37
+
$ra = new RedisArray(array("host1", "host2", "host3"), array("previous" => array("host1", "host2")));
38
+
</pre>
39
+
40
+
#### Specifying the "retry_interval" parameter
41
+
The retry_interval is used to specify a delay in milliseconds between reconnection attempts in case the client loses connection with a server
42
+
<pre>
43
+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("retry_timeout" => 100)));
44
+
</pre>
45
+
46
+
#### Specifying the "lazy_connect" parameter
47
+
This option is useful when a cluster has many shards but not of them are necessarily used at one time.
48
+
<pre>
49
+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("lazy_connect" => true)));
38
50
</pre>
39
51
40
52
#### Defining arrays in Redis.ini
@@ -76,6 +88,53 @@ In order to control the distribution of keys by hand, you can provide a custom f
76
88
77
89
For instance, instanciate a RedisArray object with `new RedisArray(array("us-host", "uk-host", "de-host"), array("distributor" => "dist"));` and write a function called "dist" that will return `2` for all the keys that should end up on the "de-host" server.
78
90
91
+
You may also provide an array of 2 values that will be used as follows:
92
+
- The first value is the initial amount of shards in use before the resharding (the x first shards specified in the constructor)
93
+
- The second value is the resharding level, or number of resharding iterations.
94
+
95
+
For instance, suppose you started with 4 shards as follows:
96
+
<pre>
97
+
0 => 0 1 2 3
98
+
</pre>
99
+
100
+
After 1 iteration of resharding, keys will be assigned to the following servers:
101
+
<pre>
102
+
1 => 0 4 1 5 2 6 3 7
103
+
</pre>
104
+
105
+
After 2 iterations, keys will be assigned to the following servers:
106
+
<pre>
107
+
2 => 0 8 4 12 1 9 5 13 2 10 6 14 3 11 7 15
108
+
</pre>
109
+
110
+
After 3 iterations, keys will be assigned to the following servers:
The idea here is to be able to reshard the keys easily, without moving keys from 1 server to another.
118
+
119
+
The procedure to adopt is simple:
120
+
121
+
For each initial shard, setup a slave. For instance, for shard 1 we setup slave 5.
122
+
123
+
Keys will now be assigned to either shard 1 or shard 5. Once the application sees the new settings, just setup shard 5 as a master. Then, in order to reclaim memory, just cleanup keys from shard 1 that belong to shard 5 and vice-versa.
124
+
125
+
On the next iteration, setup a new slave 9 for shard 1 and a new slave 13 for shard 5.
126
+
127
+
Update the application settings, disconnect the new slaves and clean up the shards from keys that don't belong there anymore.
128
+
129
+
Apply the same procedure for each resharding iteration.
This declares that we started with 2 shards and moved to 4 then 8 shards. The number of initial shards is 2 and the resharding level (or number of iterations) is 2.
137
+
79
138
## Migrating keys
80
139
81
140
When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined, e.g. `_rehash(function ($host, $count){ ... });`.
0 commit comments