RedlockManager API Reference¶
The RedlockManager is the main entry point for creating distributed locks and other synchronization primitives.
Creating a RedlockManager¶
Factory Methods¶
// Using Lettuce driver (recommended)
RedlockManager manager = RedlockManager.withLettuce(config);
// Using Jedis driver
RedlockManager manager = RedlockManager.withJedis(config);
Parameters:
config- ARedlockConfigurationinstance
Example:
RedlockConfiguration config = RedlockConfiguration.builder()
.addRedisNode("redis1.example.com", 6379)
.addRedisNode("redis2.example.com", 6379)
.addRedisNode("redis3.example.com", 6379)
.defaultLockTimeout(Duration.ofSeconds(30))
.build();
try (RedlockManager manager = RedlockManager.withLettuce(config)) {
// Use the manager
}
Lock Creation Methods¶
createLock()¶
Creates a standard distributed lock.
Parameters:
lockKey- Unique identifier for the lock
Returns: A Redlock instance implementing java.util.concurrent.locks.Lock
Example:
Redlock lock = manager.createLock("my-resource");
lock.lock();
try {
// Critical section
} finally {
lock.unlock();
}
createFairLock()¶
Creates a fair lock with FIFO ordering guarantees.
Performance Note
FairLock performs significantly better with polling wait strategy. Consider using .usePolling() in your configuration.
Example:
createMultiLock()¶
Creates a lock that atomically acquires multiple resources.
Example:
createReadWriteLock()¶
Creates a read-write lock allowing multiple readers or single writer.
Example:
RedlockReadWriteLock rwLock = manager.createReadWriteLock("shared-data");
rwLock.readLock().lock(); // Multiple readers allowed
rwLock.writeLock().lock(); // Exclusive writer
createSemaphore()¶
Creates a distributed semaphore with a fixed number of permits.
Example:
createCountDownLatch()¶
Creates a distributed countdown latch.
Example:
Async Lock Creation Methods¶
createAsyncLock()¶
Creates an asynchronous lock using CompletionStage.
createRxLock()¶
Creates a reactive lock using RxJava.
createAsyncRxLock()¶
Creates a lock supporting both async and reactive APIs.
Utility Methods¶
getConnectedNodeCount()¶
Returns the number of Redis nodes currently connected.
getQuorum()¶
Returns the quorum size required for lock acquisition.
getDriverType()¶
Returns the Redis driver type in use.
isHealthy()¶
Checks if the manager can reach a quorum of Redis nodes.
close()¶
Closes the manager and releases all resources.
Resource Management
Always use try-with-resources or explicitly call close() to prevent resource leaks.
Thread Safety¶
RedlockManager is thread-safe. A single instance can be shared across multiple threads.
Next Steps¶
- Lock Types - Detailed API for each lock type
- Async & Reactive - Async and reactive APIs
- Configuration - Configuration options