|
| 1 | +[[cluster]] |
| 2 | += Redis Cluster |
| 3 | + |
| 4 | +Working with http://redis.io/topics/cluster-spec[Redis Cluster] requires a Redis Server version 3.0+ and provides a very own set of features and capabilities. Please refer to the http://redis.io/topics/cluster-tutorial[Cluster Tutorial] for more information. |
| 5 | + |
| 6 | +TIP: Redis Cluster is only supported by <<redis:connectors:jedis,jedis>> and <<redis:connectors:lettuce,lettuce>>. |
| 7 | + |
| 8 | +== Enabling Redis Cluster |
| 9 | + |
| 10 | +Cluster support is based on the very same building blocks as non clustered communication. `RedisClusterConnection` and extension to `RedisConnection` handles the communication with the Redis Cluster and translates errors into the Spring DAO exception hierarchy. |
| 11 | +`RedisClusterConnection` 's are created via the `RedisConnectionFactory` which has to be set up with the according `RedisClusterConfiguration`. |
| 12 | + |
| 13 | +.Sample RedisConnectionFactory Configuration for Redis Cluster |
| 14 | +==== |
| 15 | +[source,java] |
| 16 | +---- |
| 17 | +@Configuration |
| 18 | +public class AppConfig { |
| 19 | +
|
| 20 | + /* |
| 21 | + * spring.redis.cluster.nodes[0] = 127.0.0.1:7379 |
| 22 | + * spring.redis.cluster.nodes[1] = 127.0.0.1:7380 |
| 23 | + * ... |
| 24 | + */ |
| 25 | + @Value("${spring.redis.cluster.nodes}") |
| 26 | + private Collection<String> initialClusterNodes; |
| 27 | +
|
| 28 | + public @Bean RedisConnectionFactory connectionFactory() { |
| 29 | +
|
| 30 | + return new JedisConnectionFactory( |
| 31 | + new RedisClusterConfiguration(initialClusterNodes)); |
| 32 | + } |
| 33 | +} |
| 34 | +---- |
| 35 | +==== |
| 36 | + |
| 37 | +NOTE: The initial configuration points driver libraries to an initial set of cluster nodes. Changes resulting from live cluster reconfiguration will only be kept in the native driver and not be written back to the configuration. |
| 38 | + |
| 39 | +== Working With Redis Cluster Connection |
| 40 | + |
| 41 | +As mentioned above Redis Cluster behaves different from single node Redis or even a Sentinel monitored master slave environment. This is reasoned by the automatic sharding that maps a key to one of 16384 slots which are distributed across the nodes. Therefore commands that involve more than one key must assert that all keys map to the exact same slot in order to avoid cross slot execution errors. |
| 42 | +Further on, hence a single cluster node, only servers a dedicated set of keys, commands issued against one particular server only return results for those keys served by the server. As a very simple example take the `KEYS` command. When issued to a server in cluster environment it only returns the keys served by the node the request is sent to and not necessarily all keys within the cluster. So to get all keys in cluster environment it is necessary to read the keys from at least all known master nodes. |
| 43 | + |
| 44 | +While redirects for to a specific keys to the corresponding slot serving node are handled by the driver libraries, higher level functions like collecting information across nodes, or sending commands to all nodes in the cluster that are covered by `RedisClusterConnection`. Picking up the keys example from just before, this means, that the `keys(pattern)` method picks up every master node in cluster and simultaneously executes the `KEYS` command on every single one, while picking up the results and returning the cumulated set of keys. To just request the keys of a single node `RedisClusterConnection` provides overloads for those (like `keys(node, pattern)` ). |
| 45 | + |
| 46 | +.Sample of Running Commands Across the Cluster |
| 47 | +==== |
| 48 | +[source,text] |
| 49 | +---- |
| 50 | +redis-cli@127.0.0.1:7379 > cluster nodes |
| 51 | +
|
| 52 | +6b38bb... 127.0.0.1:7379 master - 0 0 25 connected 0-5460 <1> |
| 53 | +7bb78c... 127.0.0.1:7380 master - 0 1449730618304 2 connected 5461-10922 <2> |
| 54 | +164888... 127.0.0.1:7381 master - 0 1449730618304 3 connected 10923-16383 <3> |
| 55 | +b8b5ee... 127.0.0.1:7382 slave 6b38bb... 0 1449730618304 25 connected <4> |
| 56 | +---- |
| 57 | +
|
| 58 | +[source,java] |
| 59 | +---- |
| 60 | +RedisClusterConnection connection = connectionFactory.getClusterConnnection(); |
| 61 | +
|
| 62 | +connection.set("foo", value); <5> |
| 63 | +connection.set("bar", value); <6> |
| 64 | +
|
| 65 | +connection.keys("*"); <7> |
| 66 | +
|
| 67 | +connection.keys(NODE_7379, "*"); <8> |
| 68 | +connection.keys(NODE_7380, "*"); <9> |
| 69 | +connection.keys(NODE_7381, "*"); <10> |
| 70 | +connection.keys(NODE_7382, "*"); <11> |
| 71 | +---- |
| 72 | +<1> Master node serving slots 0 to 5460 replicated to slave at 7382 |
| 73 | +<2> Master node serving slots 5461 to 10922 |
| 74 | +<3> Master node serving slots 10923 to 16383 |
| 75 | +<4> Slave node holding replicates of master at 7379 |
| 76 | +<5> Request routed to node at 7381 serving slot 12182 |
| 77 | +<6> Request routed to node at 7379 serving slot 5061 |
| 78 | +<7> Request routed to nodes at 7379, 7380, 7381 -> [foo, bar] |
| 79 | +<8> Request routed to node at 7379 -> [bar] |
| 80 | +<9> Request routed to node at 7380 -> [] |
| 81 | +<10> Request routed to node at 7381 -> [foo] |
| 82 | +<11> Request routed to node at 7382 -> [bar] |
| 83 | +==== |
| 84 | + |
| 85 | +Cross slot requests such as `MGET` are automatically served by the native driver library when all keys map to the same slot. However once this is not the case `RedisClusterConnection` executes multiple parallel `GET` commands against the slot serving nodes and again returns a cumulated result. Obviously this is less performing than the single slot execution and therefore should be used with care. In doubt please consider pinning keys to the same slot by providing a prefix in curly brackets like `{my-prefix}.foo` and `{my-prefix}.bar` which will both map to the same slot number. |
| 86 | + |
| 87 | +.Sample of Cross Slot Request Handling |
| 88 | +==== |
| 89 | +[source,text] |
| 90 | +---- |
| 91 | +redis-cli@127.0.0.1:7379 > cluster nodes |
| 92 | +
|
| 93 | +6b38bb... 127.0.0.1:7379 master - 0 0 25 connected 0-5460 <1> |
| 94 | +7bb... |
| 95 | +---- |
| 96 | +
|
| 97 | +[source,java] |
| 98 | +---- |
| 99 | +RedisClusterConnection connection = connectionFactory.getClusterConnnection(); |
| 100 | +
|
| 101 | +connection.set("foo", value); // slot: 12182 |
| 102 | +connection.set("{foo}.bar", value); // slot: 12182 |
| 103 | +connection.set("bar", value); // slot: 5461 |
| 104 | +
|
| 105 | +connection.mGet("foo", "{foo}.bar"); <2> |
| 106 | +
|
| 107 | +connection.mGet("foo", "bar"); <3> |
| 108 | +---- |
| 109 | +<1> Same Configuration as in the sample before. |
| 110 | +<2> Keys map to same slot -> 127.0.0.1:7381 MGET foo {foo}.bar |
| 111 | +<3> Keys map to different slots and get split up into single slot ones routed to the according nodes + |
| 112 | + -> 127.0.0.1:7379 GET bar + |
| 113 | + -> 127.0.0.1:7381 GET foo |
| 114 | +==== |
| 115 | + |
| 116 | +TIP: The above provides simple examples to demonstrate the general strategy followed by Spring Data Redis. Be aware that some operations might require loading huge amounts of data into memory in order to compute the desired command. Additionally not all cross slot requests can safely be ported to multiple single slot requests and will error if misused (eg. `PFCOUNT` ). |
| 117 | + |
| 118 | +== Working With RedisTemplate and ClusterOperations |
| 119 | + |
| 120 | +Please refer to the section <<redis:template>> to read about general purpose, configuration and usage of `RedisTemplate`. |
| 121 | + |
| 122 | +WARNING: Please be careful when setting up `RedisTemplate#keySerializer` using any of the Json `RedisSerializers` as changing json structure has immediate influence on hash slot calculation. |
| 123 | + |
| 124 | +`RedisTemplate` provides access to cluster specific operations via the `ClusterOperations` interface that can be obtained via `RedisTemplate.opsForCluster()`. This allows to execute commands explicitly on a single node within the cluster while retaining de-/serialization features configured for the template and provides administrative commands such as `CLUSTER MEET` or more high level operations for eg. resharding. |
| 125 | + |
| 126 | + |
| 127 | +.Accessing RedisClusterConnection via RedisTemplate |
| 128 | +==== |
| 129 | +[source,text] |
| 130 | +---- |
| 131 | +ClusterOperations clusterOps = redisTemplate.opsForCluster(); |
| 132 | +clusterOps.shutdown(NODE_7379); <1> |
| 133 | +---- |
| 134 | +<1> Shut down node at 7379 and cross fingers there is a slave in place that can take over. |
| 135 | +==== |
| 136 | + |
0 commit comments