Back to Server-Side Performance

Cross-Request LRU Caching

servercachelrucross-request
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

Cross-request LRU caching provides a high-performance mechanism for retaining data in memory across distinct user interactions. Unlike request-scoped utilities such as React.cache, which clear once a single server response completes, an LRU cache maintains persistent state within the server process. By storing frequently accessed records—such as configuration settings, user profiles, or lookup tables—in the active heap, you eliminate repetitive database roundtrips during rapid sequential actions. This approach excels in environments like Vercel Fluid Compute where multiple incoming requests share a warm execution instance. Because the data resides locally, it avoids the network latency associated with external key-value stores. This technique balances memory constraints through a Least Recently Used eviction policy, ensuring the most relevant data remains accessible while preventing memory exhaustion from stale entries.

When to Use This Skill

  • Storing session-specific metadata that changes slowly
  • Caching user authorization headers across rapid sub-resource fetches
  • Reducing database load for frequent lookup tables
  • Aggregating repetitive API responses for a specific user ID

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • How do I share data between different API requests?
  • Implement an in-memory cache for repeated database queries
  • Stop querying the database for the same user profile repeatedly
  • Create a cache that survives beyond a single request cycle
  • Use LRU cache to reduce latency in my serverless functions

What this skill does

  • Retains data in-memory across sequential HTTP requests
  • Configures automated memory limits to prevent heap overflow
  • Sets Time-To-Live expiration to ensure data freshness
  • Supports arbitrary key-value storage for serializable objects
  • Automatic eviction of the least accessed items when cache capacity is reached

When not to use it

  • When your application runs in a stateless, ephemeral serverless environment without shared compute instances
  • When data must be consistent across multiple distributed server nodes or regions
  • When the cached data is highly sensitive and exceeds memory safe limits

Example workflow

  1. Initialize an LRU cache instance outside the handler function to persist state
  2. Define a wrapper function to check the cache for a specific key
  3. Perform a database fetch only on a cache miss
  4. Write the fetched results back to the cache before returning
  5. Verify the subsequent request retrieves the object directly from the cache

Prerequisites

  • node-lru-cache package installed
  • Persistent server execution environment

Pitfalls & limitations

  • !Cache poisoning if keys are not scoped correctly per user
  • !Memory bloat if the cache size limits are set too high
  • !Stale data if the TTL is significantly longer than the expected data change interval
  • !Data loss if the server instance restarts or scales down

FAQ

Does this replace Redis?
Only for single-instance compute. If you need shared state across multiple servers or persistent storage, you still require Redis.
What happens if the server process dies?
All cached data is lost. The application must be able to rebuild the cache from the primary database source.
How do I choose the max size?
Monitor your server's total available heap memory and allocate a small, safe percentage that accommodates your most frequent lookups.
Is this thread-safe?
In Node.js, your code runs on a single event loop, so the cache operations are safe as long as they are treated as non-blocking async operations.

How it compares

Generic caching solutions usually rely on external network calls to databases, whereas this pattern optimizes performance by utilizing the local server heap directly.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## Cross-Request LRU Caching

React.cache() only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.

**Implementation:**

import { LRUCache } from 'lru-cache'

const cache = new LRUCache<string, any>({
max: 1000,
ttl: 5 * 60 * 1000 // 5 minutes
})

export async function getUser(id: string) {
const cached = cache.get(id)
if (cached) return cached

const user = await db.user.findUnique({ where: { id } })
cache.set(id, user)
return user
}

// Request 1: DB query, result cached
// Request 2: cache hit, no DB query


Use when sequential user actions hit multiple endpoints needing the same data within seconds.

**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** LRU caching is especially effective because multiple concurrent requests can share the same function instance and cache. This means the cache persists across requests without needing external storage like Redis.

**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.

Reference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/server-cache-lru/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md
  • Cursor: ~/.cursor/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/vercel-labs/agent-skills/server-cache-lru/SKILL.md

🚀 Install with CLI:
npx skills add vercel-labs/agent-skills

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid server-side performance issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under Server-Side Performance and is published by Vercel Engineering, maintained in vercel-labs/agent-skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.