Distributed Locks
Taming the race-condition, like a boss
Widnyana
Race Condition
http://s.id/3cV
Race Condition (cont'd)
- Resource Sharing (data, hardware, and so on)
- Distributed Computing.
- Thread-based Processing.
- Asynchronous Brainless Computing.
Race Condition (cont'd)
- Duplication
- Deadlock
- Data loss.
- Dead.
- etc...
http://courses.cs.vt.edu/professionalism/Therac_25/Therac_1.html
So, how we should deal with it?
Mutual Exclusion
ensure there are no two concurrent processes are in their critical section at the same time.
Lock
Distributed Lock
Talk is cheap, dude!
Distributed Lock.
in the wild
...
while len(stack) < count and _ret <= _maxret:
result = __inner(user_id, count=count + 2)
for feed in result:
#: lock disini. proses kalau dapet lock.
#: delete locknya setelah task selesai ditandai oleh worker.
unlockkey = lock_byid(feed['id'], worker_id, ttl=1 * 60)
if unlockkey:
logger.debug_log("pid: {} | lock succeed: {}".format(worker_id, unlockkey))
permitted_pid = redis.get(unlockkey)
if worker_id == int(permitted_pid):
rsspost_setstatus("processed", 1, feed['id'], "upd")
logger.debug_log("pid: {} | setstatus succeed. {}".format(worker_id, unlockkey))
stack.append(feed)
#: ok, now we do the unlock things
unlock_byid(unlockkey, worker_id)
logger.debug_log("pid: {} | unlock succeed. {} \n\n".format(worker_id, unlockkey))
if len(stack) == count:
break
#: incr counter
_ret += 1
Distributed Lock.
in the wild (cont'd)
Distributed Lock.
Recipes
itu sudah, tidak perlu pakai blekmejik. iya, serius
Question?
Thank You.
Distributed Locks
By Widnyana Putra
Distributed Locks
taming race condition, with python and redis using home-backed Lock
- 991