Last week I was asked to help troubleshoot some performance issues with a large site running on a Web Farm. In order to support Web Farms there are two settings in the web.config AppSettings.
- WebFarmEnabled
- CachePersistence
What do these settings do? Well, in short they are used to help the memory-based caches remain in sync with each other in a Load Balanced situation.
If both settings are set to false, then the Cache is saved in the memory of the server processing the request. If the Cache is cleared, for whatever reason, then the other servers are not "informed" of this so their Caches remain out of sync.
If the first setting "WebFarmEnabled" is set to true, then the Cache is still just saved in the memory of the server processing the request. However, a Cache Dependency is applied to the Cache which is dependent on a file which is persisted in the common Portal Home Directory. As each server's cache has a dependency on the same file, if the file is modified - then the cache is invalidated and the object will be reloaded from the database, and recached.
This file ONLY affects the cache IF it is changed - this the servers manage their own memory cache and the common file is used to invalidate - and keep in sync - memory caches if there is a change.
If the second setting "CachePersistence" is set to true then the same common file is created, except that the cached object is actually serialized to this file. This allows the cache to be persisted across Application Restarts. As long as the cache has not been explicitly cleared, then the cache provider can load (deserialize) this object back to the cache if the memory based cache has expired - which would be the case if the Application restarts.
So far so good. However, the real point I want to make in this Blog is that this second setting can even help in situations where a web farm is not being used. If you enable Cache Persistence and the memory cache expires then the Caching Provider first tries to load the object from the file saved in the common Portal Home Directory, and only if this has been explicitly cleared does it load from the database.
If you are in a situation where the database is a "Gating Resource" then you should consider using the setting.
Note: Not all cached objects are "persisted" in this way. In addition to the AppSetting, the code has to call a specific method of the DataCache class. Cached collections that are persisted in the core include:
- Portals
- Tabs
- Modules
- TabPermissions
- ModulePermissions