Support for transactions when using Redis cluster?

1,953 views
Skip to first unread message

fj

unread,
Sep 17, 2014, 9:17:48 AM9/17/14
to redi...@googlegroups.com
Hi all,

I'm playing a bit with redis cluster (beta8)

Does anyone know if it's possible and supported to use transactions in a Redis cluster assuming that all keys in the transaction belong to the same Redis instance?

For example I would like to do:

MULTI
HSET user:12345678 some-key "some-value"
HSET user:12345678 other-key "other-value"
EXEC

It seems that I can do this from redis-cli without problems, but I wonder if it is also supported in the cluster aware clients?

Regards,

Flemming


Josiah Carlson

unread,
Sep 17, 2014, 5:51:16 PM9/17/14
to redi...@googlegroups.com
As long as the keys are in the same shard, it will work without issue. Keys on different shards may or may not work (the last time I read the spec, that wasn't clarified). If you are using the same key in all of your operations, operations should all go on the same shard. In the example you provided, you could also use HMSET.

If you have different keys, but want to ensure they are in the same shard, Redis supports the bracketing of keys. This bracketing tells Redis what part of the key to calculate the shard from. Specifically: user:{12345678} would go on the same shard as info:{12345678}, but it might not go into the same shard as user:{87654321}.

 - Josiah

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Marc Gravell

unread,
Sep 17, 2014, 5:53:31 PM9/17/14
to redi...@googlegroups.com

Edge case: if the hash-slot is migrating and a key is missing you could hit glitches. From memory, this scenario results in a transaction abort.

Marc

fj

unread,
Sep 18, 2014, 7:46:11 AM9/18/14
to redi...@googlegroups.com
Hi Josiah,

Thanks for the input.
In my transactions I will make sure to only send commands for the same shard-key in all cases probably by using the {} syntax as you also mention.
I am aware of the HMSET to set multiple fields in one go, the point in the example was to show my need for multiple commands to be handled in one atomic transaction or all rolled back.
A better example could also include different commands and different hashes e.g.

MULTI
HMSET user:{12345678} some-field "some-value" other-field "other-value"
HMSET log:{12345678} this-field "this-value" that-field "that-value"
DEL userdata:{12345678}
EXEC

What I'm mostly concerned about is that the client libraries I will use (Java and C/C++) do not seem to support transactions for Redis cluster (yet).
Jedis not seem to support it yet in the cluster code, and hiredis C library is not cluster aware yet.
If anyone have information about the future of this feature in those libraries that you would like to share then please do so :)

Regards,

Flemming

fj

unread,
Sep 18, 2014, 7:48:35 AM9/18/14
to redi...@googlegroups.com
Hi Marc,

Thanks for your input.
As long as the entire transaction is aborted leaving the db in a consistent state it sounds fine, then it is up to the client to retry or abort entirely.

Regards,

Flemming

Marc Gravell

unread,
Sep 18, 2014, 8:54:10 AM9/18/14
to redi...@googlegroups.com
FWIW, StackExchange.Redis (a .NET redis client) supports single-slot redis transactions, although I haven't yet had time to fix up the new "CLUSTER NODES" topology replacement. From memory, redis actually allows transactions as long as they are all on the same *shard*, but personally I don't think that is via from a client perspective, as the end-user should never have to worry about which shard hosts which slots. Consequently, StackExchange.Redis only supports transactions that touch a single slot. If you try to touch multiple slots, it immediately fails (without ever talking to the server) citing:

"Multi-key operations must involve a single slot; keys can use 'hash tags' to help this, i.e. '{/users/12345}/account' and '{/users/12345}/contacts' will always be in the same slot"

Marc
--
Regards,

Marc

fj

unread,
Sep 18, 2014, 9:03:05 AM9/18/14
to redi...@googlegroups.com
Thanks Marc - so seems I just need to persuade the Jedis guys and Salvatore to implement this in their libs :-)
Regards,
Flemming
Reply all
Reply to author
Forward
0 new messages