October 08
Today I’m exploring the source code of https://github.com/redis/redis-py/tree/master (Redis Py) for a use case about Locks. While doing that I came across time.monotonic
and when I dig deeper into that topic I found that usual time.time()
could result in weird bugs for time calculation operations when system time changes. I wrote a TIL (https://github.com/Maheshkumar-novice/TIL/tree/main/python).
Regarding redis locks, I’ll add TIL soon. This is what I understood while learning about redis locks. Redis is single-threaded, which means each command execution is sequential. Parallel commands will be stored in a queue and executed one by one.
We can have backup mechanism in Redis. By default it uses RDB
snapshots. There is another one called AOL (append only log)
which stores the write operations I guess so all values can be replayed back after a crash.
In single node environment, using setnx
is fine for locking. But when it comes to master-slave
there could be a possibilitiy of some issues. Such as master-slave copy goes out of sync and master goes down. In that case slave will be promoted to master but it won’t contain few keys.
They have an algorithm called redlock
for this use case. But I’m still exploring the possibilities and my use cases.