Distributed Critical Partition in a Web Farm

I have about 50 websites load balanced on 5 web servers. All of them use corporate library caching and gain access to the same caching database. Items in the caching database are updated every few hours using the implementation of ICacheItemRefreshAction.

I want to ensure that only one website ever updates the cache by putting the update code in a critical section .

  • If the websites were running in the same application pool on the same server, I could use lock ()

  • If the websites were running in separate application pools on the same server, I could use Mutex .

However, they will not provide a critical partition on multiple web servers.

I am currently creating a new key in the caching database to act as a mutex. It will be usual , but I see a small chance that 2 processes can enter a critical section.

public class TakeLongTimeToRefresh : ICacheItemRefreshAction
{
    #region ICacheItemRefreshAction Members

    public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
    {
        string lockKey = "lockKey";
        ICacheManager cm = CacheFactory.GetCacheManager();

        if (!cm.Contains(lockKey))
        {
            Debug.WriteLine("Entering critical section");
            // Add a lock-key which will never expire for synchronisation.
            // I can see a small window of opportunity for another process to enter
            // the critical section here...
            cm.Add(lockKey, lockKey, 
                   CacheItemPriority.NotRemovable, null, 
                   new NeverExpired());

            object newValue = SomeLengthyWebserviceCall();
            cm.Remove(removedKey);
            Utilities.AddToCache(removedKey, newValue);

            cm.Remove("lockkey");
        }
    }
}

Is there a way to have a guaranteed critical section to ensure that I don't call the web service twice?

EDIT I must add that I cannot use a shared file, since deployment policies will prevent it.

Links to Stackoverflow:

+3
source share
2 answers

, . , t SQL , :

set transaction isolation serializable;
update t set lock = 1 where lock = 0;

, 1 , , 0. , , SQLServer, , U- S-, 0 , ( 1).

+4

/ , , .

, , , (), .

+1

All Articles