How compareAndSet works internally in redis

talex :

spring-data-redis module contains RedisAtomicLong class.

In this class you can see

public boolean compareAndSet(long expect, long update) {

    return generalOps.execute(new SessionCallback<Boolean>() {

        @Override
        @SuppressWarnings("unchecked")
        public Boolean execute(RedisOperations operations) {
            for (;;) {
                operations.watch(Collections.singleton(key));
                if (expect == get()) {
                    generalOps.multi();
                    set(update);
                    if (operations.exec() != null) {
                        return true;
                    }
                }
                {
                    return false;
                }
            }
        }
    });
}

My question is why it works?

generalOps.multi() starts transaction after get() is invoked. It means that there is possibility that two different thread (or even client) can change value and both of them will succeed.

Is operations.watch prevent it somehow? JavaDoc doesn't explain purpose of this method.

PS: Minor question: why for (;;)? There is always one iteration.

sazzad :

Q: Is operations.watch prevent it somehow?

YES.

Quoting from Redis documentation about transaction:

WATCH is used to provide a check-and-set (CAS) behavior to Redis transactions.

WATCHed keys are monitored in order to detect changes against them. If at least one watched key is modified before the EXEC command, the whole transaction aborts, and EXEC returns a Null reply to notify that the transaction failed.

You can learn more about Redis transaction from that documentation.

Q: why for (;;)? There is always one iteration.

It seems the code you've posted is very old. From Google's cache of this url, I saw the code you provided which is dated back to Oct 15th, 2012!

Latest codes look much different:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91460&siteId=1