DEV Community

Ashutosh Verma
Ashutosh Verma

Posted on

Are redisson locks jvm locks as well - HELP needed

I am using RLockReactive from redisson to get the redis distributed lock, then performing a Supplier operation.

The Supplier runs 2 steps in sequence, but I am seeing that while the lock is kept acquired on one key, the 2 steps in my input Supplier do not run sequentially.

I really am in doubt, if the redisson locks are ONLY DISTRIBUTED LOCKS, and not as well LOCKS IN A SINGLE JVM???

Here are my code snippets:

public Mono<Boolean> withLockReturnsBoolean(String lockKey, Supplier<Mono<Boolean>> supplier) {
    return Mono.defer(() ->
            RLockReactive lock = redisson.getLock(lockKey);
            return lock.lock()
                    .doOnSuccess(__ -> log.debug("Reactive lock acquired for: '{}'", lockKey))
                    .then(supplier.get())
                    .doFinally(signal -> lock.isLocked()
                            .flatMap(res -> {
                                if (res) {
                                    return lock.unlock()
                                            .doOnSuccess(__ -> log.debug("Reactive lock released for: '{}'", lockKey))
                                            .doOnError(e -> log.error("Exception occurred while releasing lock for: '{}', error = {}", lockKey, e.getMessage()));
                                }
                                return Mono.empty();
                            })
                    );
            );
}

    // CALLING HERE
    return withLockReturnsBoolean(
        lockKey,
        () -> {
            // 1. read from cache
            return budgetInvoker.validateBudget(promo, order)
                    .flatMap(isValid -> {
                        if (!isValid) return Mono.just(false);

                        // 2. update in cache
                        return budgetInvoker.cacheUpdate(discountDetail, order)
                                .thenReturn(true)
                                .onErrorReturn(false);
                    });
        });
Enter fullscreen mode Exit fullscreen mode

Top comments (0)