Runtime Cache
Runtime Cache
Runtime Cache is available on all plans.
Runtime cache is a regional, ephemeral cache you can use for storing and retrieving data across Vercel Functions, Routing middleware, and build execution within a Vercel region. It lets you cache data close to where your code runs, reduce duplicate work, and control invalidation with TTLs and tags.
Runtime cache may not share the same cache between build time and runtime depending on whether the region where the build executed matches the runtime region.
- Find out how runtime cache works
- When to use it
- Get started with the framework-specific examples
For caching complete HTTP responses (entire pages, API responses) in Vercel regions, see CDN cache. For caching build artifacts, see Remote cache.
When to use runtime cache
Runtime cache is best when your functions fetch the same data multiple times or perform expensive computations that can be reused, such as in the following scenarios:
- API calls that return the same data across multiple requests
- Database queries that don't change frequently
- Expensive computations you want to reuse
- Data fetching in server components or API routes
Runtime cache is not a good fit for:
- User-specific data that differs for each request
- Data that must be fresh on every request
- Data that must be retained for its full TTL (entries may be evicted sooner if the cache reaches its storage limit)
- Complete HTTP responses (use CDN cache instead)
How runtime cache works
Runtime cache stores data in a non-durable cache close to where your function executes. Each region where your function runs has its own cache, allowing reads and writes to happen in the same region for low latency. It has the following characteristics:
- Regional: Each region has its own cache
- Isolated by environment: Each deployment environment (
previewandproduction) uses its own cache, so they never share cached data - Scoped by plan: On Pro and Enterprise, each project uses its own cache. On Hobby, all projects in your team share a single cache. See storage scope by plan
- Persistent across deployments: Cached data persists across deployments and can be invalidated through time-based expiration or by calling
expireTag - Ephemeral: Each cache has a storage limit. When a cache reaches this limit, Vercel evicts (removes) the entries that haven't been accessed recently to free up space for new entries
- Automatic: When runtime cache is enabled, Vercel handles caching for you
- Framework-agnostic: Works with all frameworks
The cache sits between your function and your data source, reducing the need to repeatedly fetch the same data. See limits and usage for information on item size, tags per item, and maximum tag length.
Using runtime cache
You can cache your Vercel function with any framework by using the functions of the helper method getCache.
Runtime cache with any framework
This example caches data fetched from the API so that it expires after 1 hour and adds a tag to the cache entry so you can invalidate it later from code:
import { getCache } from '@vercel/functions';
export default {
async fetch(request) {
const cache = getCache();
// Get a value from cache
const value = await cache.get('somekey');
if (value) {
return new Response(JSON.stringify(value));
}
const res = await fetch('https://api.vercel.app/blog');
const originValue = await res.json();
// Set a value in cache with TTL and tags
await cache.set('somekey', originValue, {
ttl: 3600, // 1 hour in seconds
tags: ['example-tag'],
});
return new Response(JSON.stringify(originValue));
},
};
Next.js 16 and above
With Next.js 16, you can cache data at runtime in two ways:
use cache: remote: A directive that caches entire functions or components with Runtime cache. Requires enablingcacheComponentsin your config.fetchwithforce-cache: Caches individual fetch requests without additional configuration with Data cache.
Using use cache: remote
Use the use cache: remote directive at the file, component, or function level to cache the output of a function or component.
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
cacheComponents: true,
};
export default nextConfig;
Then, use the use cache: remote directive in your code. This example caches data so that it expires after 1 hour and adds a tag to the cache entry so you can invalidate it later from code:
import { cacheLife, cacheTag } from 'next/cache';
export default async function Page() {
const data = await getData();
return (
<main>
<h1>Data</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</main>
);
}
async function getData() {
'use cache: remote'
cacheTag('example-tag')
cacheLife({ expire: 3600 }) // 1 hour
const response = await fetch('https://api.example.com/data');
return response.json();
}
Revalidation
You can control how long data stays cached using the following revalidation options:
Time-based revalidation
This example revalidates the cache every hour:
import { cacheLife } from 'next/cache';
export async function GET() {
const data = await getProducts();
return Response.json(data);
}
async function getProducts() {
'use cache: remote'
cacheLife({ expire: 3600 }) // 1 hour
const response = await fetch('https://api.example.com/products');
return response.json();
}
Limits and usage
| Runtime Cache property | Limit |
|---|---|
| Item size | 2 MB |
| Tags per item | 128 tags |
| Maximum tag length | 256 bytes |
TTL and tag updates aren't reconciled between deployments. If you need to update cache behavior after a deployment, purge the runtime cache or modify the cache key.