CP model based

CP model

Database

Ideas

  • Use database locks

    • Table lock

    • Unique index

  • "SELECT ... For UPDATE" adds a row lock on record

    • e.g. SELECT * FROM distributed_lock WHERE business_code='demo' FOR UPDATE

Pros and Cons

  • Pros:

    • Easy to build

  • Cons:

    • Big pressure on database if there are high number of concurrent requests. Recommend to separate the business logic DB and lock DB

Example

Zookeeper

Distributed lock
  • How will the node be deleted:

    • Client deletes the node proactively

      • How will the previous node get changed?

        1. Watch mechanism get -w /gupao.

    • Too many notifications:

      • Each node only needs to monitor the previous node

Curator

  • Motivation: Curator encapsulates the one-time watch logic so easier to use.

    • There are three methods which could set watcher: GetData(); getChildren(); exists().

    • Whenever there is a change to the watched data, the result will be returned to client.

    • However, the watcher could be used only once.

Implementation

etcd

Operations

  1. business logic layer apply for lock by providing (key, ttl)

  2. etcd will generate uuid, and write (key, uuid, ttl) into etcd

  3. etcd will check whether the key already exist. If no, then write it inside.

  4. After getting the lock, the heartbeat thread starts and heartbeat duration is ttl/3. It will compare and swap uuid to refresh lock

Last updated

Was this helpful?