Lock your web app to 1 outgoing web service call across all threads

One of the things we see a lot of these days are web apps that call other web apps via web services, RSS, or REST APIs.

I do this all the time, and often cache the response of the request (especially RSS and stuff like that) so that my high-traffic site doesn't kill RSS host (it's it just makes sense).

First of all, you should be using an Asynchronous Page for this (a new feature as of Asp.Net 2.0, not really new...) since you don't want to be tying up IIS threads with your relatively slow server-to-server calls, but what if that call takes 3 seconds to respond and you have a barrage of traffic (say 1 thousand requests per second) at that point? 

What you really want, is for only 1 request from you server to hit the other server, cache the response, and then have all the other requests on your server wait and use the cached response.  Even if this takes 3 seconds, that's way better then killing the other server and getting timeouts.

The good thing is, this is really easy - watch.

 

private static Semaphore _serverClientLockAccess = new Semaphore(1, 1);

private static string DoMyServerWork(string url)
{
    _serverClientLockAccess.WaitOne();

    WebClient client = new WebClient();
    string s = client.DownloadString(url);

    _serverClientLockAccess.Release(1);

    return s;

}

As you can see, the Semaphore makes it easy to lock the access to this service to one thread at a time.

Obviously, we need to add more logic to implement the idea of ReaderWriterLock on that Cache object that will hold our data returned from the service... but that's another day.  (hint: go here to read about this)

 

More later - joel

No Comments