"After all, the engineers only needed to refuse to fix anything, and modern industry would grind to a halt." -Michael Lewis

Enable Massive Growth

Optimistic Locking in Redis with Reactive Lettuce

Apr 2021

The source code for what follows can be found on Github.

Optimistic Locking in Redis is one of the only reasons to want to use transactions, in my opinion. You can ensure a grouping of atomic operations only occur if a watched key does not change out from underneath you. On the CLI, this might start with:> SET key1 value1
OK> WATCH key1

WATCH is saying "redis, watch key1 for me, and if it changes at all then rollback the transaction I am about to start." Then we actually start the transaction with MULTI.

Now on this same thread, let's say we issue two commands before committing [before the EXEC, command, that is]:> SET key2 value2
QUEUED> SET key1 newvalue

Obviously, because we're in a transaction, we have not actually "committed" either of these just yet. If we now start up another terminal and run:

# different shell than the one with the open transaction> SET key1 changedbysomeoneelse

And then we try to commit the transaction we started above, we can see that it fails and neither operation was successful [which is the atomicity that we're looking for]:

# shell that has the open transaction> EXEC
(nil)> GET key2
(nil)> GET key1

So that's what it looks like on the CLI, what does optimistic locking look like in lettuce?

Optimistic Locking with Reactive Lettuce

As I droned on about my last article on transactions in redis using lettuce, you have to be very careful using any of what follows, but if you're sure you want to try it here it goes.

To really appreciate what follows, you will want to read that last post, and probably make sure you have something like a redis test container set up for lettuce. We will want to start by creating two connections, then we can start a transaction using one of them:

    public void optLocking() {
        RedisReactiveCommands<String, String> firstConnection =

        RedisReactiveCommands<String, String> secondConnection =

Similar to the CLI example above, we are first watching "key-1" for any changes, then starting a transaction. If "key-1" is modified by a different process while we execute anything in the following code using that connection, then it will roll back. Here's some code demonstrating that:


        secondConnection.set("key-1", "10").subscribe();

                // transaction not committed
                .expectNextMatches(tr -> tr.wasDiscarded())

                .expectNextMatches(val -> "10".equals(val))

We increment "key-1" in the existing transaction, then use a different connection [which is obviously not in the same transaction] to change that key. When we then try to commit the transaction, redis aborts it on our behalf because that key has already been changed by a different thread.

I would still recommend you find a different way to solve your problem due to the caveats mentioned in that last post, but if you're intent on doing it, this approach could technically work.

Nick Fisher is a software engineer in the Pacific Northwest. He focuses on building highly scalable and maintainable backend systems.