Problem statement:
Garden variety of distributed locking.
Implement a distributed locking for a service, which has multiple instances running.
The lock is to be taken on 'redis' running on 1 master and 2 slaves.
Implementation:
Service Instance 1
RLock lock11 = redisson.getLock("lock11");
RLock lock12 = redisson.getLock("lock12");
RLock lock13 = redisson.getLock("lock13");
RedissonRedLock lock = new RedissonRedLock(lock11, lock12, lock13);
lock.lock();
//...
lock.unlock();
Service Instance 2
RLock lock21 = redisson.getLock("lock21");
RLock lock22 = redisson.getLock("lock22");
RLock lock23 = redisson.getLock("lock23");
RedissonRedLock lock = new RedissonRedLock(lock21, lock22, lock23);
lock.lock();
//...
lock.unlock();
Expected behavior :
If service instance 1 takes the lock, then service instance 2 should NOT be able to take the lock. ( for Distributed locking - this should be the normal behaviour )
Question:
How would Redis know that the object "lock11" is to be created exclusive to the object "lock21" ?
Do we ( as developers ) need to program something for hash collision ?
Note : The two objects will have different hashCodes ( service instance 1 will NOT know the hashcode of the service instance 2's object )