Docs
Python
valkey-py, asyncio with FastAPI, Django cache and Celery.
Each example reads the connection URL from VALKEY_URL; copy it from your instance page in the dashboard. See connection details and limits for what applies to every client.
On this page: valkey-py · FastAPI (asyncio) · Django cache · Celery
valkey-py
The official Valkey client, a fork of redis-py with the same API. redis-py works the same way, with Redis and Valkey instances alike: import redis instead.
Install
pip install valkeymain.py
import os import valkey client = valkey.from_url(os.environ["VALKEY_URL"], decode_responses=True) client.set("greeting", "hello", ex=60)print(client.get("greeting"))FastAPI (asyncio)
Open one async client for the app's lifetime and share it between requests.
Install
pip install fastapi uvicorn redismain.py
import osfrom contextlib import asynccontextmanager from fastapi import FastAPIfrom redis.asyncio import Redis @asynccontextmanagerasync def lifespan(app: FastAPI): app.state.valkey = Redis.from_url(os.environ["VALKEY_URL"], decode_responses=True) yield await app.state.valkey.aclose() app = FastAPI(lifespan=lifespan) @app.get("/hits")async def hits(): return {"hits": await app.state.valkey.incr("hits")}Django cache
Django 4.0+ ships a Redis cache backend; it only needs redis-py.
Install
pip install redissettings.py
import os CACHES = { "default": { "BACKEND": "django.core.cache.backends.redis.RedisCache", "LOCATION": os.environ["VALKEY_URL"], "KEY_PREFIX": "myapp", }} # Optional: keep sessions in the cache too.SESSION_ENGINE = "django.contrib.sessions.backends.cache"Celery
Celery refuses a rediss:// URL unless it states how to check the certificate, so add ssl_cert_reqs=required.
Install
pip install "celery[redis]"tasks.py
import os from celery import Celery url = os.environ["VALKEY_URL"] + "?ssl_cert_reqs=required" app = Celery("tasks", broker=url, backend=url)app.conf.broker_transport_options = {"health_check_interval": 30} @app.taskdef add(x, y): return x + yStarter evicts keys when memory is full (allkeys-lru), which can silently drop jobs. Use it for experiments; production queues need a persistent plan with noeviction (Casual or Pro, coming soon).