Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Redis need locking? [closed]

Tags:

redis

valkey

I read that Redis is single threaded and all commands are atomic. But there are also references talk about locks in Redis, e.g. https://redis.io/topics/distlock

It is not clear to me why Redis still need locks if it is single threaded. Can someone please explain?

like image 569
feeltheheat Avatar asked Jul 15 '26 02:07

feeltheheat


1 Answers

No, Redis doesn't need locking.

Yes, Redis is single-threaded. The locks we are talking about here are not for Redis, but using Redis for a Distributed Lock.

The purpose of a distributed lock is to ensure that among several nodes that might try to do the same piece of work, only one actually does it (at least only one at a time). That work might be to write some data to a shared storage system, to perform some computation, to call some external API, or suchlike.

From: How to do distributed locking

These several nodes are workloads you have on other servers. You are using Redis kind-of as a RAM-shared-memory to implement the lock.

Redis is an excellent choice for a distributed lock because it gives you sub-millisecond latency as it is an in-memory database. For an example of such a lock implemented with Redis see python-redis-lock.

like image 149
LeoMurillo Avatar answered Jul 19 '26 23:07

LeoMurillo