60

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
ripper234
  • 222,824
  • 274
  • 634
  • 905

6 Answers6

24

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
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • Any deployment effort? I'll refine my question - solutions that do not need further deployment and can use either MySql, JavaSpaces/GigaSpaces or NetApp are (highly) preferable. – ripper234 Jun 30 '09 at 08:34
  • 1
    I haven't actually deployed Hazelcast yet, but I'm actively looking at using it for distributed locking. Deployment seems to be almost trival as there is no external process, all the JVM's in the cluster just kinda work out what todo – Gareth Davis Jun 30 '09 at 09:30
  • Seems suspicious. They at least need to know about one another's existence somehow... Anyway, I prefer (and insist on) a solution based on existing technologies we're employing - I don't think it's wise introducing yet another clustering framework when we already work with GigaSpaces. Is it possible that no MySql-based solution exists??? – ripper234 Jul 01 '09 at 20:25
  • 3
    Yes, hazelcast uses mutlicast to auto discover it's members, hardwiring the cluster is also possible if mutlicast isn't suitable. If you want to lock stuff using MySql that is really easy isn't it? just do 'SELECT ID FROM LOCK_TABLE WHERE ID = `sharedname` FOR UPDATE' – Gareth Davis Jul 01 '09 at 21:13
  • For #2 it seems that it doesn't fully fulfill what someone may want. If you have multiple worker threads on one machine and one of them goes rogue or gets stuck without returning the lock, you probably still want to expire that lock after a certain time. You may not get a full server failure, just a thread failure before the lock is released. Is there anyway around that issue? – Scott Mar 31 '14 at 15:46
14

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).

  • 4
    AFAIU, ZooKeeper writes to disk for each lock acquire. It's too expensive. – stepancheg Dec 20 '10 at 10:17
  • 5
    The wirte to the write-ahead log is for recoverability. And since the lock service is distributed, lock acquire will incur network travel if the lock is not cached. Compared to network travel, a append to disk is not very expensive. Zookeepr is used in many distributed systems such as HBase. Its performance is good. – Jingguo Yao Feb 26 '13 at 05:33
  • Zookeeper is low-level but there is https://curator.apache.org/getting-started.html which solves the issue. – Adrian Dec 04 '18 at 12:00
12

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.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
8

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.

Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
  • Does this cover all the use cases the question had? I took a look through and didn't see an auto expiration timeout feature on the lock. – Scott Mar 31 '14 at 14:07
  • 2
    @Scott i fixed my answer, new ability to setup lock release timeout has added in Redisson 1.1.4 version – Nikita Koksharov Jul 15 '14 at 18:11
2

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.

raindev
  • 1,017
  • 1
  • 12
  • 25
frail
  • 4,123
  • 2
  • 30
  • 38
  • 2
    Though I don't disagree with you I still want to know related to "ZooKeeper became a de facto" how did you measured it? – Adrian Dec 04 '18 at 12:07
-2

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.

spandey
  • 1,034
  • 1
  • 15
  • 30
cpurdy
  • 1,177
  • 5
  • 12
  • I don't understand your statement, partially because it is not syntactically correct, and partially because it appears to be an invalid claim. I am no longer with Oracle, but Oracle Coherence is a distributed system first released in 2001, that has had distributed locking support since it's 1.0 release in 2001. The software has been used to build distributed systems by companies such as Amazon and most other online retailers, the NY Stock Exchange and most other stock exchanges, all of the major banks in the world, most of the airlines, and so on. – cpurdy Apr 30 '19 at 20:31
  • can you share some link where i can explore this? – spandey Jun 20 '19 at 04:48
  • It's an Oracle product: https://twitter.com/taotetek/status/1142031107640188928 ... what that means is that it may be difficult to use unless you're already an Oracle customer, because Oracle mostly works with big companies and sells them giant bundles of who-knows-what (called "enterprise license agreements"). But you can easily download an eval copy of Coherence: https://www.oracle.com/technetwork/middleware/coherence/downloads/index.html ... and doc https://docs.oracle.com/middleware/12213/coherence/index.html – cpurdy Jun 21 '19 at 16:22