views:

53

answers:

1

Hi, I have to create a .net web application which would run on more than one front end server which are load balanced. Now I have to use some third party api which is not thread safe so only one thread should call the API at a time so I have to use some syncronization machanism like lock or mutex which provide syncronisation within the process and across the process in machine.Now will lock or mutex work in loadbalanced multiserver environment correctly? If not then is there any mechanism using which I can syncronise all the threads of my application in all the machines??

+1  A: 

In what way is the third party API "not thread safe"? Usually that means you can only call it from one thread per process - but you can call it from multiple different processes at the same time, unless they will all be accessing a single shared resource.

If you could tell us more about the API, that would help...

EDIT: Okay, as it's using a single shared resource, you might want to consider creating a server which all the others connect to and which is the sole machine to use this third-party API. This might involve a message queue for requests, or possibly just a normal web service. Obviously that would be a single point of failure and a bottleneck in the system - if you can find any way of avoiding the single shared resource, that would be preferable.

You could potentially avoid it being a single point of failure by having multiple machines "trying" to be this master, e.g. by holding a database lock. Of course this comes back to your original question of a shared lock - but it means that you're not attempting to acquire that lock for each of the third party API operations - just for the privilege of being the "active master".

Jon Skeet
Actually API is using a single shared resource internally so only one thread should call it at a time.
Anoop