Distributed locks can actually be understood as: controlling the distributed system to operate shared resources in an orderly manner, and maintaining consistency through mutual exclusion.
Give an inappropriate example: (Recommended learning: Redis video tutorial)
Assumption The shared resource is a house with various books in it. The distributed system is the people who want to enter the house to read. The distributed lock ensures that the house has only one door and only one person can enter at a time, and the door has only one key. Then many people want to read a book, okay, line up, the first person takes the key, opens the door, goes in to read, and locks the door. Then the second person doesn’t have the key, so just wait, wait for the first one to come out, and then you get it Enter with the key, and so on
Implementation principle
Mutual exclusivity
Ensure that there is only one customer at the same time The client can get the lock, that is, it can operate on the shared resources
Security
Only the locked service can have unlocking permission, that is, a cannot be allowed to add Locks and bcd can be unlocked. If they can be unlocked, then distributed locks are meaningless.
The possible situation is that a queries and finds that the lock is held, and is preparing to unlock. At this time, a suddenly holds Some locks have expired, and then b gets the lock, because a's lock has expired, and b gets the lock. At this time, a continues to execute the second step to unlock. If no verification is performed, the lock held by b will be deleted.
Avoid deadlock
If a deadlock occurs, any subsequent services will not be able to obtain the lock, and no more operations can be performed on the shared resources
Ensure that locking and unlocking operations are atomic operations
This is actually a problem of implementing distributed locks. Suppose a uses redis to implement distributed locks
Assume adding Lock operation, the operation steps are divided into two steps:
1. Set key set (key, value) 2. Set expiration time for key
Assume that the program crashes after a has just implemented set. This leads to a deadlock that occurs because the key does not set an expiration time.
How to implement distributed locks
There are many ways to implement distributed locks , as long as the above conditions are met, distributed locks can be implemented, such as database, redis, zookeeper. Here I will first talk about how to use redis to implement distributed locks
The key to implementing distributed locks is In addition to the distributed application server, build a storage server to store lock information. At this time, we can easily think of Redis. First we need to build a Redis server and use the Redis server to store lock information.
Use redis to implement distributed lock
Use redis command set key value NX EX max-lock-time to implement locking
Use Redis command EVAL realizes unlocking
Several key points to pay attention to when implementing:
1. The lock information must expire and time out, and a thread cannot be allowed to Holding a lock for a long time may lead to deadlock;
2. Only one thread can acquire the lock at the same time.
Several redis commands to be used:
setnx(key, value): "set if not exits", if the key-value does not exist, then Successfully added to the cache and returns 1, otherwise returns 0.
get(key): Get the value corresponding to the key, if it does not exist, return nil.
getset(key, value): First get the value corresponding to key, return nil if it does not exist, and then update the old value to the new value.
expire(key, seconds): Set the validity period of key-value to seconds.
For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!
The above is the detailed content of What is redis distributed lock. For more information, please follow other related articles on the PHP Chinese website!