保证 memcached 锁
因此,我尝试仅在不存在契约的情况下使用 memcached 和 add() 的存储来实现分布式锁(Java 和间谍memcached,但当然适用于任何语言)。当然,如果一个实例消失,那么我们就会失去锁,因此我们的想法是添加锁 3 次(例如 MyLock1、MyLock2、MyLock3),这很可能会散列到 3 个不同的实例。
但是,我意识到,如果一个实例的哈希值下降,那么显然会发生变化(使用spymemcached的重新分配失败模式),因此当再次尝试添加()锁时,所有3个锁的哈希值可能不会改变匹配 memcached 集群中剩余的 2 个锁中的任何一个。
那么...使用 memcached 进行分布式锁还有其他想法吗?或者像我所说的那样基本上不可能实现有保证的锁定?
编辑:好的,所以在查看spymemcached源代码时,对于重新分发模式,它只是转到列表中的下一个活动memcached实例,而不是重新散列任何内容,所以它应该可以正常工作。
So, I'm trying to implement a distributed lock using memcached and add()'s store only if does not exist contract (Java & spymemcached, but applicable in any language of course). Of course, if an instance goes away, then we lose the lock so the thought was add the lock 3 times (e.g. MyLock1, MyLock2, MyLock3) which will very likely hash out to 3 different instances.
But, I've realized that if an instances goes down the hash then obviously changes (using spymemcached's Redistribute failure mode) and so it's likely that when another attempt is made to add() the locks, that the hashes of all 3 lock's will not match any of the 2 remaining locks in the memcached cluster.
So...any other ideas for distributed locks using memcached? Or is it basically impossible to do a guaranteed lock like what I'm referring to?
EDIT: Ok, so in looking through spymemcached source code, for Redistribute mode, it simply goes to the next active memcached instance in it's list, rather than re-hashing anything, so it should work OK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能,至少不能可靠。 memcached 不对数据保留做出任何保证 - 作为缓存,它可能会在任何时候丢弃数据而不发出警告。即使memcache实例看起来有可用空间,它也可能由于slab限制而不得不逐出数据。
如果您需要分布式锁定,则需要寻找其他地方——memcached 是不适合这项工作的工具。就其价值而言,MySQL 有锁: http://dev. mysql.com/doc/refman/5.1/en/miscellaneous-functions.html
You can't, at least not reliably. memcached doesn't make any guarantees about data retention -- as a cache, it may discard data at any point without warning. Even if the memcache instance appears to have space available, it may have to evict data due to slab constraints.
If you need distributed locking, you'll need to look elsewhere -- memcached is the wrong tool for the job. For what it's worth, MySQL has locks: http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html
如果您确实想使用 memcached 来避免在您的环境中引入更多内容/复杂性,那么请考虑一个非常小但专用的 memcached 配置,仅用于锁定。
但如果您愿意接受不依赖 memcached 的解决方案,那么我个人会使用 zookeeper 在java中实现分布式锁。我还会使用 Netflix curator utils 来简化此操作。
If you really want to use memcached to avoid introducing more stuff/complexity into your environment, then consider a very small but dedicated memcached config just for locking.
But if you're open to solutions that don't rely on memcached, then personally I'd use zookeeper to implement a distributed lock in java. I'd also use the Netflix curator utils to make this easier.
如果您使用 Java,我建议使用 Hazelcast(1.9+),它确实支持跨集群的分布式锁,并且很容易创建一个。
Hazelcast 保证如果持有锁的服务器出现故障,锁将会被释放。
http://hazelcast.com/docs/1.9.4/manual/single_html/ #Lock
此外,Hazelcast 公开了与 memcached 相同的合约,因此如果您需要从 JVM 访问它,您可以这样做(此示例演示了任何客户端都可以工作):
Java:
PHP:
1.9+ 的文档:
http://hazelcast.com/docs/1.9.4/manual/single_html/
希望有帮助。
If you're using Java, I recommend using Hazelcast (1.9+), it does support distributed locks across a Cluster, and is easy to create one.
Hazelcast guarantees that if a server that holds a lock goes down, the lock will be released.
http://hazelcast.com/docs/1.9.4/manual/single_html/#Lock
Also Hazelcast exposes the same contract as a memcached, so if you need to access it out of the JVM you can do it (this sample demonstrates that any client would work):
Java:
PHP:
Documentation of 1.9+:
http://hazelcast.com/docs/1.9.4/manual/single_html/
Hope it helps.