title: ASP.Net server side data caching on a web farm
body: <p><strong>Scenario:</strong> </p>
  
  <p>Implement in-memory caching of master data in the WCF layer of an ASP.Net application for a web farm scenario  </p>
  
  <ol>
  <li>Data is cached on first access to the service layer ‘s, say, GetCountryList() method with cache’ expiry set to midnight.  Let’s say the cache key is “CountryList_Cache”</li>
  <li>All subsequent requests are served through cache’</li>
  <li>If the Country list is updated using the master screen, then an additional call is made to invalidate the “CountryList_Cache” and fresh data is loaded into it</li>
  <li>The next call now receives the updated country list
  The above step is easy in a single server scenario, as step 3 only requires a cache expiry call to one server.  The complexity increases when we have 2 or 3 load balanced web servers, because in that case the cache is updated (via master screen) on only one of the servers but has to be invalidated on all 3 servers.</li>
  </ol>
  
  <p><strong>Our proposed solution:</strong></p>
  
  <p>We intend to have an external service/ exe/ web page which would be aware of all load balanced servers (via a configuration file).  In order to invalidate a specific cache, we would invoke this external component which in turn would invalidate the respective cache key on all the web servers and load then cache with latest data.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>Although the above approach would work for us, we do not think it is a clean approach for an enterprise class LOB application.  Is there a better/ cleaner way of achieving the cache expiry across multiple servers?</p>
  
  <p>Note:</p>
  
  <ol>
  <li>We do not want to use distributed caching due to the obvious performance penalty, as compared to in-proc/ in-memory cache</li>
  <li>Caching has been implemented using System.Runtime.Caching</li>
  <li>We have worked with SQL dependency and used it in scenario of single web server </li>
  </ol>