Skip to main content
This guide shows how to use Upstash Redis with Cloudflare Workers. The HTTP-based SDK is specifically designed for edge computing environments where traditional TCP connections aren’t available.

Why Upstash Redis for Cloudflare Workers?

  • Edge-native: HTTP-based connection perfect for Workers
  • Global low latency: Access Redis from 300+ cities worldwide
  • No cold starts: Instant execution at the edge
  • Automatic scaling: Handles traffic spikes seamlessly

Prerequisites

1

Create Cloudflare Account

Sign up for a Cloudflare account
2

Install Wrangler CLI

Install the Cloudflare Workers CLI:
3

Create Upstash Redis Database

Create a Redis database on Upstash Console

Quick Start

Project Setup

1

Clone Example or Create New Project

Or create a new project:
2

Install Dependencies

Example: Counter Worker

This example implements a simple counter that increments on each request.
Notice the import path: @upstash/redis/cloudflare - This is important for Cloudflare Workers compatibility.

Configuration

Add Environment Variables

You can configure environment variables in two ways:
wrangler.toml
1

Go to Workers Dashboard

2

Select Your Worker

Click on your Worker or create a new one
3

Add Environment Variables

Go to Settings > Variables and add:
  • UPSTASH_REDIS_REST_URL
  • UPSTASH_REDIS_REST_TOKEN

Option 3: Wrangler CLI Secrets (Most Secure)

Development

Run Locally

1

Start Development Server

2

Test Your Worker

Open your browser at localhost:8787

Deploy to Cloudflare

Your Worker will be deployed to Cloudflare’s global network and accessible via a *.workers.dev URL.

Advanced Examples

REST API with Routing

Edge Caching

Rate Limiting at the Edge

Session Management

TypeScript Support

For full TypeScript support, use the TypeScript template:
Define your environment interface:

Best Practices

Initialize Redis Client Inside Handler

Unlike traditional environments, Workers don’t maintain state between requests. Initialize the client in each request:

Error Handling

Use Edge Location Data

Cloudflare provides request context with location data:

Testing

Local Testing with Wrangler

Test endpoints:

Unit Testing

For unit testing Workers, see Cloudflare Workers testing documentation.

Monitoring

View Logs

Or view logs in the Cloudflare Dashboard.

Add Custom Logging

Troubleshooting

Make sure you’re using the correct import path:
Not:
Ensure variables are:
  1. Set in wrangler.toml [vars] section, OR
  2. Set in Cloudflare Dashboard, OR
  3. Set as secrets via wrangler secret put
Access them via the env parameter:
Check your wrangler.toml configuration:
  • name must be unique
  • main must point to your entry file
  • compatibility_date should be recent

Next Steps

AWS Lambda

Deploy Redis with AWS Lambda

Next.js

Integrate Redis with Next.js

Basic Usage

Learn more Redis operations

Cloudflare Docs

Cloudflare Workers documentation