Does Redis Work With Cloudflare Pages?
You can use Redis with Cloudflare Pages, but only through external services—Pages functions cannot run Redis directly due to serverless constraints.
Quick Facts
How Redis Works With Cloudflare Pages
Cloudflare Pages is a static site generator and serverless function platform without persistent storage on the edge. You cannot run a Redis instance directly on Pages, but you can connect your Pages Functions to an external Redis instance—either a managed service like Upstash, Redis Cloud, or a self-hosted Redis server. Your Pages Functions execute in Cloudflare's Workers runtime, which supports outbound connections via fetch APIs. This means your serverless functions can authenticate and make requests to your Redis instance for caching, sessions, rate limiting, or real-time data. The architecture involves deploying your static assets to Pages while routing API calls from your frontend through Pages Functions that talk to your external Redis backend. Latency depends on your Redis provider's location relative to Cloudflare's edge network—Upstash offers geo-distributed replicas specifically optimized for this pattern. For high-traffic applications, this works well; for ultra-low-latency needs, consider Cloudflare's Durable Objects as a Redis alternative.
Best Use Cases
Quick Setup
npm install redisimport { createClient } from 'redis';
export const onRequest: PagesFunction = async (context) => {
const client = createClient({
url: context.env.REDIS_URL
});
try {
await client.connect();
const cached = await client.get('user:123');
if (cached) {
return new Response(cached, { status: 200 });
}
const data = JSON.stringify({ id: 123, name: 'John' });
await client.setEx('user:123', 3600, data);
return new Response(data, { status: 200 });
} finally {
await client.quit();
}
};Known Issues & Gotchas
Cold starts and connection overhead add latency to every request
Fix: Use connection pooling, keep Redis connections alive, or cache frequently accessed data in Cloudflare KV for faster edge access
Cloudflare Functions have execution timeout limits (10-50ms depending on plan), making long-running Redis operations risky
Fix: Use Redis pipelining and multi-command batching; implement timeouts on your client library; consider Durable Objects for persistent connections
Data in external Redis is not replicated to Cloudflare's edge, so every request traverses the internet to reach it
Fix: Use Upstash with Cloudflare integration for optimized routing, or use Durable Objects for workload that needs edge-local state
Costs multiply: you pay for Cloudflare Pages invocations AND Redis service tier separately
Fix: Monitor usage; consider if Durable Objects or KV alone would be more cost-effective for your access patterns
Alternatives
- •Cloudflare Durable Objects + Workers (persistent, edge-local state without external Redis)
- •Cloudflare KV + Pages Functions (simpler caching, eventual consistency)
- •Vercel Edge Functions + Upstash Redis (similar architecture with different edge provider)
Resources
Related Compatibility Guides
Explore more compatibility guides