.NET Architecture & System Design Interview Question #32

What is the difference between absolute and sliding cache expiration?

Caching, CDN, Queues & Asynchronous Processing Junior Beginner

Quick Interview Answer

Absolute expiration removes a cache entry after a fixed time measured from creation or a specified timestamp. Sliding expiration extends the lifetime when the entry is accessed, provided it continues to be used.

Detailed Explanation

Absolute expiration removes a cache entry after a fixed time measured from creation or a specified timestamp. Sliding expiration extends the lifetime when the entry is accessed, provided it continues to be used.

Absolute expiration gives an upper bound on staleness. Sliding expiration is useful for frequently accessed data but can keep an entry alive for a long time, so designs sometimes combine both limits.

Code Example

entry.AbsoluteExpirationRelativeToNow =
    TimeSpan.FromMinutes(30);

entry.SlidingExpiration =
    TimeSpan.FromMinutes(5);