I am new to azure caching and facing one problem.
I will brief about the scenario first. We are using SQL Azure
as DB for our application. To avoid the latency issues and Throttling issues we are using Azure Caching (co-located caching on web role). By using Azure Caching we are fetching data from DB only once and keeping it in cache for further use.
Since we are using Cached data, challenge here is to always keep in sync data between SQL Azure DB and Azure Cache at any point of time whenever there is any DML operation performed. We are doing this by first updating DB and if update successful invalidating cached data. This approach works fine for normal scenario. However, with concurrent users working and performing updates there seems an issue. We are using pessimistic concurrency model while updating data in Cache. Here, we are using transient retry policy to make ensure retry attempts (let's say 5 times with fixed interval of 1 sec).
The sample code looks like this:
Microsoft.Practices.TransientFaultHandling.RetryPolicy cacheRetryPolicy =
GetAzureCacheRetryPolicy();
cacheRetryPolicy.Retrying += TransientRetryManager.OnRetrying;
DataCacheLockHandle handle = null;
cacheRetryPolicy.ExecuteAction(() =>
{
try
{
object obj = cache.GetAndLock(key, TimeSpan.FromSeconds(1), out handle);
cache.PutAndUnlock(key, value, handle, expirationTime.Value, tags);
}
catch (DataCacheException ex)
{
if (ex.ErrorCode == DataCacheErrorCode.KeyDoesNotExist)
{
cache.Put(key, value, expirationTime.Value, tags);
}
else
{
//This means wait till the cache is released.
//Throwing from here will allow to retry with Transient
//handling retry policy.
throw ex;
}
}
}
Here, in case number of waits (lets say 6) exceed the number retry attempts (i.e. 5 in our case). By the time 6th comes for its turn, retry attempts are already over and hence 6th Wait which has latest update will fail to update it in cache.
There is one way to overcome this problem by queuing all waits in case errorcode
is objectlocked
while getting the lock on cache key.
Can anyone please suggest best way of handling this case?