Distributed Lock Service

JavaTimeoutLockingDistributed Lock

Java Problem Overview


Which distributed lock service would you use?

Requirements are:

  1. A mutual exclusion (lock) that can be seen from different processes/machines
  2. lock...release semantics
  3. Automatic lock release after a certain timeout - if lock holder dies, it will automatically be freed after X seconds
  4. Java implementation
  5. Nice to have: .Net implementation
  6. If it's free: Deadlock detection / mitigation
  7. Easy deployment, see note below.

I'm not interested in answers like "it can be done over a database", or "it can be done over JavaSpaces" - I know. I'm interested in a ready, out-of-the-box, proven implementation.

Java Solutions


Solution 1 - Java

A newer kid on the block is hazelcast. I've been playing with it and it is amazingly simple to use and configure.

As far as I can see there shouldn't be any conflict between Gigaspaces and hazelcast as hazelcast doesn't have any dependencies i.e. no jgroups.jar etc

Hazelcast:

  1. A mutual exclusion (lock), yep implementation of java.util.concurrency.locks.Lock
  2. Automatic lock release after a certain timeout, yep all locks are released if a member leaves the cluster
  3. Java implementation, yep
  4. Nice to have: .Net implementation, nope is a pure java solution, might be possible to port to j#
  5. If it's free: Deadlock detection / mitigation, nope no effort is made my Hazelcast to handle this
  6. Easy deployment, it's a single jar with a single config file, deployed as part of your application, no additional processes are required

Solution 2 - Java

Check out Apache's Zookeeper (A Hadoop sub-project) - it offers distributed synchronization. The documentation isn't great, but what there is makes it look an interesting product - checkout the recipes for ideas on how to use Zookeeper.

It is lower-level than you'd probably want and it does require additional deployment as it recommends dedicated servers.

You can model different locking strategies and it does offer a solution for a lock holder dying (ephemeral nodes).

Solution 3 - Java

Teracotta, including the Open Source edition, has distributed locking semantics by using either synchronized or the java.util.concurrent.ReentrantReadWriteLock - the latter apparently fitting your requirements.


Update

Since the question now added the requirement of 'mixing' with GigaSpaces, I'm going to say don't mix them. It's just going to add more complexity to your technological stack, and the effort of:

  • integrating, in terms of both code and infrastructure;
  • managing synchronisation between them;
  • learning/tuning/debugging Teracotta.

will be better spent creating or implementing a locking solution based on GigaSpaces.

Solution 4 - Java

I recommend to use Redisson it's a Redis based on In-Memory Data Grid. It implements familiar Java data structures including distributed java.util.Lock and java.util.concurrent.ReentrantReadWriteLock objects. Including ability to setup leaseTime. Lock usage example:

Redisson redisson = Redisson.create(config);

Lock lock = redisson.getLock("anyLock");
try {
   // unlock automatically after 10 seconds of hold
   lock.lock(10, TimeUnit.SECONDS);

} finally {
   lock.unlock();
}

...

redisson.shutdown();

Supports cloud vendors like Azure and AWS.

Solution 5 - Java

ZooKeeper became a de facto standard in distributed locking with the help of Apache Curator framework. Check out the locks in recipes for more information.

Solution 6 - Java

Oracle Coherence, which is very stable and mature, includes mutual exclusion support:

  cache.lock(key, -1);
    try {
      // .. add your critical code here
    } finally {
      cache.unlock(key);
    }

Locks survive server failures, rolling re-starts, etc.

For the sake of full disclosure, I work at Oracle. The opinions and views expressed in this post are my own, and do not necessarily reflect the opinions or views of my employer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionripper234View Question on Stackoverflow
Solution 1 - JavaGareth DavisView Answer on Stackoverflow
Solution 2 - JavaSteven DickView Answer on Stackoverflow
Solution 3 - JavaRobert MunteanuView Answer on Stackoverflow
Solution 4 - JavaNikita KoksharovView Answer on Stackoverflow
Solution 5 - JavafrailView Answer on Stackoverflow
Solution 6 - JavacpurdyView Answer on Stackoverflow