32
DISTRIBUTED LOCKS WITH REDIS AND PYTHON Prepared by Sebastian Buczyński

Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED LOCKSWITH REDIS AND PYTHON

Prepared by Sebastian Buczyński

Page 2: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

WHO AM ISoftware developer

Using mainly gevent, Twisted & Celery.

Working for Focus Telecom Poland

Page 3: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

WHAT WE DO @ FOCUSB2B telco solutions in Software as Service model

teleconferences and pbx, but our main product is aContact Center.

Page 4: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

CONTACT CENTER?

A platform for building call centers, hotlines or helpdesks.

Page 5: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

GOALS OF THIS TALKImplementation of distributed locks using Redis and Python

Use in a real life application

Page 6: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

NoSQL database - key -> value storagekey ∈ {string}

value ∈ {string, list, map, set, ...}+ message passing

redis.io

Page 7: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

LOCK

Software primitive that allows to exclusively access aresource in a way, that nobody else can use it

Page 8: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

CASE STUDY - CONTACT CENTER

Page 9: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

CONTACT CENTER IS ...

Work automation

Page 10: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

CONTACT CENTER IS ...

 

Page 11: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

CONTACT CENTER IS LIKE A COMMON

TASK QUEUE

Page 12: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

NOPE, IT'S NOT

Worker is not passiveTasks are prioritizedWorker can handle multiple tasks simultaneously

Page 13: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

SOLUTION - THE BIG PICTUREEvent-driven task manager - Twisted basedRedis - locks server

Page 14: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED MUTEX/BINARY SEMAPHOREEither locked or unlockedStored as a Redis stringNon - blocking calls

Page 15: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED MUTEX - USE CASE 1Keeping state - one lock for each workerLocked while working on a task, released afterwards

Page 16: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED MUTEX - USE CASE 2Choosing task on one's ownDiscontinue work

Page 17: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

Stored in Redis as a string

IMPLEMENTATION DETAILS

Need for atomicity

Page 18: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

IMPLEMENTATION DETAILS 2GET lock_keyif lock_key == 'unlocked': SET lock_key 'locked'

Page 19: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

IMPLEMENTATION DETAILS 2WATCH lock_keyGET lock_keyif lock_key == 'unlocked': SET lock_key 'locked'else: UNWATCH

Page 20: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

IMPLEMENTATION DETAILS 2WATCH lock_keyGET lock_keyif lock_key == 'unlocked': MULTI SET lock_key 'locked' EXECelse: UNWATCH

Page 21: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

PYTHON PART - TASK MANAGERShould be notified

PUBLISH locks_changes_channel lock_key_locked

To receive notifications

SUBSCRIBE locks_changes_channel

Page 22: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

PYTHON PART - TWISTED POWEREDfrom txredis.client import RedisSubscriberclass LockSubscriber(RedisSubscriber): def messageReceived(self, channel, message) # do some stuff

Page 23: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED SEMAPHOREthreading.Semaphorecan be acquired/released few times

Stored as a Redis listBlocking calls

s = Semaphore(2)s.acquire()s.acquire()# s.acquire() # Exceptions.release()s.release()# s.release() # that too

Page 24: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

DISTRIBUTED SEMAPHORE - USE CASEMultiple tasks at the same time

Page 25: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

Stored in Redis as a list

IMPLEMENTATION DETAILS

Page 26: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

IMPLEMENTATION DETAILS 2

Acquiring

BRPOP semaphore_key some_timeout

Releasing

RPUSH semaphore_key some_val

Page 27: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

PYTHON PART - WITHOUT CHANGES...but personally never needed notifications on these.

Page 28: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

SEMAPHORE'S STATE AFTER CHANGING ITApproach one - wrap with MULTI - EXEC

Simpler in this case - write a lua script

redis.call('RPUSH', 'semaphore_key', 'some_val'local count = redis.call('LLEN', 'semaphore_key'return count

Evaluate this:

redis-cli EVAL "$(cat semaphore_release.lua)"

Page 29: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

WARNING!Making BRPOP inside MULTI/EXEC will return nil

BRPOP inside lua script will result in an error

Page 30: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

ALTERNATIVE?Non blocking - RPOP

Page 31: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

FINAL REMARKSCare about starting conditions

Study carefully control flow in your application

Work up a restoring state procedure

Page 32: Distributed locks with Redis and Python · DISTRIBUTED LOCKS WITH REDIS AND PYTHON ... WHAT WE DO @ FOCUS B2B telco solutions in Software as Service model teleconferences and pbx,

@EnforcerPL

QUESTIONS?