Azure Cache Preview - Retry logic, best practice

Answered Azure Cache Preview - Retry logic, best practice

  • Tuesday, July 31, 2012 8:47 PM
     
      Has Code

    I'm trying to figure out what the best practice would be for retry logic on Azure Cache Preview... Before we switched to the "Preview" cache we used the Transient Fault Handling (Topaz) library, but it's not compatible with Cache Preview, so now I need to fab my own.

    This is what I came up with, but with this logic we noticed a significant rise in CPU usage...

    var data = CacheActionRetryBlock(() => (T)_cache.Get(key))
    private T CacheActionRetryBlock<T>(Func<T> cacheFunction)
    {
    	var maxRetries = 10;
    	var delayMs = 100;
    
    	for (var retry = 0; retry < maxRetries; retry++)
    	{
    		try
    		{
    			return cacheFunction.Invoke();
    		}
    		catch (Exception ex)
    		{
    			if (retry < maxRetries)
    			{
    				Thread.Sleep(delayMs);
    			}
    			else
    			{
    				throw ex;
    			}
    		}
    	}
    
    	return default(T);
    }

    I've also stumbled across this piece of code Azure + Bing Maps: Accessing spatial data with Entity Framework, which is very similar to my own implication but is using "Action" rather than passing a function delegate.

    The question is, how can I improve on the logic above? Keep in mind this will be used on a Azure Web Role.

    Thanks in advance.

All Replies