What is a Java distributed lock?

Distributed systems are highly focused on efficiency, speed, and performance. When building a distributed system in the Java programming language, it's a good idea to use Java distributed locks that can stop different machines from stepping on the others' toes. But what is a Java distributed lock exactly, and how do Java distributed locks work?

What are Java distributed locks?

In computer science, locks are mechanisms in a multithreaded environment to prevent different threads from operating on the same resource. When using locking, a resource is "locked" for access by a specific thread, and can only be accessed by a different thread once the resource has been released. Locks have several benefits: they stop two threads from doing the same work, and they prevent errors and data corruption when two threads try to use the same resource simultaneously.

Distributed locks in Java are locks that can work with not only multiple threads running on the same machine, but also threads running on clients on different machines in a distributed system. The threads on these separate machines must communicate and coordinate to make sure that none of them try to access a resource that has been locked up by another.

How do Java distributed locks work?

As you might imagine, implementing and using distributed locking in Java (or any other programming language) is more complicated than locking on a single machine. Distributed locks need to decide whether to use an optimistic or a pessimistic locking policy:

  • Optimistic locking policies assume that the resource being read will not change while reading it, and therefore do not lock the resource during read operations.
  • Pessimistic locking policies assume that the resource being read will change while reading it, and therefore lock the resource during read operations.

Java includes several different lock interfaces that can be used for distributed locking. The list of distributed locks in Java includes:

  • Lock: The Lock interface is the most basic implementation of distributed locking in Java. This interface is only implemented by the ReentrantLock class, which as its name suggests is a reentrant lock (i.e. it allows readers and writers to lock the same resource multiple times).
  • Fair lock: When you implement a distributed lock in Java (or another programming language), you need to decide how you will assign locks if you have multiple threads waiting for it. Fair locks use a fairness policy in which access is first given to the thread that has been waiting the longest. You can implement a fair lock in Java by setting the optional fairness parameter when creating a ReentrantLock object.
  • Read-write lock: A read-write lock is a lock that allows multiple threads to read from the same resource simultaneously, but only one thread to write to it. The ReadWriteLock class in Java uses a pair of associated locks, one for readers and one for writers.

These Java distributed locks are available in the java.util.concurrent.locks package. In addition to these three locks, there are a couple more distributed locks that you should know about:

  • Redlock: The Redlock algorithm provides fault-tolerant distributed locking built on top of Redis, an open-source, in-memory data structure store used for NoSQL key-value databases, caches, and message brokers.
  • Multi-lock: In some cases, you may want to manage several distributed locks as a single "multi-lock" entity.

Java distributed locks in Redis

. The good news is that distributed locking in Redis is possible, as you can read in the linked guide. The bad news, however, is that it's much more complex than simply creating an instance of a Lock class, as you can do with the Java standard library.

To simplify the development process, many Java developers using Redis choose to install a third-party Redis Java client such as Redisson. Redisson offers dozens of implementations of various Java collections, objects, and constructs, including Java distributed locks. The Java distributed locks that Redisson implements include:

In addition, Redisson implements the following distributed locking mechanisms:

Below is a simple code example demonstrating just how easy it is to use Java distributed locks in Redis with Redisson:

RLock lock = redisson.getLock("anyLock");
lock.lock();
try {
 ...
} finally {
 lock.unlock();
}
Similar terms