Better Programming

Advice for programmers.

Follow publication

Member-only story

Redis as a Lock! Are You Sure?

Chunting Wu
Better Programming
Published in
4 min readMay 2, 2022

--

Photo by Florian Olivo on Unsplash

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…

--

--

Chunting Wu
Chunting Wu

Written by Chunting Wu

Architect at SHOPLINE. Experienced in system design, backend development, and data engineering.

Write a response