Cache Invalidation and the Methods to Invalidate Cache (original) (raw)

Last Updated : 30 May, 2026

Cache invalidation is the process of removing or updating outdated data stored in the cache memory. It ensures that applications always use the latest and correct data instead of stale cached information.

**Example: If a user changes their profile picture, the old image may still appear from the cache. Cache invalidation removes the old cached image so the new profile picture is displayed correctly.

Importance

Cache invalidation is important because it ensures that applications do not serve outdated or incorrect data from cache when the original data has changed.

Cache Invalidation Methods

Cache invalidation is an important process for maintaining accurate and up-to-date data in a cache. There are several methods of cache invalidation, each with its own advantages and disadvantages. Here are a few of the most popular techniques:

1. Time-based Cache Invalidation

Time-based cache invalidation is a technique where cached data is assigned a fixed expiration time. Once this time expires, the data is considered invalid and must be refreshed from the original source.

**Example: A weather app stores weather data in cache for 10 minutes. After 10 minutes, the cached data expires and the app fetches updated weather information from the server.

Benefits

Challenges

2. Key-based Cache Invalidation

Key-based cache invalidation is a technique where each cached data item is linked to a unique key. When the original data changes, that specific key is invalidated, and the cache is either updated or removed.

**Example: In an e-commerce site, each product has a unique cache key. When the product price changes, only that product’s cache key is invalidated and updated.

Benefits

Challenges

3. Write-through Cache Invalidation

Write-through cache invalidation is a strategy where data is first written to the primary data source and then immediately updated or removed in the cache. This ensures the cache always stays synchronized with the main database.

**Example: In a banking system, when a user deposits money, the transaction is first saved in the database. Then the updated balance is immediately reflected in the cache so the user always sees the correct balance.

Benefits

Challenges

4. Write-behind Cache Invalidation

Write-behind cache invalidation is a technique where data is first updated in the cache and then written to the original data source asynchronously. This improves performance because the application does not wait for the database update to complete immediately.

**Example: In a social media app, when a user updates their status, it is first saved in the cache for quick display. The update is then written to the database later in the background.

Benefts

Challenges

5. Purge Cache Invalidation

Purge cache invalidation is a method where cached data for a specific object, URL, or group of URLs is completely removed from the cache. It is used when the cached content becomes outdated or incorrect after updates.

**Example: When a news article is updated after publication, the cached version of that article page is purged. The next user request loads the updated article directly from the server.

Benefits

Challenges

6. Refresh Cache Invalidation

Refresh cache invalidation is a technique where the system fetches the latest content from the origin server even if cached data already exists. Instead of deleting the cache, it updates the existing cached entry with the newest version.

**Example: In a stock market app, cached stock prices are periodically refreshed from the server. The old cached values are replaced with the latest prices so users always see up-to-date information.

Benefits

Challenges

7. Ban Cache Invalidation

Ban cache invalidation is a method where specific rules such as URL patterns, headers, or conditions are used to identify and remove cached content. Any cached data matching the ban criteria is immediately invalidated.

**Example: In a website, all cached pages under /products/outdated/* are banned after a major catalog update. This ensures users always receive updated product information from the origin server.

Benefits

Challenges

8. Time-To-Live(TTL) expiration Cache Invalidation

Time-to-Live (TTL) cache invalidation is a method where cached data is assigned a fixed time limit after which it becomes stale. When a request is made, the cache checks the TTL value and serves data only if it is still valid; otherwise, it fetches fresh data from the origin server and updates the cache.

**Example: A video streaming platform caches movie metadata for 30 minutes. After 30 minutes, the cached data expires and is refreshed from the server to ensure updated information is shown.

Benefits

Challenges

9. Stale-while-revalidate Cache Invalidation

Stale-while-revalidate is a caching technique where the system serves cached (possibly slightly outdated) content immediately while simultaneously fetching an updated version from the origin server in the background. Once the fresh data is received, the cache is updated.

**Example: In a news website, an article is shown instantly from cache when a user visits. At the same time, the system fetches the latest version in the background and updates the cache for future users.

Benefits

Challenges