Tutorial :Replicate Memcached to multiple servers



Question:

I have the following code:

private bool CheckForDuplicatesInCache(Message theMessage)  {      var cacheClient = MemcachedClient.GetInstance("IntegrationCache");      var messageHash = theMessage.GetHashCode();      if (cacheClient.Get(messageHash.ToString()).IsNotNull())      {          IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process.");          return false;      }                   cacheClient.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0));        return true;  }  

The problem is, when I scale this to multiple servers...I need to have multiple memcached intances that share data for redundancy. Does anyone have any insight?


Solution:1

There are 2 ways you could theoretically do it:

There is a product called repcached, however it only runs on linux right now. It has the ability to replicate the cache to a different server.

The other option is in code: you could write the value to BOTH caches and check for it in both chaches. If you add more than 2 servers, you could do it in a for loop.

        private bool CheckForDuplicatesInCache(Message theMessage)      {          var cacheClient = MemcachedClient.GetInstance("IntegrationCache");          var cacheClient2 = MemcachedClient.GetInstance("IntegrationCache2");          var messageHash = theMessage.GetHashCode();          if (cacheClient.Get(messageHash.ToString()).IsNotNull())          {              IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process.");              return false;          }            if (cacheClient2.Get(messageHash.ToString()).IsNotNull())          {              IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process.");              return false;          }            cacheClient.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0));          cacheClient2.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0));          return true;      }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »