py-sherlock / sherlock

Easy distributed locks for Python with a choice of backends.
MIT License
374 stars 35 forks source link

Multi Master setup #30

Open Cediddi opened 6 years ago

Cediddi commented 6 years ago

Hi, I want to use sherlock in a distributed system. I'd like to have multi master setup because if lock server (memcached, redis, etcd) goes down, all system goes down. How can we solve that?

vaidik commented 5 years ago

Hi @Cediddi,

This is very much possible with sherlock even now.

If you are using Redis, you can't do Redis clustering because sherlock uses redis-py and redis-py does not support clustering. However it supports sentinel. You can read this article which explains how to setup Redis Sentinel and this article to understand how to use redis-py to discover masters and slaves using sentinel. Since Sherlock uses redis-py internally, it would support using sentinel as well. This example should work (although I haven't tested it myself):

import redis
import sherlock

from redis.sentinel import Sentinel

sentinel = Sentinel([('X.X.X.X', 26379)], socket_timeout=0.1)
master_client = sentinel.master_for('mymaster', socket_timeout=0.1)

sherlock.configure(client=master_client)

lock = sherlock.Lock('my_lock')
lock.acquire()

Etcd also has a cluster guide to help you achieve the same. In a very similar way, you can achieve this with etcd as well if you have etcd in a cluster mode.

import etcd
import sherlock

etcd_client = etcd.Client(host=(('X.X.X.A', 4001), ('X.X.X.B', 4001), ('X.X.X.C', 4001)))

sherlock.configure(client=etcd_client)

lock = sherlock.Lock('my_lock')
lock.acquire()
vaidik commented 5 years ago

Memcached doesn't have a very straight forward way to achieve reliability like Redis and Etcd. You can read more about how Memcached works and what most people do here and here.

My recommendation would be to stick with either Redis or Etcd. But I don't know your requirements well enough. If you could share your use-case, I might be in a better position to comment.