Distributed lock application scheme

    I've been studying locks recently, let's talk about this part of the understanding.

    First of all, when we use locks, we must be clear, why do we use locks? There are about three points:

    1. Is it only needed in a multitasking environment? This multitasking can refer to multithreading or multiprocessing.

    2. When all tasks need to write to the same shared resource. Concurrency safety issues arise when multithreading modifications to a shared resource.

    3. Access to resources is mutually exclusive.

    When a thread or process is operating on a shared resource, no other thread or process can operate on the resource, until the thread or process completes its operation, other threads or processes can operate on the resource, while other threads or processes can operate on the resource. Or the process is in a waiting state again.

There are currently three mainstream distributed lock schemes :

1. Implementation scheme using mysql

   Implementation idea: use the lock mechanism provided by the database itself, and require the database to support row-level locks

2. Implementation scheme using redis

  Implementation idea: use the cached CAS mechanism to ensure the atomicity of the cache operation sequence

3. Implementation scheme using zk

  zk-based node features and watch mechanism implementation

Today we mainly introduce the distributed lock based on redis.

Redis is an open source, in-memory data structure server. Can be used as a database. Caching and message queue brokers. It adopts a single-threaded single-process model and has powerful concurrency capabilities. It is the mainstream distributed architecture tool in the current Internet architecture.

Basic knowledge about redis distributed locks

  • cache validity period

    The data of redis is not necessarily persistent; given the lifetime set by the key, when the key expires, it will be automatically deleted.

  • SETNX command

    SETNX key value, set the value of the key to value if and only if the key does not exist. If the given key already exists, SETNX does nothing.

    SETNX is the abbreviation of [SET IF NOT EXISTS] if it does not exist, then SET

  • lua script

    A lightweight and compact scripting language that supports the atomicity of redis operation sequences;

Correct posture for redis plus unlock

  • lock

Write a random value to a specific key through setnx, and set the expiration time, write the value successfully and lock successfully

Note: A failure time must be set ------> to avoid deadlock

              When locking, each node generates a random string ------> avoid accidental deletion of locks

              Writing random values ​​and invalidation time must be at the same time ------> Ensure that the lock is atomic.

SET key value NX PX 30000

  • unlock

To match random values ​​and delete characteristic key data on redis, it is necessary to ensure that the three operations of obtaining data, judging consistency, and deleting data are atomic.

Execute the following lua script:

if  redis.call("get",KEY[1])==ARGV[1]then

    return redis.call("del",KEYS[1])

else

    return 0;

end

Guess you like

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