Alachisoft / NCache

NCache: Highly Scalable Distributed Cache for .NET
http://www.alachisoft.com
Apache License 2.0
650 stars 123 forks source link

Absolute Expiration is not respected exactly #39

Open gregpakes opened 5 years ago

gregpakes commented 5 years ago

I can't get NCache to respect a 10 second absolute expiration. It always expires after about 15 seconds.

I have created a sample repository to demonstrate this:

https://github.com/gregpakes/NCache-Expiration-Test

The code is simple. In the ValuesController (https://github.com/gregpakes/NCache-Expiration-Test/blob/master/NCache-Expires/Controllers/ValuesController.cs), I am caching the current DateTime for 10 seconds. If you continually refresh the page, you will see that the value gets refreshed every 15 seconds or so. I would expect this to be 10 seconds.

I have tried the same demo using MemoryCache.Default and it works as expected. The cache entry is refreshed every 10 seconds.

What is the issue here?

Brad-NCache commented 5 years ago

Hi Greg,

Please note that NCache has a background Expiration thread that marks cache items that are expired (absolute or sliding) and then removes all such items in bulk. This bulk cleanup is done periodically and is referred to as clean-up interval which is configurable and has a default value of 15 seconds. That is why you are seeing expirations happening at the 15 sec mark.

This bulk removal pattern is much more efficient than having the thread continuously looping through the items and removing expired items within moments of expiry as this is a much more CPU-hungry operation. With possibly millions of expirations going on at a time, you can see how continuous expiration checks can seriously hamper cache response time.

The default value of 15 sec for the clean-up interval has been tested to be appropriate for most applications. However, If you would like to change the clean-up interval value for a cache to be different than the default, you can easily do so in the config.ncconf file and changing the value of the interval attribute of the cleanup tag nested within the cache-settings tag. Please do remember to restart NCache service and restart the cache for the changes to take effect.

As an example, shown below is the screen grab from a config.ncconf file showing a clean-up interval value of 15 sec configured for the mypartitionedcache. More information about NCache expiration policies and gotchas can be found at the following link:

http://www.alachisoft.com/resources/docs/ncache-oss/admin-guide/data-expiration.html

cleanup_interval

gregpakes commented 5 years ago

Hi Brad,

Thanks for the clarification. That is interesting. So NCache does not work for any cache duration that is less than the clean-up interval, which defaults to 15 seconds.

We have a number of places where we cache for less time than that, so this may be an issue for us. However, I suspect we do this due to a lack of decent cache invalidation, so that might be a smell in itself that we should look to eradicate.

Thanks for the information.

Greg

Brad-NCache commented 5 years ago

Hi Greg,

Thanks for your confirmation. As suggested earlier, Clean Interval value is configurable on a cache level and you can always use a smaller value which may work for you. The minimum value you can use is 5 seconds.

Moreover, based on your comment for cache invalidation strategies, please note that NCache does provide a number of strategies that may be of interest to you. I am giving the links to some of the relevant topics below:

http://www.alachisoft.com/resources/docs/ncache/prog-guide/cache-dependencies.html

http://www.alachisoft.com/resources/docs/ncache/prog-guide/notification-based-dependencies.html

http://www.alachisoft.com/resources/docs/ncache/prog-guide/sync-cache-with-external-source.html

If you find something in there that may be of interest to you, please do let me know and I will try to assist you more in the matter.