分布式锁

分布式锁

1、分布式系统简介

为了规范分布式系统中各个系统的统一,CAP理论应运而生,维基百科原文如下:

In theoretical computer science, the CAP theorem, also named Brewer’s theorem after computer scientist Eric Brewer, states that any distributed data store can only provide two of the following three guarantees:[1][2][3]

  • Consistency

    Every read receives the most recent write or an error.

  • Availability

    Every request receives a (non-error) response, without the guarantee that it contains the most recent write.

  • Partition tolerance

    The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

When a network partition failure happens, it must be decided whether to

  • cancel the operation and thus decrease the availability but ensure consistency or to
  • proceed with the operation and thus provide availability but risk inconsistency.

Thus, if there is a network partition, one has to choose between consistency and availability. Note that consistency as defined in the CAP theorem is quite different from the consistency guaranteed in ACID database transactions.[4]

Eric Brewer argues that the often-used “two out of three” concept can be somewhat misleading because system designers only need to sacrifice consistency or availability in the presence of partitions, but that in many systems partitions are rare.

解释如下:

1、一致性

2、可用性

3、分区容忍性

2、分布式锁

常见的分布式锁如下:

  1. 数据库锁:乐观锁和悲观锁
  2. zookeeper锁
  3. redis锁
    1. redis+lua
    2. redlock
    3. redisson

引用资料:

  1. 维基百科
  2. 一文看懂|分布式系统之CAP理论