如何在服务器群中实现锁定?

发布于 2024-07-17 23:53:14 字数 290 浏览 7 评论 0原文

是否有众所周知的跨服务器场同步任务的最佳实践? 例如,如果我有一个在服务器场上运行的基于论坛的网站,并且有两个主持人尝试执行某些操作,需要写入数据库中的多个表,并且这些主持人的请求由服务器中的不同服务器处理农场,如何实现一些锁定功能以确保他们无法同时对同一项目执行该操作?

到目前为止,我正在考虑使用数据库中的表进行同步,例如检查表中项目的ID,如果不存在则插入并继续,否则返回。 也可能可以使用共享缓存,但我目前没有使用它。

还有其他办法吗?

顺便说一下,我使用 MySQL 作为我的数据库后端。

Are there well-known best practices for synchronizing tasks across a server farm? For example if I have a forum based website running on a server farm, and there are two moderators trying to do some action which requires writing to multiple tables in the database, and the requests of those moderators are being handled by different servers in the server farm, how can one implement some locking functionality to ensure that they can't take that action on the same item at the same time?

So far, I'm thinking about using a table in the database to sync, e.g. check the id of the item in the table if doesn't exsit insert it and proceed, otherwise return. Also probably a shared cache could be used for this but I'm not using this at the moment.

Any other way?

By the way, I'm using MySQL as my database back-end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听闻余生 2024-07-24 23:53:14

您的问题意味着数据级并发控制——在这种情况下,请使用 RDBMS 的并发控制机制。

如果稍后您希望控制应用程序级别的操作,这些操作不一定一对一地映射到数据实体(例如表记录访问),那么这对您没有帮助。 一般的解决方案是有一个反向代理服务器,它可以理解应用程序级语义并在必要时进行相应的序列化。 (这会对可用性产生负面影响。)

阅读 CAP 定理也可能不会有什么坏处!

Your question implies data level concurrency control -- in that case, use the RDBMS's concurrency control mechanisms.

That will not help you if later you wish to control application level actions which do not necessarily map one to one to a data entity (e.g. table record access). The general solution there is a reverse-proxy server that understands application level semantics and serializes accordingly if necessary. (That will negatively impact availability.)

It probably wouldn't hurt to read up on CAP theorem, as well!

姐不稀罕 2024-07-24 23:53:14

您可能想要研究分布式锁定服务,例如 Zookeeper。 它是 Google 服务的重新实现,为应用程序提供非常高速的分布式资源锁定协调。 不过,我不知道将其合并到网络应用程序中有多容易。

You may want to investigate a distributed locking service such as Zookeeper. It's a reimplementation of a Google service that provides very high speed distributed resource locking coordination for applications. I don't know how easy it would be to incorporate into a web app, though.

書生途 2024-07-24 23:53:14

如果所有状态都在(中央)数据库中,那么数据库事务应该为您处理这个问题。

请参阅http://en.wikipedia.org/wiki/Transaction_(database)

If all the state is in the (central) database then the database transactions should take care of that for you.

See http://en.wikipedia.org/wiki/Transaction_(database)

沙沙粒小 2024-07-24 23:53:14

这可能与您无关,因为这个问题很旧,但它对其他人仍然可能有用,所以我无论如何都会发布它。

您可以在锁定对象上使用“SELECT FOR UPDATE”数据库查询,因此您实际上使用数据库来实现锁定机制。

如果你使用ORM,你也可以这样做。 例如,在 nhibernate 中你可以这样做:

session.Lock(Member, LockMode.Upgrade);

It may be irrelevant for you because the question is old, but it still may be useful for others so i'll post it anyway.

You can use a "SELECT FOR UPDATE" db query on a locking object, so you actually use the db for achieving the lock mechanism.

if you use ORM, you can also do that. for example, in nhibernate you can do:

session.Lock(Member, LockMode.Upgrade);
长安忆 2024-07-24 23:53:14

拥有一个锁表是一种不错的方法,它简单且有效。

您还可以将代码作为单个服务器上的服务,更多的是一种 SOA 方法。

您还可以将时间戳字段与事务一起使用,如果自上次获取数据以来时间戳已更改,则可以恢复事务。 因此,如果有人先进入,他们就有优先权。

Having a table of locks is a OK way to do it is simple and works.

You could also have the code as a Service on a Single Server, more of a SOA approach.

You could also use the the TimeStamp field with Transactions, if the timestamp has changed since you last got the data you can revert the transaction. So if someone gets in first they have priority.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文