Three ways to implement distributed locks

1. Use scenarios of distributed locks

The same variable is shared between different processes. To ensure the correctness of the shared variable, a distributed lock is required.

Distributed systems need to meet: data consistency, availability, and partition fault tolerance .

In actual use, no distributed system can meet consistency, availability, and partition fault tolerance at the same time, and can only meet two of them at the same time. Usually, in most scenarios, we will sacrifice strong consistency in exchange for the high availability of the system . As long as the final consistency can be guaranteed, users can still accept it.

Second, what are the requirements for distributed locks

  1. In a distributed system environment, a method can only be executed by one thread of one machine at the same time;
  2. Need to have reentrant features;
  3. With lock failure mechanism;
  4. Prevent deadlock;

Three, three ways to achieve

  1. Realize distributed lock based on database
  2. Realize distributed lock based on cache (Redis, etc.)
  3. Implement distributed lock based on Zookeeper

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/112298930