Member-only story
Redis as a Lock! Are You Sure?
Is the lock you are talking about an exclusive lock or a barrier?
Redis is a storage that keeps data in memory, and we know anything in memory is unreliable. Furthermore, Redis’ data persistence is not as reliable as claimed. Therefore, it is very dangerous to use Redis as a lock to achieve some sort of synchronization.
This article, however, is not intended to focus only on Redis’ unreliability, but rather to further analyze whether those locks are being used correctly.
In fact, we often refer to two very different concepts as locks.
- Exclusive lock: Used to control the access rights of a critical section, where only one person can be in the critical section at a time. A similar concept is the semaphore, which allows the maximum number of people in the critical section.
- Barrier: Used to reduce the frequency of a specific action.
Nevertheless, these two concepts have completely different purposes and are implemented in totally different ways. However, usually, we do not carefully define the requirements and choose the correct approach, resulting in an abnormality.
Exclusive Lock
The purpose of an exclusive lock, also known as a mutex lock, is to control the…