Skip to content

Redis cluster with non-transactional workloads may be broken #2151

Closed

Description

@josiahcarlson

This bug is the result of two different conversations on the Redis mailing list:
[1] - https://groups.google.com/d/topic/redis-db/38Oml2I5pmw/discussion
[2] - https://groups.google.com/d/topic/redis-db/4I0ELYnf3bk/discussion

To understand how Redis cluster might be broken, let's look at a simple use-case that is perfectly reasonable in non-clustered Redis: a client that wants to execute a group of commands on keys (in a single shard slot), but doesn't seem to need the transaction protection of MULTI/EXEC.

Normally, Jedis, redis-py, and other clients would just collect the commands together in a single buffer, and send them along to Redis upon calling .execute() or something similar (aka pipelining for most clients). In non-clustered Redis, this does exactly what you would expect: commands are executed one after another, their results sent back to the client. You know that commands were executed in-order, and that assuming your data/commands were properly combined, you get exactly the resulting system state you were interested in.

But in clustered Redis, because slot migration can basically begin/continue at any time outside of a MULTI/EXEC transaction, sending a sequence of commands that you expect to execute sequentially can fail in unique and interesting ways. As an example, consider 3 commands along with their possible outcomes:

  1. "LPUSH key value" - completes successfully because while 'key' might be in a slot being migrated, it is currently live on the server
  2. "RPOPLPUSH key okey" - fails because 'key' started migrating immediately after step 1 completed successfully
  3. "RPOP okey" - completes successfully, but may not have any data because step 2 failed

Because all three commands were sent at once, the client can only discover that the 2nd command failed well after it had already sent the third command. But at this point the system may be in a state that is not necessarily consistent with what a client would want/expect. I used simple lists here, but there is no reason why the commands couldn't be a series of ZUNIONSTORE/ZINTERSTORE operations, which could result in very mangled data.

At present, the only way to fix this class of problems is to wrap every block of commands in a MULTI/EXEC transaction, which verifies that all keys are available before executing all commands. This is a reasonable requirement, but it should be documented (it isn't).

Alternatively, in [2] I describe how a client could set that they are operating "asynchronously" (not waiting for a response before sending a new command), which would return an error for all commands following a single slot error during execution. This would allow for the client to recognize that something happened, reset the connection state, re-fetch the topology information, and re-send commands to the proper server(s).

For both pipelined and truly async clients (like Node.js), forcing all commands into an error state on a connection after a single error would prevent slot migration race conditions from mangling data accidentally, and would remove the requirement for MULTI/EXEC for these cases. Either way, whether the command changes I describe in [2] are warranted, or whether MULTI/EXEC is seen as the proper solution, some documentation for the sake of Redis client developers is warranted.

Activity

  1. Grokzen commented on Jan 7, 2015

    @Grokzen

    Bump! I have stumbled into the exact same problem in my cluster enabled python lib.

    In short MULTI/EXEC must report back MOVED/ASK errors that occur and not just True/False like it do currently. If this is not fixed for the cluster implementation then there is no safe solution to make pipelines that won't break when slots are moving.

  2. Grokzen commented on Jan 7, 2015

    @Grokzen

    If this is not fixed somehow for 3.0.0 final release then i am thinking about scrapping pipelines from the client lib until it is fixed in a future release.

  3. antirez commented on Jan 7, 2015

    @antirez
    Contributor

    Hello @Grokzen, this is not really a bug, and was addressed here: https://groups.google.com/d/msg/redis-dev/hhW7HCiD374/P2UrG8Z_F3MJ

    Pipelining is very important feature IMHO and form an idea. Even if you are not convinced, you should just wrap MULTI/EXEC by default in a pipeline, and let the user to opt-out if she/he knows what is doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions