---
title: "Add a Redis cache over MCP with while.sh"
url: https://while.sh/en/guides/add-redis-cache/
author: "Aloïs Oddon, co-founder of Nextmap"
updated: 2026-11-02
---

# Add a Redis-compatible cache to your app, from your AI agent

Is your app slowing down under load? Ask your agent for a cache: connected to while over MCP, it declares one in three lines, and your app finds it in REDIS_URL.

## Prerequisites

- A Node.js, Python or Go app.
- An AI agent that works in a terminal: Claude Code, Codex, Cursor or Gemini CLI.

## Step 1: Give your agent these instructions

Open your agent in your app's folder, and paste these instructions into it:

```text titre="Instructions"
Connect to while with my account (while.sh/agents), then deploy my app.
```

Your agent adds while's MCP server itself. If it asks you to, restart it. On the first call to while, a page opens in your browser. In Claude Code, if it doesn't open, type /mcp, choose while, then Authenticate. On that page, choose "A new app", then click Authorize.

## Step 2: Open your app

Your agent gives you your app's address, for example `https://mon-appli.while.cloud`, over HTTPS.

## What your agent does

It adds three lines to while.yaml:

```yaml titre="while.yaml"
apiVersion: while/v1
project: mon-appli
services:
  - name: web
    port: 3000
    health:
      path: /health
caches:
  - name: cache
    memory: 256Mi
```

The last three lines declare a 256 MiB Redis-compatible cache. Its address, password included, reaches your app in `REDIS_URL` (source 1):

```js titre="server.js"
import { createClient } from 'redis';

const cache = createClient({ url: process.env.REDIS_URL });
await cache.connect();
```

## Make the cache bigger

> You, to your agent: Increase the cache to 512 MiB.

Your agent changes `memory` in while.yaml. When the cache is full, the keys that have gone unused the longest make room for new ones.

## Keep the data after a restart

> You, to your agent: Turn the cache into a job queue that never loses anything.

Your agent makes the cache durable: its data is written to disk, and no key is evicted.

## Add a database

> You, to your agent: Add a PostgreSQL database.

Your agent declares the database in while.yaml, and its address reaches your code in `DATABASE_URL`. It's backed up every day, and can go back to a precise moment if needed.

## Restrict your app to your colleagues

> You, to your agent: Restrict the app to people in the company.

Your agent adds two lines to the service:

```yaml titre="while.yaml"
    access: internal
    email_domains: [acme.com]
```

Once your Google Workspace or Microsoft 365 is linked to while, on the Pro plan, your colleagues open it with their usual account, and your code receives their name and email address, with no sign-in page to write.

## Go back to the previous version

> You, to your agent: Go back to the previous version.

In the 30 minutes after a deployment, the previous version comes back in a second. After that, it takes a few seconds. Once the previous version is back, your agent reads what broke in while's logs, fixes it and deploys again.

## Next steps

- [Deploy a Node.js API and its PostgreSQL database](/en/guides/deploy-node-api-postgresql/)
- [Run a scheduled job](/en/guides/run-cron-job/)
- [Compare the hosts that have an MCP server](/en/comparisons/mcp-hosting-claude-code/)

## Sources

1. [Redis, connecting a Node.js app with the redis package (createClient and its url option)](https://redis.io/docs/latest/develop/clients/nodejs/connect/), 2026-11-02

## Questions

### Is it really Redis?

It's Valkey, which is Redis-compatible: any Redis client connects to it as is.

### Is the cache protected?

Yes, by a password, included in REDIS_URL.

### Can I use it for a job queue?

Yes, with a durable cache: nothing is lost on restart, and no key is evicted.
