# caching-strategies

Adopt this skill: https://proskills.md/skills/architecture/kraitdev-skill-md-skills-architecture-caching-strategies

Page: https://proskills.md/skills/architecture/kraitdev-skill-md-skills-architecture-caching-strategies
This document is the public skill bundle for that page. It contains SKILL.md and every helper file the catalog carries. No login is required.

## File: skills/architecture/caching-strategies/SKILL.md
size: 8004
upstream: https://github.com/kraitdev/skill.md/blob/main/skills/architecture/caching-strategies/SKILL.md

````text
---
name: caching-strategies
description: When improving read performance and reducing database load.
version: 2.0.0
category: architecture
tags: [architecture, performance, caching]
skill_type: architecture
author: skiLLM
license: MIT
compatible_agents: [claude-code, cursor, copilot, codex]
estimated_context_tokens: 1800
dangerous: false
requires_review: false
security_level: safe
dependencies: [database-query-optimization]
triggers: [performance, latency, database load, cache, redis]
permissions:
  filesystem: { read: true, write: true }
  network: { outbound: true }
  shell: { execute: true }
input_requirements: [slow read endpoints, cache infrastructure]
output_contract: [cache never source of truth, always has fallback, ttl enforced]
failure_conditions: [cache becomes source of truth, no fallback logic, infinite ttl]
last_updated: 2026-05-15
---

# Caching Strategies

## Purpose
Databases are slow; caches are fast. This skill teaches teams to strategically cache frequently accessed data in fast storage (Redis, Memcached) to intercept requests before they hit the database, dramatically reducing latency and database load. Caching is NOT a substitute for proper database optimization.

## When to use
- System is experiencing high latency on read-heavy API endpoints
- Database CPU/Memory usage is constantly peaking due to repeated queries
- Designing a new system expecting massive read throughput
- Content rarely changes but reads happen constantly

## When NOT to use
- Before optimizing database queries (optimize first, cache second)
- For data that changes frequently or is highly personalized
- When consistency is more important than performance
- As a quick fix for poor application architecture

## Inputs required
- Slow query logs or endpoint metrics
- Cache infrastructure (Redis, Memcached)
- Read/write patterns of the data
- Acceptable staleness window (TTL)

## Workflow
1. **Identify Bottlenecks**: Profile APIs to find read-heavy, slow-changing data (user profiles, static config, product catalogs)
2. **Measure Access Patterns**: How often is data read vs. written? How large? How old can it be?
3. **Select Strategy**:
   - *Cache-Aside*: App checks cache; on miss, fetches DB, writes cache
   - *Write-Through*: App writes to cache and DB simultaneously
   - *Write-Behind*: App writes to cache only; batch updates to DB later (use cautiously)
4. **Set Eviction Policy**: Assign Time-To-Live (TTL) values based on business tolerance for stale data
5. **Handle Invalidation**: Implement cache invalidation logic on mutations (UPDATE/DELETE)
6. **Prevent Stampedes**: Implement caching locks or staggered TTLs to prevent multiple clients fetching from DB simultaneously when a key expires
7. **Add Monitoring**: Track cache hit rates, eviction rates, and staleness

## Rules
- MUST treat cache as ephemeral (can disappear at any time)
- MUST NOT cache highly personalized or sensitive data (PII) in shared caches
- MUST ALWAYS implement fallback to primary data store on cache failure
- MUST set TTL values (no infinite cache)
- MUST invalidate cache on mutations (UPDATE, DELETE operations)
- MUST use separate caches for different data types/sensitivity levels
- MUST NOT use cache as source of truth

## Anti-patterns
- **Infinite TTL**: Storing keys without expiration (leads to unbounded memory, stale data)
- **Premature Caching**: Adding Redis before the database is properly indexed
- **No Fallback**: Cache failure crashes the application
- **Over-Caching**: Caching everything including rarely-used data (wastes cache memory)
- **Forgetting Invalidation**: Updating database but not cache (stale data served indefinitely)
- **Cache Stampede**: All requests hit cache miss simultaneously, causing DB spike
- **Storing Mutable Objects**: Storing references to objects that change, returning stale mutations

## Failure conditions
- Cache becomes source of truth (data lost if cache clears)
- No fallback logic when cache is unavailable
- Cache miss causes cascading database failures
- Stale data leads to data corruption
- Cache stampede (all requests simultaneously fetch from DB)

