The 3 machines co-ordinate through a central lock manager
- Multiple Threads synchronise through
- Mutex & Semaphore
- Disk
- Remote Lock
Example: apt-get upgrade
cannot be run twice concurrently
To understand remote locks better, let’s synchronise multiple consumers over an unprotected remote queue
Queue is unprotected, we want one consumer to make call to the queue at a time (Not all consumers can read at a time)
A consumer can pick the item from the queue, update the database in order to lock, so when consumer x finishes the processing it can release the lock by updating the database
- Database Requirements
- A key-value store
- TTL
- Atomic Database
Redis would be the Database
- It is a KV Store
- It provides RedLock
- Also has a feature called TTL
Consumer’s pseudocode
- All consumers wait on ACQ_LOCK()
- While one of them READ_MSG()
Our requirements from the lock manager?
- Atomic Operations
- Automatic Expiration
(As we discussed in database requirements)
Function to acquire the lock
Function to release the lock
‘EVAL’ Executed atomically using LUA Script (Kind of like a procedure)
Redis is prone to SPoF in this architecture, let’s fix it
(MongoDB transaction uses remote locks on involved rows)
Distributed [Redlock] → Distributed locks with Redis
Distributed locks are mostly used in systems which need high degree of correction
Idea: 5 master nodes of redis, No replication, All independent
Acquire Lock
- Client goes throughout 5 nodes, trying to ACQ_LOCK with timeout
- If the lock is acquired on >50% then ACQUIRED
- Else release the lock on acquired instances and return FAILED
💡 We are only doing this is to prevent SPoF
Read this paper (Google still uses this in production)