> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sevalla.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> These are the docs for Sevalla, a platform for deploying applications, databases, static sites, object storage and load balancers. The dashboard is at https://app.sevalla.com.
> Bold text in steps is an exact dashboard label.
> To act on a Sevalla account, prefer the Sevalla MCP server (https://mcp.sevalla.com/mcp, see https://docs.sevalla.com/ai-agents/overview.md), then the Sevalla CLI (https://docs.sevalla.com/developer-tools/cli.md), then the REST API (https://api-docs.sevalla.com, OpenAPI spec at https://api.sevalla.com/v3/openapi.json).
> Every page index is at https://docs.sevalla.com/llms.txt.

# Cache Django with Redis

> Use a Sevalla Redis database as Django's cache backend over an internal connection, cache views with cache_page, and control the CDN and edge cache with Cache-Control.

Django's cache framework keeps computed views and values in a cache backend. On Sevalla, the backend is a Redis database that the application reaches over an internal connection, so every instance of the web process shares one cache. Django includes the Redis backend: it needs only the `redis` package.

## At a glance

This guide adds to the application from [Deploy a Django application](https://docs.sevalla.com/guides/python/django/deploy.md#at-a-glance):

| | |
| - | - |
| Services | A Redis or Valkey database over an internal connection, next to the PostgreSQL database |
| Size | **DB1** for the Redis database |

### Environment variables

| Variable | Required | Available during | Secret | Value |
| - | - | - | - | - |
| `REDIS_URL` | Yes | Build and runtime | Yes | Added by the internal connection to `storefront-redis`: `redis://:<password>@<host>:<port>/0` |

The internal connection also adds `REDIS_HOST`, `REDIS_PORT` and `REDIS_PASSWORD`, which the project doesn't read. The deploy guide lists the [other variables](https://docs.sevalla.com/guides/python/django/deploy.md#environment-variables).

## Before you begin

* A Django project deployed on Sevalla, as in [Deploy Django](https://docs.sevalla.com/guides/python/django/deploy.md).

## Create a Redis database

**1. Create the database**

In the sidebar, click **Databases**, then click **Create database**. Under **Database**, choose **Redis**, or **Valkey**: the code in this guide works with both. Enter `storefront-redis` as the **Display name**, choose the same **Data center** as the application and a **Size**, then click **Create database**.

**2. Connect it to the application**

In the sidebar, click **Applications**, then click the application. Click the **Networking** tab. Under **Connected services**, click **Add internal connection**. Choose `storefront-redis` as the **Service**, turn on **Add environment variables to the application**, and click **Add internal connection**. Sevalla adds `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD` and `REDIS_URL`. See [Connect an application](https://docs.sevalla.com/databases/connect.md#connect-an-application).

![The Add internal connection sheet with the Service field and the Add environment variables to the application switch](https://assets.sevalla.com/screenshots/light/app-networking-internal-connection-add.png)

## Configure the cache

Install redis-py and update `requirements.txt`:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
pip install redis
pip freeze > requirements.txt
```

In `storefront/settings.py`, use Django's Redis backend with the URL from the internal connection:

```python storefront/settings.py theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
# A Valkey URL added before Oct 30, 2026 starts with valkey://, which redis-py doesn't accept.
REDIS_URL = env("REDIS_URL", default="redis://localhost:6379/0").replace("valkey://", "redis://", 1)

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": REDIS_URL,
    }
}
```

The same setting serves `cache_page`, template fragment caching (`{% cache %}`) and the low-level API (`django.core.cache.cache`).

## Cache a view

Cache a view's response for 2 minutes with `cache_page`:

```python storefront/views.py theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
from django.http import HttpResponse
from django.utils import timezone
from django.views.decorators.cache import cache_page


@cache_page(60 * 2)
def cached(request):
    return HttpResponse(f"Generated at {timezone.now():%H:%M:%S}")
```

```python storefront/urls.py theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
from django.contrib import admin
from django.urls import path

from storefront.views import cached

urlpatterns = [
    path("admin/", admin.site.urls),
    path("cached/", cached),
]
```

Commit and push the changes, `requirements.txt` included: Ingot installs `redis` on the next build. If **Auto-deploy** is off, click **Deploy now** on the application's **Overview** tab.

## Verify

When the deployment's status is **Deployed**, request the page twice, with your application's domain:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
curl https://storefront-x7k2m.sevalla.app/cached/
curl https://storefront-x7k2m.sevalla.app/cached/
```

Both responses show the same time, for example `Generated at 16:08:48`, and carry `Cache-Control: max-age=120`. After 2 minutes, the next request shows a new time. To see both in one run:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
curl -s https://storefront-x7k2m.sevalla.app/cached/; echo
curl -s -D - -o /dev/null https://storefront-x7k2m.sevalla.app/cached/ | grep -i '^cache-control'
curl -s https://storefront-x7k2m.sevalla.app/cached/; echo
```

```text theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
Generated at 16:08:48
cache-control: max-age=120
Generated at 16:08:48
```

To check the cache itself, run a command in the web process with [Run a command in a process](https://api-docs.sevalla.com/v3/applications/run-a-command-in-a-process) (`execProcessCommandV3`), with `APP_ID` and `WEB_ID` from [Deploy with the API](https://docs.sevalla.com/guides/python/django/deploy.md#deploy-with-the-api):

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
curl -s -X POST "$API/applications/$APP_ID/processes/$WEB_ID/exec" \
  -H "Authorization: Bearer $SEVALLA_API_KEY" -H "Content-Type: application/json" \
  -d '{"command": ["python", "manage.py", "shell", "-v", "0", "-c", "from django.core.cache import cache; cache.set(\"check\", \"ok\"); print(cache.get(\"check\"))"]}'
```

```json theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
{"stdout":"ok\n","stderr":"","exit_code":0}
```

To look at the keys, open the database and click the **Redis studio** tab: `cache_page` stores keys whose names start with `:1:views.decorators.cache.cache_page`. See [Database studio](https://docs.sevalla.com/databases/studio.md).

## Control the CDN and edge cache

Sevalla can also cache responses at Cloudflare's edge, in front of the application. Turn on the CDN and edge caching on the application's **Networking** tab; see [CDN and edge caching](https://docs.sevalla.com/applications/caching.md).

* The CDN caches static files by file extension, such as `.css`, `.js` and images. WhiteNoise's hashed file names make them safe to cache for a long time.
* Edge caching caches pages and other responses too. It caches only `GET` requests, skips requests with an `Authorization` header, and doesn't store responses that set a cookie, such as pages that start a session or set the CSRF cookie.
* When a response has no `Cache-Control` header, Sevalla adds one. With edge caching on, a `200` response without its own header can stay cached for up to 30 days, unless a deployment or a purge clears the cache first. See [Control caching from your application](https://docs.sevalla.com/applications/caching.md#control-caching-from-your-application).

So with edge caching on, set `Cache-Control` on every view. `never_cache` keeps a page out of every cache, and `cache_control` sets how long it may be kept:

```python storefront/views.py theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
from django.views.decorators.cache import cache_control, never_cache


@never_cache
def account(request):
    ...


@cache_control(public=True, max_age=600)
def catalog(request):
    ...
```

Django's admin views already use `never_cache`. To check what a page sends, run `curl -sI https://storefront-x7k2m.sevalla.app/catalog/` and read the `Cache-Control` header.

## Deploy with the API

This sequence adds the Redis database to the application from [Deploy with the API](https://docs.sevalla.com/guides/python/django/deploy.md#deploy-with-the-api) and continues in its shell, with `API`, `CLUSTER_ID`, `DB1_ID` and `APP_ID` set. Push the code changes from [Configure the cache](#configure-the-cache) and [Cache a view](#cache-a-view) first. Each step names the [CLI](https://docs.sevalla.com/developer-tools/cli.md) command and the [Sevalla MCP server](https://docs.sevalla.com/ai-agents/overview.md) tool that do the same.

**1. Look up the Redis version**

[List database versions](https://api-docs.sevalla.com/v3/resources/list-database-versions) (`listDatabaseVersionsV3`) returns the versions the location offers, oldest first. Save the newest:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
REDIS_VERSION=$(curl -s "$API/resources/database-versions?cluster_id=$CLUSTER_ID&type=redis" \
  -H "Authorization: Bearer $SEVALLA_API_KEY" | jq -r '.[0].versions[-1]')
```

CLI: `sevalla resources db-versions --cluster "$CLUSTER_ID" --type redis`. MCP: not needed, `create_database` takes the newest version by default.

**2. Create the database**

[Create database](https://api-docs.sevalla.com/v3/databases/create-database) (`createDatabaseV3`). You choose the password, and the API never returns it, so keep it for the connection URL:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
REDIS_PASSWORD=$(openssl rand -hex 16)
REDIS_ID=$(curl -s -X POST "$API/databases" \
  -H "Authorization: Bearer $SEVALLA_API_KEY" -H "Content-Type: application/json" \
  -d "$(jq -n --arg cluster "$CLUSTER_ID" --arg size "$DB1_ID" --arg version "$REDIS_VERSION" --arg password "$REDIS_PASSWORD" \
    '{display_name: "storefront-redis", type: "redis", version: $version, cluster_id: $cluster,
      resource_type_id: $size, db_name: "storefront", db_password: $password}')" \
  | jq -r .id)
```

CLI: `sevalla databases create --name storefront-redis --type redis --db-version "$REDIS_VERSION" --cluster "$CLUSTER_ID" --resource-type "$DB1_ID" --db-name storefront --db-password "$REDIS_PASSWORD"`. MCP: `create_database` with `type: "redis"`; with `connect_to_application_id` and `env_var_name: "REDIS_URL"`, it also does the internal connection and adds `REDIS_URL`.

**3. Wait for the database**

[Get database](https://api-docs.sevalla.com/v3/databases/get-database) (`getDatabaseV3`) shows `status` `ready` when the database is up, with its internal hostname and port:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
for i in $(seq 1 60); do
  [ "$(curl -s "$API/databases/$REDIS_ID" -H "Authorization: Bearer $SEVALLA_API_KEY" | jq -r '.status // empty')" = ready ] && break
  sleep 10
done
REDIS=$(curl -s "$API/databases/$REDIS_ID" -H "Authorization: Bearer $SEVALLA_API_KEY")
REDIS_HOST=$(echo "$REDIS" | jq -r .internal_hostname)
REDIS_PORT=$(echo "$REDIS" | jq -r .internal_port)
```

CLI: `sevalla databases get "$REDIS_ID"`. MCP: `get_database`.

**4. Connect the database**

[Create internal connection](https://api-docs.sevalla.com/v3/applications/create-internal-connection) (`createApplicationInternalConnectionV3`):

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
curl -s -X POST "$API/applications/$APP_ID/internal-connections" \
  -H "Authorization: Bearer $SEVALLA_API_KEY" -H "Content-Type: application/json" \
  -d "$(jq -n --arg db "$REDIS_ID" '{target_id: $db, target_type: "database"}')" | jq -r .id
```

The API adds no environment variables; the next step adds them. CLI: `sevalla apps internal-connections create "$APP_ID" --target-id "$REDIS_ID" --target-type database`. MCP: `sevalla_api_create` with `createApplicationInternalConnectionV3` and the same body.

**5. Add the environment variables**

[Create environment variable](https://api-docs.sevalla.com/v3/applications/create-environment-variable) (`createEnvironmentVariableV3`), once per variable, with the names and format of the dashboard's internal connection:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
for pair in "REDIS_HOST=$REDIS_HOST" "REDIS_PORT=$REDIS_PORT" "REDIS_PASSWORD=$REDIS_PASSWORD" \
  "REDIS_URL=redis://:$REDIS_PASSWORD@$REDIS_HOST:$REDIS_PORT/0"; do
  curl -s -X POST "$API/applications/$APP_ID/env-vars" \
    -H "Authorization: Bearer $SEVALLA_API_KEY" -H "Content-Type: application/json" \
    -d "$(jq -n --arg key "${pair%%=*}" --arg value "${pair#*=}" '{key: $key, value: $value, is_runtime: true, is_buildtime: true}')" \
    | jq -r .key
done
```

CLI: `sevalla apps env-vars create "$APP_ID" --key REDIS_URL --value "redis://:$REDIS_PASSWORD@$REDIS_HOST:$REDIS_PORT/0"`, once per variable. MCP: `set_environment_variables`.

**6. Deploy**

[Trigger deployment](https://api-docs.sevalla.com/v3/applications/trigger-deployment) (`triggerDeploymentV3`) deploys the latest commit with the Redis variables. With **Auto-deploy** on, the push already started a deployment, without them; this one queues behind it:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
DEPLOYMENT_ID=$(curl -s -X POST "$API/applications/$APP_ID/deployments" \
  -H "Authorization: Bearer $SEVALLA_API_KEY" -H "Content-Type: application/json" -d '{}' | jq -r .id)
```

CLI: `sevalla apps deployments trigger "$APP_ID"`. MCP: `trigger_deployment`.

**7. Wait for the deployment**

[Get deployment](https://api-docs.sevalla.com/v3/applications/get-deployment) (`getDeploymentV3`) reports the `status`, which ends as `success`, `failed`, `cancelled` or `skipped`. The loop keeps polling through a failed request, for up to 30 minutes:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-dark-default"}}
for i in $(seq 1 120); do
  STATUS=$(curl -s "$API/applications/$APP_ID/deployments/$DEPLOYMENT_ID" \
    -H "Authorization: Bearer $SEVALLA_API_KEY" | jq -r '.status // empty')
  case $STATUS in success|failed|cancelled|skipped) break ;; esac
  sleep 15
done
echo "$STATUS"
```

CLI: `sevalla apps deployments get "$DEPLOYMENT_ID" --app-id "$APP_ID"`. MCP: `get_deployment`.

Then run the checks in [Verify](#verify), with your application's domain.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.