In everyday life, you may have left a carton of milk in the refrigerator for too long and forgotten to throw it out. As a result, you end up with stale milk taking up space, which is of no use and can be harmful if consumed. The solution in both cases is similar: throw it out. This blog is all about explaining the expiration strategies for keeping the data in your cache fresh and throwing the stale data out! As such, to keep your cache data fresh, it should come with an expiry as well.
Cache Data Expiration: The Need and the Concept
Imagine you’re running an e-commerce website, and you’ve cached frequently used product information. Over time, the cache data hasn’t been not updated, but now the prices of certain products have been updated, but not in the cache. This situation causes data inconsistency, as customers are still viewing the outdated prices from the cache, not the updated ones from the database.
To avoid any resulting data integrity issues NCache provides the Expiration feature where you can set a time limit with your data, and once the limit has been reached, the data is no longer valid. This data needs to be removed from the cache after a predefined Clean Interval—an interval set by user, after which all the expired items are automatically removed from the cache. Thus, any expired item is not removed from the cache immediately.
Types of Expiration in NCache
NCache provides the following strategies to cater to the user’s requirements. We will look closely at both of these strategies and their usage.
Absolute Expiration
In this strategy, an absolute time is specified with the item that needs to be invalidated. The time specified is maintained in the UTC format, hence, the time specified in any zone is converted to this format on the cache server. The time can range from seconds to months, and after the time passes, the item expires. Look at Figure 1 for further clarity:
Use Case:
For cases where you can estimate how long any item is required to be retained in the cache. For example, if a limited time product is launched and cannot be sold after 24 hours, the item is added with an absolute expiration of 24 hours and after that, the product expires from the cache.
Let us look at the code example below that shows how to add an item in the cache that expires after 5 minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Pre-condition: Cache is already connected // Get product from database against given product ID Product product = FetchProductFromDB(1001); // Generate a unique cache key for this product string key = $"Product:{product.ProductID}"; // Create a new CacheItem for this product with expiry var cacheItem = new CacheItem(product); var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(5)); cacheItem.Expiration = expiration; cache.Insert(key, cacheItem); |
Sliding Expiration
As the name explains, this strategy keeps the data in the cache as long as it is being used. So, the data that has not been used for a specific time is invalidated. Every time any data added with sliding expiration is accessed, the duration for that data to exist in the cache is extended. For example, if any item with 30 seconds sliding interval, is not accessed for 30 seconds, it is expired. Similarly, if it is accessed within 30 seconds, the life of the item is exceeded by another 30 seconds in the cache.
Use Case:
Let’s suppose you have an application that takes the user’s credentials to be accessed. You want to provide access to the user as long as the user is active and using the application. Sliding expiration helps you in such cases where the session can be maintained by keeping track of the active users.
In the example given below, a sliding expiration of 5 minutes is added with the product:
1 2 3 4 5 6 7 8 9 10 11 12 |
// Get product from database against given product ID Product product = FetchProductFromDB(1001); // Generate a unique cache key for this product string key = $"Product:{product.ProductID}"; // Create a new CacheItem for this product with expiry var cacheItem = new CacheItem(product); var expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5)); cacheItem.Expiration = expiration; cache.Insert(key, cacheItem); |
Default Expiration Strategies
NCache also lets you add default expiration to help you save the effort of manually configuring it. These can only be configured using NCache Management Center or configuration files (shown later in the blog). Following are the default values:
- Default Absolute
- Default Sliding
- Default Absolute Longer
- Default Sliding Longer
The default value is 5 seconds for all the expiration strategies. Please refer to Default Expiration documentation for more information.
Configure Expiration using NCache Web Manager
You can enable expiration and set its duration using NCache Management Center in the following way:
Configure Expiration using Configuration files
You can also enable expiration in NCache using the configuration file (config.ncconf) that is installed in NCache install directory by default. Under the <cache-settings> tag, there is an <expiration-policy> tag as shown below:
1 2 3 4 |
<expiration enabled="False"> <absolute longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/> <sliding longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/> </expiration-policy> |
Similarly, you can specify the cleanup interval after which the expired items are removed from the cache as shown below:
1 2 3 |
<cache-settings ... > <cleanup interval="15sec"/> </cache-settings> |
Bulk Removal of Expired Items
Expired items in the cache can be removed in bulk to prevent any performance issues that might occur from deleting items individually. You can specify the size of the bulk removal and the delay between consecutive removals. NCache comes with an Alachisoft.NCache.Service.exe.config file placed in the installation directory and has the following configurable tags:
1 2 |
<add key="NCacheServer.ExpirationBulkRemoveSize" value="10"/> <add key="NCacheServer.ExpirationBulkRemoveDelay" value="0"/> |
Facts to Know
- If any item is added in the cache with no expiration (absolute or sliding), it remains in the cache forever until manually removed.
- You will have to restart the NCache service after configuring anything using the configuration files for the change to take effect.
- In NCache, expiration works differently with respect to different topologies, everything is explained thoroughly in Expiration in Clustered Environment.
Conclusion
In conclusion, effectively managing stale data in your cache is essential, and expiration is a proven method for achieving this. NCache’s robust features Absolute and Sliding Expiration, along it’s customizable default expiration strategies, is critical in managing your cache data efficiently.