---
name: redis-django
description: "Provisions a Redis database (Upstash-compatible, REST and TCP) for a Django app with `npx extraorbital add redis` (no signup, no card) and wires it into Django with `redis`. Use when a Django project needs Redis, a cache, rate limiting, sessions, a job queue, Upstash, Valkey or REDIS_URL, or when REDIS_URL is missing or unset in .env."
---

# Redis in Django

<!-- Generated from ExtraOrbital's service registry by `pnpm skills:build` in platform/app. Edit the generator, not this file. -->

ExtraOrbital provisions a Redis database (Upstash-compatible, REST and TCP) in one command and writes the credentials into the project's env file. For anything this recipe does not cover, use the `extraorbital` skill or https://extraorbital.dev/llms.txt.

## 1. Provision

```bash
npx extraorbital add redis
```

Run it from the project root. It is idempotent (a second run returns the same resource) and writes to `.env.local` if the project has one, otherwise `.env`, keeping that file out of git. Add `--json` to parse the result; credential values are redacted there, and you should never print them either.

| Variable | |
| --- | --- |
| `UPSTASH_REDIS_REST_URL` | REST endpoint, for serverless and edge runtimes |
| `UPSTASH_REDIS_REST_TOKEN` | Its token |
| `REDIS_URL` | `rediss://` URL for any Redis-protocol client |

Free on the Prototype plan: 256 MB, 500K commands/mo.

## 2. Install the client

```bash
pip install python-dotenv redis
```

## 3. Connect

Django reads no env file by itself: `settings.py` loads it with `python-dotenv`, below `BASE_DIR`.

**`yourproject/settings.py`**

```python
import os

from dotenv import load_dotenv

# ExtraOrbital writes .env.local when the project has one, else .env.
# load_dotenv never overrides a variable already set, so the first file wins.
load_dotenv(BASE_DIR / ".env.local")
load_dotenv(BASE_DIR / ".env")

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

## 4. Use it

**`yourapp/views.py`**

```python
from django.core.cache import cache
from django.http import JsonResponse

# yourapp/urls.py: path("api/redis/", views.check_redis)


def check_redis(request):
    cache.add("hits", 0)
    hits = cache.incr("hits")
    return JsonResponse({"hits": hits})
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
python manage.py runserver
curl http://localhost:8000/api/redis/
```

A JSON answer means the credentials, the client and the route all work. Delete the route afterwards if the app does not need it.

## Pitfalls

- Both ways in reach the same database: the `UPSTASH_REDIS_REST_*` pair works with the `upstash-redis` package where a TCP connection is not an option.
- Set an expiry on cache keys (`ex`/`EX`): the Prototype allowance is 256 MB and 500K commands a month, across the account.
- `yourapp` and `yourproject` stand for the project's own app and settings package.
- Read credentials once in `settings.py` and import `django.conf.settings` elsewhere; `os.environ[...]` fails at startup when one is missing, which is the point.
- Exit code 3 means the account is past its free allowance: show the human the link the command printed and wait. Do not retry or work around it.

Docs: https://extraorbital.dev/docs/resources/redis.md