## Validation checklist
- [ ] Cache is treated as optimization layer, not data store
- [ ] Fallback to primary data store on cache miss works
- [ ] TTL is set (no infinite cache)
- [ ] Cache invalidation triggered on data mutations
- [ ] Cache hit rate > 80% for target queries
- [ ] No cache stampede when keys expire simultaneously
- [ ] Sensitive data not cached (or encrypted if cached)
- [ ] Cache memory usage monitored (no unbounded growth)
- [ ] Stale data handling defined (acceptable staleness window)
- [ ] Monitoring dashboards show hit rate and eviction rate

## Output format
- **Cache-Aside wrapper**: Function that checks cache, handles miss, populates cache
- **Invalidation triggers**: ON UPDATE/DELETE, clear cache key
- **Configuration**: TTL values, eviction policy, cache key naming convention
- **Monitoring**: Hit rates, eviction rates, staleness metrics
- **Documentation**: Which data is cached, staleness tolerance, fallback behavior

## Security considerations
- Sensitive data MUST NOT be cached or only in encrypted form
- Cache keys MUST be predictable (not guessable)
- Access control MUST be enforced (not all users can access all cache keys)
- Sensitive data MUST be masked in monitoring logs
- Cache MUST be protected from unauthorized access

## Agent execution notes
- Agent MAY: Add cache checks, implement invalidation, set TTL, add monitoring
- Agent MUST NEVER: Use cache as source of truth, forget fallback logic, set infinite TTL
- Agent MUST ASK: Before caching sensitive data, before major caching strategy change
- Agent MUST VALIDATE: Fallback logic works, TTL configured, invalidation on mutations

## Example

**❌ Anti-pattern (No fallback, infinite TTL, no invalidation):**
```javascript
// WRONG: No fallback if cache fails
const getUser = async (userId) => {
  return redis.get(`user:${userId}`); // Fails if Redis is down
};

// WRONG: Infinite TTL
redis.set(`config:app-settings`, settings); // Never expires = stale data forever

// WRONG: No invalidation on update
async function updateUser(userId, data) {
  await db.users.update(userId, data);
  // Forgot to clear cache - stale data served
}

// WRONG: Cache stampede - all requests hit DB when key expires
const getExpensiveData = async () => {
  const cached = await redis.get('expensive-data');
  if (!cached) {
    // All concurrent requests hit this - DB spike
    const data = await slowQuery();
    await redis.set('expensive-data', data, { ex: 3600 });
    return data;
  }
  return cached;
};
```

**✅ Correct pattern (Fallback, TTL, invalidation, stampede prevention):**
```javascript
// CORRECT: Fallback to database on cache miss or failure
const getUser = async (userId) => {
  try {
    const cached = await redis.get(`user:${userId}`);
    if (cached) return JSON.parse(cached);
  } catch (e) {
    logger.warn('Cache miss, falling back to DB', e);
  }
  
  // Fallback to DB
  const user = await db.users.findById(userId);
  
  // Repopulate cache
  try {
    await redis.setex(`user:${userId}`, 3600, JSON.stringify(user)); // 1 hour TTL
  } catch (e) {
    logger.warn('Failed to cache, but returning DB data', e);
  }
  
  return user;
};

// CORRECT: TTL on all cache entries
redis.setex('config:app-settings', 86400, JSON.stringify(settings)); // 24 hour TTL

// CORRECT: Invalidate cache on mutations
async function updateUser(userId, data) {
  await db.users.update(userId, data);
  await redis.del(`user:${userId}`); // Clear specific key
}

// CORRECT: Prevent cache stampede with locks
const stampedeLock = new Mutex();
const getExpensiveData = async () => {
  const cached = await redis.get('expensive-data');
  if (cached) return JSON.parse(cached);
  
  // Use lock to ensure only one request fetches from DB
  return stampedeLock.runExclusive(async () => {
    // Double-check if another request already populated cache
    con
````

## File: README.md
upstream: https://github.com/kraitdev/skill.md/blob/main/README.md

```text
<div align="center">

![Logo](ba.gif)

**skiLL.Md is a structured, open-source collection of reusable, self-contained Markdown modules designed to teach AI coding assistants and human developers how to perform specific software engineering tasks.**

