Reputation: 11
I've implemented custom locking service with Kotlin and Spring Boot Webflux to prevent transaction of same user is processed at the same time. the second transaction should loop until the first one is finished and release the lock key. but after few second (not reach ttl yet) the second process can lock, before acquire lock I also print log for the current lock status of user but turns out got empty value. Can anyone help me identify the problem? not sure which info should I provide more. I suspect the cause is Redis cluster behavior but not sure how and why.
This is my code for trying to lock with same key, the process that calling this will loop until get true from this
redisTemplate
.opsForValue()
.get(lockKey)
.flatMap { containValue ->
redisTemplate.getExpire(lockKey).flatMap { ttl ->
Mono.fromSupplier {
// print log
containValue.isNullOrEmpty()
}
}
}
.switchIfEmpty(Mono.just(true))
.flatMap { isUnlocked ->
// print log
if (isUnlocked) {
redisTemplate
.opsForValue()
.setIfAbsent(lockKey, traceId, LOCK_EXPIRY)
.defaultIfEmpty(false)
} else {
Mono.just(false)
}
}
the key still exists by checking TTL. and also tried to get value of the key before lock but at first get value but after some random period got empty. after searching found the redis cluster will response with MOVE
indicate that the key will move to another node if not implement code with cluster mode might cause incorrect behavior but my code is already connect Redis with cluster mode
Upvotes: 1
Views: 37