Share via


Expiration and Eviction for Azure Managed Cache Service

Important

Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me?

Microsoft Azure Cache does not retain cached objects in memory permanently. In addition to being explicitly removed from the cache by use of the Remove method, cached objects may also expire or be evicted by the cache.

Expiration

Cache expiration allows the cache to automatically remove cached objects from the cache. When using the Put or Add methods, an optional object time-out value can be set for the particular cached object that will determine how long it will reside in cache. If the object time-out value is not provided at the time the object is cached, the cached object uses the default expiration time. The default time for Managed Cache Service is ten minutes.

The Expiry Policy works in conjunction with the Time (min) setting to determine when cached items expire, and these behaviors are configured at the named cache level in the Management Portal on the Configure tab for Cache.

Expiry Policy for Windows Azure Cache Service

There are three types of expiration policies: Absolute, Sliding, and Never.

Expiry Policy Description

Never

Expiration is disabled. Items remain in the cache until they are evicted. When Never is specified, Time (min) must be set to 0.

Absolute

The expiration interval specified by Time (min) begins when an item is added to the cache. After the interval specified by Time (min) elapses, the item expires.

Sliding

The expiration interval specified by Time (min) is reset each time an item is accessed in the cache. The item does not expire until the interval specified by Time (min) elapses after the last access to the item. This keeps frequently used items in the cache longer.

Absolute is the default expiration policy and ten minutes is the default setting for Time (min). The expiry policy is fixed for each item in a named cache, but the Time (min) can be customized for each item by using Add and Put overloads that take a timeout parameter.

Note

It is important to note the behavior of Sliding expiration when used in combination with local cache. If an item is read from the local cache, this does not access the object on the cache. So it is possible that the item will expire on the server even though it is being read locally.

When cached objects are locked for the purposes of concurrency, they will not be removed from the cache even if they are past their expiration. As soon as they are unlocked, they will be immediately removed from the cache if past their expiration.

To prevent instant removal when you unlock expired objects, the Unlock method also supports extending the expiration of the cached object.

You can view the health and performance of your cache on the Monitor tab for Cache in the Management Portal, including metrics for eviction and expiration. For more information, see Monitoring the cache service using the Monitor tab.

Local Cache Invalidation

There are two complementary types of invalidation for local cache: time-out-based invalidation and notification-based invalidation.

Tip

After objects are stored in the local cache, your application can use those objects until they are invalidated, regardless of whether those objects are updated by another client. For this reason, it is best to enable local cache for data that changes infrequently.

Time-out-based Invalidation

After objects are downloaded to local cache, they stay there until they reach the object time-out value specified in the cache client configuration settings. After they reach this time-out value, objects are invalidated so that the object can be refreshed from the cache the next time that it is requested.

Notification-based Invalidation

If your cache client has enabled local cache, you can also use cache notifications to automatically invalidate your locally cached objects. By shortening the lifetime of those objects on an "as needed" basis, you can reduce the possibility that your application is using stale data.

Note

Notifications are available in the Standard and Premium cache offerings, and are not available in the Basic cache offering. For more information, see Cache Offerings for Azure Managed Cache Service.

When you use cache notifications, your application checks with the cache on a regular interval to see if any new notifications are available. This interval, called the polling interval, is every 300 seconds by default. The polling interval is specified in units of seconds in the application configuration settings. Note that even with notification-based invalidation, timeouts still apply to items in the local cache. This makes notification-based invalidation complementary to timeout-based invalidation.

For more information and examples, see Local Cache for Azure Managed Cache Service.

Eviction

To maintain the memory capacity available for cache, least recently used (LRU) eviction is supported. When memory consumption is close to the size of your cache offering, objects are evicted from memory, regardless of whether they have expired or not, until the memory pressure is relieved.

Warning

If you disable eviction, you run the risk of throttling. In this condition, memory has exceeded the threshold, but there is no ability to alleviate the memory shortage. Clients that attempt to add items to the cache in this state receive an exception until it is resolved.

You can view the health and performance of your cache on the Monitor tab for Cache in the Management Portal, including metrics for eviction and expiration. For more information, see Monitoring the cache service using the Monitor tab.

There are two settings for eviction: Enabled and Disabled. Eviction is configured at the named cache level in the Management Portal on the Configure tab for Cache, and the default setting is Enabled.

Eviction Policy for Windows Azure Cache Service

Specifying Expiration and Eviction Settings using Code

The following methods allow you to override the default settings that are in the cache:

  • The Add and Put methods provide overloads that allow you to specify an expiration time-out value only for the object you add to the cache.

  • The PutAndUnlock and Unlock methods provide overloads that allow you to extend an object's expiration after unlocking it.

  • The ResetObjectTimeout method allows you to explicitly extend an object's lifetime, overriding the expiration settings of the cache.

Note

Regardless of the expiration or eviction settings, cache data is not preserved during unexpected shutdowns or failures of the cache service. Your application code must reload the cache from a data source if the data is not found in the cache. This is often referred to as a cache-aside programming pattern.

See Also

Other Resources

Azure Managed Cache Service Features