[![Skills](https://img.shields.io/badge/14_Skills-white.svg)](./skills)
[![Version](https://img.shields.io/badge/v2.0.0-white.svg)]()
[![Registry](https://img.shields.io/badge/Registry-Synchronized-success.svg)]()
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)]()
![Markdown](https://img.shields.io/badge/-Markdown-000000?style=flat-square&logo=markdown)
[![License: MIT](https://img.shields.io/badge/MIT_License-maroon.svg)](./LICENSE)

<p align="center">
  <a href="#how-to-use-the-library">Usage Guide</a> •
  <a href="#repository-architecture">Architecture</a> •
  <a href="./CONTRIBUTING.md">Contributing</a>
</p>

</div>

## The Philosophy: Why Does This Exist?

When working with modern AI coding assistants (like Cursor, GitHub Copilot, or Claude) or autonomous agents, the quality of the output is heavily dependent on the context provided. 

Massive, monolithic "system prompts" or `.cursorrules` files quickly become unmanageable, leading to token bloat and diluted AI focus. 

**The skiLL.Md solves this by introducing modularity.**
Instead of telling the AI *everything* about your codebase at once, you dynamically inject hyper-specific, technology-agnostic checklists (`SKILL.md`) exactly when the AI needs them. 

- **No generic advice** ("Write clean code").
- **No training datasets** (Only rules, constraints, and actionable workflows).
- **No model or IDE specific logic** (Works across ChatGPT, Claude, Gemini, Github Copilot, Cursor, VSCode, Claude Code, Codex etc.).

## What is a `SKILL.md`?

A "Skill" is an executable, technology-agnostic mental checklist for a specific workflow, design pattern, or architecture. Every skill in this repository strictly adheres to `schema/SKILL_SCHEMA.md` and contains:

1. **Clear Use Cases:** Exact conditions for when the skill should be activated.
2. **Actionable Workflows:** Step-by-step instructions.
3. **Hard Constraints:** Non-negotiable architectural rules.
4. **Anti-patterns:** Specific mistakes the AI (or developer) must avoid.
5. **Output Formats:** Expected code footprint.

## How to Use the Library

Skills are meant to be used as injected context. Here is how you can use them across different workflows:

### 1. With AI Code Editors (Cursor, Windsurf, Copilot)
When asking your IDE's chat or inline generator to perform a task, `@`-mention the relevant skill file to strictly bind the AI's output to the standard.
> **Prompt:** *"I need to build a new Settings page for the user dashboard. Please review `@skills/frontend/react-component-design/SKILL.md` and `@skills/ui-ux/accessibility-ui-design/SKILL.md` before generating the code."*

### 2. With Autonomous Agents / Custom GPTs
If you are building a custom AI agent, use the `registry.json` file. 
Provide the registry to your agent as a tool. When the user asks for a task, the agent can query the registry, find the path to the required `SKILL.md`, and read the file into its context window *before* writing code.

### 3. As Human SOPs (Standard Operating Procedures)
Because every skill is written in clear, checklist-style Markdown, they double perfectly as onboarding documents, PR review checklists, and team engineering standards.

### The Power of `registry.json`
The repository includes an auto-updated `registry.json`. This acts as an API for the filesystem. Tooling, CI/CD pipelines, and AI agents can parse this file to dynamically load skills by `category` or `tags` without needing to crawl directories.

## Concrete Example: Prompt Injection

Here is an example of what happens when you combine an AI prompt with a Skill from this library.

**Without the Skill:**
> *"Write a generic React button component."*
> **Result:** The AI might write a class component, it might use inline styles, it might forget ARIA labels, or it might overcomplicate the props.

**With the Skill:**
> *"Write a React button component. Adhere strictly to the rules and anti-patterns defined in the attached `react-component-design/SKILL.md`."*
> **Result:** The AI outputs a pure functional component, strictly typed via TypeScript interfaces, isolated from business logic, utilizing CSS classes instead of inline styles, and avoiding prop-drilling.

## Contributing

We welcome contributions from the community! However, to maintain the high quality and uniformity of this library, **all contributions must pass strict quality checks.**

Please read the [CONTRIBUTING.md](./CONTRIBUTING.md) before submitting a Pull Request.

If you aren't sure if your skill belongs here, open a **New Skill Proposal** Issue using our issue templates!
```
