Giter Site home page Giter Site logo

pombredanne / redlock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from glasslion/redlock

0.0 1.0 0.0 303 KB

Distributed locks with Redis and Python

Home Page: https://github.com/glasslion/redlock

License: MIT License

Makefile 12.79% Python 87.21%

redlock's Introduction

RedLock logo

RedLock - Distributed locks with Redis and Python

Build Status

This library implements the RedLock algorithm introduced by @antirez

Yet another ...

There are already a few redis based lock implementations in the Python world, e.g. retools, redis-lock.

However, these libraries can only work with single-master redis server. When the Redis master goes down, your application has to face a single point of failure . We can't rely on the master-slave replication, because Redis replication is asynchronous.

This is an obvious race condition with the master-slave replication model :

  1. Client A acquires the lock into the master.
  2. The master crashes before the write to the key is transmitted to the slave.
  3. The slave gets promoted to master.
  4. Client B acquires the lock to the same resource A already holds a lock for. SAFETY VIOLATION!

A quick introduction to the RedLock algorithm

To resolve this problem, the Redlock algorithm assume we have N Redis masters. These nodes are totally independent (no replications). In order to acquire the lock, the client will try to acquire the lock in all the N instances sequentially. If and only if the client was able to acquire the lock in the majority ((N+1)/2)of the instances, the lock is considered to be acquired.

The detailed description of the RedLock algorithm can be found in the Redis documentation: Distributed locks with Redis.

APIs

The redlock.RedLock class shares a similar API with the threading.Lock class in the Python Standard Library.

Basic Usage

from redlock import RedLock
# By default, if no redis connection details are 
# provided, RedLock uses redis://127.0.0.1:6379/0
lock =  RedLock("distributed_lock")
lock.acquire()
do_something()
lock.release()

With Statement / Context Manager

As with threading.Lock, redlock.RedLock objects are context managers thus support the With Statement. This way is more pythonic and recommended.

from redlock import RedLock
with RedLock("distributed_lock"):
    do_something()

Specify multiple Redis nodes

from redlock import RedLock
with RedLock("distributed_lock", 
              connection_details=[
                {host='xxx.xxx.xxx.xxx', port=6379, db=0},
                {host='xxx.xxx.xxx.xxx', port=6379, db=0},
                {host='xxx.xxx.xxx.xxx', port=6379, db=0},
                {host='xxx.xxx.xxx.xxx', port=6379, db=0},
              ]
            ):
    do_something()

The connection_details parameter expects a list of keyword arguments for initializing Redis clients. Other acceptable Redis client arguments can be found on the redis-py doc.

Reuse Redis clients with the RedLockFactory

Usually the connection details of the Redis nodes are fixed. RedLockFactory can help reuse them, create multiple RedLocks but only initialize the clients once.

from redlock import RedLockFactory
factory = RedLockFactory(
    connection_details=[
        {host='xxx.xxx.xxx.xxx'},
        {host='xxx.xxx.xxx.xxx'},
        {host='xxx.xxx.xxx.xxx'},
        {host='xxx.xxx.xxx.xxx'},
    ])

with factory.create_lock("distributed_lock"):
    do_something()

with factory.create_lock("another_lock"):
    do_something()

redlock's People

Contributors

glasslion avatar lorien avatar mjschultz 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.