1

I have a Redis Cluster. I am using JedisCluster client to connect to my Redis.

My application is a bit complex and I want to basically control to which partition data from my application goes. For example, my application consists of sub-module A, B, C. Then I want that all data from sub-module A should go to partition 1 for example. Similarly data from sub-module B should go to partition 2 for example and so on.

I am using JedisCluster, but I don't find any API to write to a particular partition on my cluster. I am assuming I will have same partition names on all my Redis nodes and handling which data goes to which node will be automatically handled but to which partition will be handled by me.

I tried going through the JedisCluster lib at

https://github.com/xetorthio/jedis/blob/b03d4231f4412c67063e356a7c3acf9bb7e62534/src/main/java/redis/clients/jedis/JedisCluster.java

but couldn't find anything. Please help? Thanks in advance for the help.

mp911de
  • 17,546
  • 2
  • 55
  • 95
DTCool
  • 33
  • 6

1 Answers1

2

That's not how Redis Cluster works. With Redis Cluster, each node (partition) has a defined set of keys (slots) that it's handling. Writing a key to a master node which is not served by the master results in rejection of the command.

From the Redis Cluster Spec:

Redis Cluster implements a concept called hash tags that can be used in order to force certain keys to be stored in the same node.

[...]

The key space is split into 16384 slots, effectively setting an upper limit for the cluster size of 16384 master nodes (however the suggested max size of nodes is in the order of ~ 1000 nodes). Each master node in a cluster handles a subset of the 16384 hash slots.

You need to define at Cluster configuration-level which master node is exclusively serving a particular slot or a set of slots. The configuration results in data locality.

The slot is calculated from the key. The good news is that you can enforce a particular slot for a key by using Key hash tags:

There is an exception for the computation of the hash slot that is used in order to implement hash tags. Hash tags are a way to ensure that multiple keys are allocated in the same hash slot. This is used in order to implement multi-key operations in Redis Cluster.

Example:

{user1000}.following

The content between {…} is used to calculate the slot. Key hash tags allow you to group keys on particular nodes and enforce the same data locality when using arbitrary hash tags.

You can also go a step further by using known hash tags that map to slots (you'd need either precalculate a table or see this Gist). By using known hash tags that map to a specific slot you're able to select the slot and so the master node on which the data is located.

Everything else is handled by your Redis client.

mp911de
  • 17,546
  • 2
  • 55
  • 95
  • Thanks for the update. Will definitely try using it. – DTCool Jun 02 '16 at 17:53
  • I was going through redis and jedis documentation. There is one more thing called JedisFactory which allows me to set a database index. Will this database index serve the same purpose? I have provided link for the same - https://github.com/xetorthio/jedis/blob/b03d4231f4412c67063e356a7c3acf9bb7e62534/src/main/java/redis/clients/jedis/JedisFactory.java – DTCool Jun 02 '16 at 19:47
  • Multiple databases (database numbers) are not supported with Redis Cluster. – mp911de Jun 02 '16 at 20:05