---
title: "How do you decide what data should be cached and for how long?"  
description: "How do you decide what data should be cached and for how long?"  
author: "Anubhav Sharma"  
published: 2025-04-02  
updated: 2025-04-02  
canonical: https://www.mindstick.com/interview/34024/how-do-you-decide-what-data-should-be-cached-and-for-how-long  
category: "cache"  
tags: ["cache", "programming help"]  
reading_time: 6 minutes  

---

# How do you decide what data should be cached and for how long?

#### How to Decide What Data to Cache and for How Long?

Caching is a trade-off between **freshness** and **performance**. To decide what data to cache and for how long, consider the following factors:

#### 1. What Data Should Be Cached?

## Good Candidates for Caching

| Data Type | Reason for Caching |
| --- | --- |
| **Frequently accessed data** | Avoid redundant database or API calls (e.g., top articles, user profile). |
| **Expensive computations** | Save CPU time (e.g., complex reports, aggregated statistics). |
| **Static or rarely changing data** | No need to fetch frequently (e.g., product categories, country lists). |
| **Third-party API responses** | Reduce external API calls and costs (e.g., weather, currency exchange rates). |
| **Session-specific data** | Improve user experience (e.g., shopping cart, user preferences). |

## Bad Candidates for Caching

| Data Type | Why Not Cache? |
| --- | --- |
| **Rapidly changing data** | Cached values might become stale (e.g., stock prices, live scores). |
| **Sensitive or personal data** | Security and privacy risks. |
| **Data with strict consistency needs** | Can lead to stale reads (e.g., banking transactions). |

#### 2. How Long Should Data Be Cached?

**Cache duration depends on how frequently the data changes and how critical freshness is.**

| Data Type | Suggested Cache Duration |
| --- | --- |
| **Static content** (logos, CSS, JS) | Days to weeks (`max-age=604800`) |
| **Product lists, categories** | Hours to a day (`max-age=3600 to 86400`) |
| **User sessions, shopping carts** | Session duration (`Sliding Expiration`) |
| **News articles, blog posts** | Minutes to hours (`max-age=600 to 3600`) |
| **Real-time stock prices, chat messages** | Do not cache (or use very short TTL < 10s) |

## Caching Strategies

| Strategy | When to Use? |
| --- | --- |
| **Absolute Expiration** (`max-age`) | Good for data that updates at known intervals (e.g., daily reports). |
| **Sliding Expiration** | Ideal for session-related data (e.g., extend expiry as long as the user is active). |
| **ETag / Last-Modified** | Best for browser caching (ensure the latest data is fetched only if changed). |
| **Cache Invalidation** | Use when data needs immediate updates (e.g., clear cache when an order is placed). |

#### 3. Choosing Cache Location

| Cache Type | Use Case |
| --- | --- |
| **MemoryCache** | Fastest, best for single-server apps. |
| **Distributed Cache (Redis, SQL Server Cache)** | Best for multi-instance apps. |
| **Browser Cache (Cache-Control, ETag)** | Best for static assets and API responses. |
| **CDN (Content Delivery Network)** | Best for images, scripts, videos. |

####

#### Summary

1. Cache frequently accessed, expensive, or static data.
2. Avoid caching real-time, sensitive, or fast-changing data.
3. Set cache duration based on data volatility (seconds for real-time, hours/days for static data).
4. Use a combination of in-memory, distributed, and browser caching for efficiency.
5. Implement cache invalidation strategies for dynamic content.

###

## Answers

### Answer by Anubhav Sharma

#### How to Decide What Data to Cache and for How Long?

Caching is a trade-off between **freshness** and **performance**. To decide what data to cache and for how long, consider the following factors:

#### 1. What Data Should Be Cached?

## Good Candidates for Caching

| Data Type | Reason for Caching |
| --- | --- |
| **Frequently accessed data** | Avoid redundant database or API calls (e.g., top articles, user profile). |
| **Expensive computations** | Save CPU time (e.g., complex reports, aggregated statistics). |
| **Static or rarely changing data** | No need to fetch frequently (e.g., product categories, country lists). |
| **Third-party API responses** | Reduce external API calls and costs (e.g., weather, currency exchange rates). |
| **Session-specific data** | Improve user experience (e.g., shopping cart, user preferences). |

## Bad Candidates for Caching

| Data Type | Why Not Cache? |
| --- | --- |
| **Rapidly changing data** | Cached values might become stale (e.g., stock prices, live scores). |
| **Sensitive or personal data** | Security and privacy risks. |
| **Data with strict consistency needs** | Can lead to stale reads (e.g., banking transactions). |

#### 2. How Long Should Data Be Cached?

**Cache duration depends on how frequently the data changes and how critical freshness is.**

| Data Type | Suggested Cache Duration |
| --- | --- |
| **Static content** (logos, CSS, JS) | Days to weeks (`max-age=604800`) |
| **Product lists, categories** | Hours to a day (`max-age=3600 to 86400`) |
| **User sessions, shopping carts** | Session duration (`Sliding Expiration`) |
| **News articles, blog posts** | Minutes to hours (`max-age=600 to 3600`) |
| **Real-time stock prices, chat messages** | Do not cache (or use very short TTL < 10s) |

## Caching Strategies

| Strategy | When to Use? |
| --- | --- |
| **Absolute Expiration** (`max-age`) | Good for data that updates at known intervals (e.g., daily reports). |
| **Sliding Expiration** | Ideal for session-related data (e.g., extend expiry as long as the user is active). |
| **ETag / Last-Modified** | Best for browser caching (ensure the latest data is fetched only if changed). |
| **Cache Invalidation** | Use when data needs immediate updates (e.g., clear cache when an order is placed). |

#### 3. Choosing Cache Location

| Cache Type | Use Case |
| --- | --- |
| **MemoryCache** | Fastest, best for single-server apps. |
| **Distributed Cache (Redis, SQL Server Cache)** | Best for multi-instance apps. |
| **Browser Cache (Cache-Control, ETag)** | Best for static assets and API responses. |
| **CDN (Content Delivery Network)** | Best for images, scripts, videos. |

####

#### Summary

1. Cache frequently accessed, expensive, or static data.
2. Avoid caching real-time, sensitive, or fast-changing data.
3. Set cache duration based on data volatility (seconds for real-time, hours/days for static data).
4. Use a combination of in-memory, distributed, and browser caching for efficiency.
5. Implement cache invalidation strategies for dynamic content.

###


---

Original Source: https://www.mindstick.com/interview/34024/how-do-you-decide-what-data-should-be-cached-and-for-how-long

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
