Use Upstash Redis With Django
Use Upstash serverless Redis with Django for caching, sessions, rate limiting, and Celery: connect over rediss:// with TLS, understand per-request pricing and connection limits, choose regions, and know when the REST API suits serverless deployments better.
Problem statement
Your Django app runs on a platform without its own Redis (Vercel, Fly.io, Cloud Run, a small VPS), and you need a cache, sessions, rate limiting, or a Celery broker without operating a server. Upstash offers Redis-compatible databases billed per request with a free tier, reachable over TLS from anywhere. Django connects to it like any Redis, but per-request pricing and connection behavior change how you should use it, especially for Celery.
- Create a database in the region closest to your app, and copy the
rediss://URL. - Set
REDIS_URLand use Django'sRedisCachebackend; TLS verification works with standard CA certificates. - Use it for caching, sessions, and rate limits, where command volume is predictable.
- For Celery, expect many polling commands; check costs or use a fixed-price plan before relying on it as a broker.
- In serverless functions, keep connections short-lived or use the Upstash REST API instead of TCP connections.
Step-by-step solution
1. Create the database
In the Upstash console, create a Redis database and choose a primary region near your app (for example the same AWS region as your Vercel functions or Fly.io region). Enable eviction if you use it as a cache. Copy the connection string:
rediss://default:<password>@<name>.upstash.io:6379
2. Configure Django
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": os.environ["REDIS_URL"],
"TIMEOUT": 300,
"KEY_PREFIX": "shop",
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
cached_db sessions keep a copy in the database, so a cache outage or eviction does not log users out.
3. Mind TLS and latency
Upstash requires TLS. Its certificates come from a public CA, so default verification works; make sure the container image has ca-certificates. Each command is a network round trip; from a different region, that is tens of milliseconds. Prefer a few larger cache entries over many small get calls per request, and use get_many where possible.
4. Celery with Upstash
Celery's Redis transport polls and uses blocking commands, heartbeats, and pub/sub, which generate a steady stream of commands even when idle. On per-request pricing, that can cost more than expected. If you use Upstash as a broker:
CELERY_BROKER_URL = os.environ["REDIS_URL"] + "?ssl_cert_reqs=CERT_REQUIRED"
CELERY_BROKER_TRANSPORT_OPTIONS = {"polling_interval": 5}
CELERY_WORKER_SEND_TASK_EVENTS = False
CELERY_BROKER_HEARTBEAT = 0
Disable events and lower background chatter, monitor the command count in the Upstash console, and consider a fixed-price plan. Upstash also offers QStash, an HTTP message queue, which some serverless Django apps use instead of Celery.
5. Serverless functions
On Vercel or AWS Lambda, each cold start opens a new TCP connection. With many concurrent invocations, connection counts climb. The Upstash REST API (via the upstash-redis Python client) uses HTTPS per command and avoids persistent connections; use it in functions for simple key operations, and the standard Redis protocol from long-running servers.
Explanation
Upstash runs Redis-compatible storage behind a multi-tenant proxy, charging per command rather than per server, and persisting data to durable storage. That model is ideal for intermittent or spiky workloads and for platforms without private networking. Because it is reached over the internet with TLS, latency and per-command cost matter more than with a Redis on the same network, which shapes how heavily you lean on it.
Edge cases / notes
- Global databases. Read replicas in several regions lower read latency, but writes go to the primary region.
- Command limits. Plans have limits on request rate, data size, and max entry size; check them for large cached pages.
- Lua and transactions. Most commands, including Lua scripts used by some rate limiters, are supported; check Upstash's compatibility list for edge cases.
Next steps
- Configure caching patterns with Set Up Redis Cache for Django in Production.
- Fix TLS errors with Fix Redis SSL CERTIFICATE_VERIFY_FAILED.
- Running on Vercel? See Deploy Django on Vercel.
FAQ
Does Django's Redis cache work with Upstash?
Yes. Use the rediss:// URL with Django's RedisCache backend.
Is Upstash good as a Celery broker?
It works, but Celery's background commands can raise costs on per-request pricing. Check usage or use a fixed plan.
Do I need to disable TLS verification?
No. Upstash uses certificates from a public CA.
What is the Upstash REST API for?
For environments like serverless functions where persistent TCP connections are a problem.