简体   繁体   中英

Distributed locking mechanism .NET

We have a requirement to manage concurrent operations of a task. In short only one version of this task can be running at any one time.

The issue is we will be running in a multi-server environment.

Requirements:

  • Allow once instance of a method to run at any one time. (OS mutex).
  • Must work in a multi server environment.
  • Mutex must be dropped if process dies.
  • Must be a robust and mature solution.

Environment:

  • Windows Server (on premise)
  • .Net
  • Azure

Things I have considered so far:

  • OS mutex: Works for on premise, unsure if it will release the lock on process death. Also unsure if windows supports a robust mutex.
  • DB Flag: Works for on premis, works in multi-server. Won't unlock on process death.
  • AppFabric: Works on premise though an extra setup step is undesirable. Works multi-server. Won't unlock on process death though locks can be set to time out when acquired. (best so far)
  • CIFS File lock: Works on premise, works multi-server, should unlock on process death. Fails the robust test though this may be personal bias.

I imagine this is a fairly common problem and I'm interested to hear how the community commonly solves it.

I'm specifically interested in hearing thoughts on the CIFS locking solution or if MSMQ could be used?

If you are OK using Windows Azure Storage, then you can use a Windows Azure Blob to acquire a lease on that blob with a timeout (the max you can use is 1 minute). The server that acquires the lease can run the task, and it can keep renewing the lease before it expires.

Other servers will continue to try and acquire the lease, in case the leader process dies.

How often you poll depends on how critical it is for other processes to immediately resume, but can probably lead to some extra cost (which might not be marginal if its too frequent and depending how many servers you have polling competing for that lease).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM