Redis distributed lock

distributed concept

Because of the evolution of the architecture, there is a concept of a single variable cluster. The corresponding data storage has also changed from one location to multiple locations, that is, distributed storage. Dispersion ability

The resource capabilities of the same business are decentralized, highly available, and highly utilized.
The domain capabilities of business A can be dispersed into A1, A2, A3, A4, and 4 sets of resources. If A1 has problems, A2, A3, and A4 can all work, and the service integration capability is tripled.

lock concept

Since high availability and capabilities are scattered across multiple servers, what is the purpose of locks? You must know how powerful business processing capabilities are. There is only one place for data core processing, which is database resources. This is a shared area, and locks are In order to ensure safety in the case of high availability.

The lock is the lock shared content The
lock method is the shared memory block
Monitor.Enter
Monitor.Exit
In fact, the shared header in the memory is converted between 0 and 1 to realize the identification of a lock

Why Distributed Locks

Because the current business is complex, the structure is huge, and there are multiple clusters,
but distributed is multiple machines and multiple processes. The lock is only for one's own resources,
so it is necessary to find a cross-machine resource as the lock, and identify the lock through a shared file. condition.

So cross-machine database resources, redis es mongodb mysql suffices

If you use database resources as locks, there will be IO read and write performance, so it is best to find in-memory database resources,
so choose redis

In this way, multiple machines send requests to redis, even if multiple machines are multi-threaded, but after redis is here, it is controlled by a single thread

How to manage so many distribution requests.

1. Acquire lock
2. Set time
3. Release

1. SETNX
SETNX key val: If and only if the key does not exist, set a string whose key is val and return 1; if the key exists, do nothing and return 0.
2. expire
expire key timeout: Set a timeout for the key, in seconds, after which the lock will be automatically released to avoid deadlock.
3. delete
delete key: delete key
When using Redis to implement distributed locks, these three commands are mainly used

The process is actually like this,
but it should be noted that the way of acquiring locks is blocking and non-blocking
. At the same time, the basis for grabbing is to look at the version number in the transaction to determine whether the
LUA script operates atomically.

Setting the time
1. It is to prevent unexpected situations from not releasing the lock all the time and causing deadlock
2. It is afraid that it will be released before the business operation is completed.

Release lock
LUA script atomic operation

Who locks who unlocks

reentrant

If redisson
can avoid using distributed or avoid it, after all, it has a performance relationship.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324062014&siteId=291194637