Giter Club home page Giter Club logo

redlock's Introduction

Redlock

This library is an implementation of Redlock (Redis destributed lock)

Redlock

Installation

If available in Hex, the package can be installed by adding redlock to your list of dependencies in mix.exs:

def deps do
  [
    {:redlock, "~> 1.0.10"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/redlock.

Usage

resource = "example_key:#{user_id}"
lock_exp_sec = 10

case Redlock.lock(resource, lock_exp_sec) do

  {:ok, mutex} ->
    # some other code which write and read on RDBMS, KVS or other storage
    # call unlock finally
    Redlock.unlock(resource, mutex)

  :error ->
    Logger.error "failed to lock resource. maybe redis connection got trouble."
    {:error, :system_error}

end

Or you can use transaction function

def my_function() do
  # do something, and return {:ok, :my_result} or {:error, :my_error}
end

def execute_with_lock() do

  resource = "example_key:#{user_id}"
  lock_exp_sec = 10

  case Redlock.transaction(resource, lock_exp_sec, &my_function/0) do

    {:ok, :my_result} ->
      Logger.info "this is the return-value of my_function/0"
      :ok

    {:error, :my_error} ->
      Logger.info "this is the return-value of my_function/0"
      :error

    {:error, :lock_failure} ->
      Logger.info "if locking has failed, Redlock returns this error"
      :error

  end
end

Setup

children = [
  # other workers/supervisors

  {Redlock, [pool_size: 2, ...]}
]
Supervisor.start_link(children, strategy: :one_for_one)

Options

Single Node Mode

readlock_opts = [

  pool_size:                  2,
  drift_factor:               0.01,
  max_retry:                  3,
  retry_interval_base:        300,
  retry_interval_max:         3_000,
  reconnection_interval_base: 500,
  reconnection_interval_max:  5_000,

  # you must set odd number of server
  servers: [
    [host: "redis1.example.com", port: 6379],
    [host: "redis2.example.com", port: 6379],
    [host: "redis3.example.com", port: 6379]
  ]

]
  • pool_size: pool_size of number of connection pool for each Redis master node, default is 2
  • drift_factor: number used for calculating validity for results, see https://redis.io/topics/distlock for more detail.
  • max_retry: how many times you want to retry if you failed to lock resource.
  • retry_interval_max: (milliseconds) used to decide how long you want to wait untill your next try after a lock-failure.
  • retry_interval_base: (milliseconds) used to decide how long you want to wait untill your next try after a lock-failure.
  • reconnection_interval_base: (milliseconds) used to decide how long you want to wait until your next try after a redis-disconnection
  • reconnection_interval_max: (milliseconds) used to decide how long you want to wait until your next try after a redis-disconnection
  • servers: host, port and auth settings for each redis-server. this amount must be odd. Auth can be omitted if no authentication is reaquired

How long you want to wait until your next try after a redis-disconnection or lock-failure

the interval(milliseconds) is decided by following calculation.

min(XXX_interval_max, (XXX_interval_base * (attempts_count ** 2)))

Cluster Mode

readlock_opts = [

  pool_size:                  2,
  drift_factor:               0.01,
  max_retry:                  3,
  retry_interval_base:        300,
  retry_interval_max:         3_000,
  reconnection_interval_base: 500,
  reconnection_interval_max:  5_000,

  cluster: [
    # first node
    [
      # you must set odd number of server
      [host: "redis1.example.com", port: 6379, auth: password],
      [host: "redis2.example.com", port: 6379, auth: password],
      [host: "redis3.example.com", port: 6379, auth: password]
    ],
    # second node
    [
      # you must set odd number of server
      [host: "redis4.example.com", port: 6379],
      [host: "redis5.example.com", port: 6379],
      [host: "redis6.example.com", port: 6379]
    ],
    # third node
    [
      # you must set odd number of server
      [host: "redis7.example.com", port: 6379],
      [host: "redis8.example.com", port: 6379],
      [host: "redis9.example.com", port: 6379]
    ]
  ]

]

Set cluster option instead of servers, then Redlock works as cluster mode. When you want to lock some resource, Redlock chooses a node depends on a resource key with consistent-hashing way (ketama algorithm using md5).

redlock's People

Contributors

geofflane avatar gmjosack avatar kalymero avatar lyokato avatar mihaildv avatar parallel588 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.