r/redis • u/tihomir-mateev • 1d ago
News A vendor-agnostic distributed lock for Java — feedback welcome
Sharing redlock4j, a Java take on distributed locking, including multi-node consensus locking as described by the Redlock algorithm. It's plain Redis, so it runs anything RESP-compatible. No modules, no managed service. A simple distributed lock just implements the JDK Lock interface:
// full multi-node Redlock quorum, or single node — same code
Lock lock = redlockManager.createLock("orders:reindex");
lock.lock();
try {
reindex();
} finally {
lock.unlock();
}
Son interesting features
- RESP3 push notifications so one connection carries both commands and keyspace-notification pub/sub — waiters wake on lock release in single-digit ms instead of polling (Falls back to polling where CONFIG/keyspace events are locked down).
- Auto-detects native CAS/CAD (DELEX ... IFEQ, 8.4+) for safe release/extend, with a Lua GET==token then DEL fallback on older servers.
- Multi-node quorum acquire/release fans out in parallel (CompletableFuture), so 3 nodes cost ~max(RTT) instead of 3 × RTT.
- see the guide for more information
Benchmarks (mostly vibe-coded so take them with a grain of salt)
(Testcontainers, 3-node Redis 7 cluster, 50 ms work per critical section, 60 s measurement, vs Redisson)
Throughput (ops/s, higher is better):
| Primitive | Redisson | redlock4j-3node |
|---|---|---|
| MultiLock | 17.05 | 17.72 |
| ReadWriteLock (writer) | 0.21 | 16.42 |
| Semaphore | 54.71 | 87.50 (91 single-node) |
| CountDownLatch | 59.02 | 59.91 |
p99 latency (ms, lower is better):
| Primitive | Redisson | redlock4j-3node |
|---|---|---|
| ReadWriteLock (writer) | 16820.7 | 104.2 |
| Semaphore | 386.5 | 4.20 |
| DistributedLock | 1286.7 | 858.0 |
| CountDownLatch | 22.6 | 19.9 |
The RW-writer gap is the interesting one - Redisson's writer path seems to starve hard under reader load, redlock4j stays ~100 ms at p99.
Where it's still behind, honestly: plain 3-node DistributedLock throughput (0.81 vs Redisson's 18.24) and 3-node FairLock — both bottlenecked on the polling floor. Fixing that (a pub/sub-on-release wait strategy) is the next big lever.
This is a personal project, actively maintained, licensed under MIT, Java 8+, on Maven Central.
Genuinely after feedback on the direction and where the multi-node correctness cost is worth paying.
