.NET Caching how does Sliding Expiration work?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Sliding expiration means a cache entry stays alive as long as it keeps getting used within a configured time window. Every successful access pushes the expiration deadline forward, so frequently used items remain cached while inactive ones age out automatically.
The Basic Idea
Suppose an entry has a sliding expiration of 10 minutes. If nobody reads it for 10 minutes, it expires. If code reads it after 6 minutes, the timer resets, and the new expiration point becomes 10 minutes from that read.
That makes sliding expiration very different from absolute expiration:
- absolute expiration removes the item at a fixed time
- sliding expiration removes the item after a period of inactivity
Sliding expiration is useful for session-like data, frequently reused lookup results, and other values that should stay warm only while they are actively needed.
Example with IMemoryCache
In ASP.NET Core, a common implementation uses IMemoryCache and MemoryCacheEntryOptions.
Every cache hit on product:42 refreshes the inactivity window. If the key is not touched for 10 minutes, it becomes eligible for removal.
Combine Sliding and Absolute Expiration
A very common production pattern is to combine sliding expiration with an absolute upper bound. That prevents a hot item from living forever.
With this configuration:
- each access keeps the item alive within the 10-minute inactivity window
- the item still expires after one hour total, no matter how often it is used
That pattern is safer for data that should eventually refresh even under steady traffic.
Important Behavior Detail
Sliding expiration is refreshed by cache access, not by the mere existence of the item. If the key sits in memory and nothing calls Get, TryGetValue, or equivalent access paths, the sliding timer is not extended.
Also remember that expiration is not always enforced by a literal timer firing at the exact millisecond. Many cache implementations remove expired items during normal cache activity or cleanup cycles. So think in terms of expiration eligibility, not precise stopwatch behavior.
Older MemoryCache Style
The same concept exists in older .NET caching APIs as well:
The API shape changes, but the rule stays the same: each successful access resets the inactivity window.
Common Pitfalls
- Assuming sliding expiration is the same as absolute expiration.
- Forgetting that frequently accessed items can remain cached indefinitely unless you add an absolute cap.
- Expecting expiration to happen at an exact instant rather than during cache maintenance and access flow.
- Using sliding expiration for data that must refresh on a strict schedule.
- Believing writes or background existence alone refresh the sliding timer when the real trigger is cache access.
Summary
- Sliding expiration removes cache entries after a period of inactivity.
- Each successful access resets the inactivity window.
- It is useful for data that should stay cached only while it remains active.
- Combine it with absolute expiration when you need a maximum lifetime.
- Use it for access-driven freshness, not for strict time-based refresh guarantees.
Related reading
- Netlogo - How to replicate an experiment adjusting one parameter using import-world?
- No AppDomains in .NET Core Why?
- No current assignment for partition occurs even after poll in Kafka
- Nodejs Child Process on another server using server to server communication
- .NET Config Files configSource outside the application directory folder
- .NET Configuration app.config/web.config/settings.settings
- Node.js distributed shared memory solution
- Node.js maxing out at 1000 concurrent connections

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.