How to implement blocking in a server farm?

Are there well-known recommendations for synchronizing tasks in a server farm? For example, if I have a forum-based site running on a server farm, and there are two moderators who try to perform some actions that require writing to several tables in the database, and the requests of these moderators are processed by different servers on the farm server, as possible implement some locking functions to ensure that they cannot take this action in the same element at the same time?

So far I have been thinking about using a table in a database for synchronization, for example. check the id of the element in the table if it does not exsit insert it and continue, otherwise return. Probably a common cache could be used for this, but I'm not using it at the moment.

Any other way?

By the way, I use MySQL as the database of my database.

+4
source share
5 answers

Your question implies the level of concurrency data management - in this case, use the RDBMS concurrency management mechanisms.

This will not help you if you later want to control application-level actions that do not necessarily map one to one data object (for example, access to a record in a table). The general solution is a reverse proxy server that understands the semantics of the application layer and, if necessary, serializes. (This will negatively impact accessibility.)

It probably would not hurt to read the CAP theorem as well!

+4
source

You might want to explore a distributed locking service such as Zookeeper . This is a reimplementation of the Google service, which provides coordination of blocking distributed resources at a high speed for applications. I do not know how easy it would be to include in a web application.

+2
source

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

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

+1
source

Having a lock table is the right way to make it simple and works.

You can also have code as a service on one server, a more approach to SOA.

You can also use the TimeStamp field with Transactions, if the timestamp has changed since the last data was received, you can return the transaction. Therefore, if someone hits first, they have priority.

0
source

This may not be appropriate for you because the question is old, but it can still be useful to others, so I will post it anyway.

You can use the "SELECT FOR UPDATE" query for the lock object, so you are actually using db to achieve the lock mechanism.

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

session.Lock(Member, LockMode.Upgrade); 
0
source

All Articles