Why is etcd based distributed lock more secure than redis lock

Time:2022-5-3

Redis implemented distributed lock

#Lock the resource key. When the key does not exist, it is created and set. It expires automatically in 10 seconds
SET key value EX 10 NX
#Delete key
DEL key

Role of nx

The NX parameter is used to ensure that only one client can write the secondary key successfully and obtain the lock when the distributed lock does not exist.

The first core element of distributed locks is mutual exclusion and security. Multiple clients are not allowed to obtain locks at the same time

The automatic expiration time of key is not set

The second core element of distributed lock is activity. In the process of implementing distributed locks, it should be considered that the client may crash or network partition, and it is necessary to apply for distributed locks and set the automatic expiration time of locks,
Automatically release the lock through expiration, timeout and other wit to avoid deadlock and business interruption.

When the expiration time is set, there is still oversold

Redis distributed lock is blocked. After the request is executed, the atomicity of redis comparison inventory and fastener inventory operations can not be guaranteed. You can use Lua script to realize the atomicity of redis comparison inventory and fastener inventory operations.

Why do you like using redis as a distributed lock

Redis’s core advantages are fast, simple and easy to deploy.

Redis distributed lock problem

Problems with redis distributed locks:

  • 1. There is a single point of failure problem. The official solution is redlock algorithm.
  • 2. After the lock acquisition fails, only exceptions can be thrown, and the thread cannot be blocked. Redisson open source framework solves these problems.

Scheme of distributed lock creation

Zookeeper is a typical distributed metadata storage service. Its distributed lock implementation is based on the temporary node and sequence characteristics of zookeeper.

The temporary node has the function of automatic data deletion. When the connection and session between the client and zookeeper are broken, the corresponding temporary node will be deleted.

Zookeeper also provides a watch feature to monitor the data changes of keys.

Etcd distributed lock implementation

Transaction and lock security

The transaction characteristics of etcd are composed of if statement, else statement and then statement. The if statement supports the comparison of key and the modified version number mod_ Version and create version number create_ version.

You can create through the creation version number of the key_ Check if the version key does not exist_ Revision version number is 0.

Lease and lock activity

Lease is an activity detection mechanism that provides the ability to detect the survival of each client.
Through the lease mechanism, we can gracefully solve the deadlock problems in various failure scenarios, such as the crash failure of the client and the isolation between the client and the etcd cluster network. If it exceeds the lease TTL, it will be released automatically.

Watch and lock availability

Watch provides efficient data monitoring capability. When the client receives the watch delete event, it can quickly judge whether it is qualified to obtain the lock, which greatly reduces the unavailable time of the lock.

reference

Redis Lua script implements distributed lock
Redis + Lua distributed lock and current limit
Redis distributed lock – setnx + Lua script implementation

Recommended Today

ffmpeg pushes video with RTP protocol

docker build environment docker pull abdulachik/ffmpeg.js:latest docker run -it -p 8080:8080 -v /Users/workspace/Downloads/ffmpeg_wasm:/tmp –privileged=true abdulachik/ffmpeg.js:latest /bin/bash cd /tmp Process overview camera->ffmpeg->encode to h264 video->pass to h264 decoder->play(You can also push the video directly to RTMP protocol after encoding, and the local decoding and playback is only for testing) ffmpeg encodes video stream (from camera) to […]