Async in Django 5: a real speed-up
For a long time Django was associated with synchronous request-response handling and WSGI.
With ASGI and the release of Django 5.0, we can finally build truly asynchronous
services. But simply writing async def in your views will not give you a speedup
if the architecture is not ready for concurrency.
Practice shows that the teams that win are the ones that methodically move their stack to ASGI, update their drivers and build up observability, rather than just adding a couple of asynchronous endpoints. Below is a proven set of steps and patterns.
When async is justified
- High latency between services: external APIs, queues, databases.
- Streaming data to real-time interfaces (websocket, SSE).
- Heavy work with files and storage.
- Microservices with a large number of concurrent but short I/O operations.
If the bottleneck is CPU-bound work, it is better to move it into Celery background tasks or Rust services.
Moving to ASGI without the pain
- Update Django to 5.x and set
ASGI_APPLICATIONin your settings. - Switch to
uvicornorhypercornand drop the legacy WSGI layer. - Replace your drivers:
psycopg3inasyncmode,asyncpg,redis.asynciofrom redis-py (aioredis was merged into redis-py and is no longer developed separately). - Check your middleware: synchronous classes need to be wrapped in
sync_to_asyncor rewritten. - Rebuild your deployment pipeline and health checks to account for the new worker.
This plan lets you move a project to ASGI iteratively, without breaking existing synchronous applications and the admin.
A typical architecture
We combine Django + PostgreSQL + Redis + a FastAPI worker (we wrote separately about DI and testing in FastAPI). Django is responsible for
business logic and the admin, while strong async views cover the integrations. The communication channel is Redis Streams, so that it does not get in the way of synchronous code. Separate workers serve
websocket connections via channels, and SSE streams can be served
through StreamingHttpResponse.
client → ASGI → Django async view → external APIs → response
↘ task in Celery/FastAPI
Thanks to this, you can serve up to 4-5x more connections with the same number of pods.
Whether to split the backend into services is a separate question: we wrote about why a clean monolith is often better than microservices.
Patterns and anti-patterns
- Delegate heavy HTTP calls to adapter services with timeouts and retries.
- Avoid mixing
syncandasyncORM queries within a single request. - Control the size of your connection pools so you do not block PostgreSQL.
- Watch out for access to global objects: async exposes race conditions.
An example of an async view
from django.http import JsonResponse
from .models import Author
async def author_featured_book(request, author_id: int):
author = await Author.objects.select_related("profile").aget(id=author_id)
book = await author.books.filter(is_featured=True).afirst()
if book is None:
return JsonResponse({"author": author.name, "book": None}, status=404)
reviews = [
{"rating": review.rating, "comment": review.comment}
async for review in book.reviews.order_by("-rating").aiterator(chunk_size=5)
]
return JsonResponse(
{
"author": author.name,
"book": {
"title": book.title,
"published_at": book.published_at,
"rating": book.rating,
},
"top_reviews": reviews,
}
)
Here we use the full asynchronous ORM: aget, afirst and aiterator
remove the need for sync_to_async, reducing overhead and making I/O handling truly
non-blocking.
Observability and testing
- Cover key endpoints with
pytest-asynciotests and usehttpx.AsyncClient. - Hook up OpenTelemetry or Sentry Performance to catch blocking calls.
- Expose metrics on event loop activity: the number of active tasks, queue sizes.
- Replay production traffic with
locustork6to find bottlenecks.
Without telemetry, an asynchronous architecture turns into a black box: it is critical to see where a request has "got stuck".
A mini benchmark
| Configuration | RPS (p95) | Latency |
|---|---|---|
| WSGI + gunicorn | 480 | 210 ms |
| ASGI + uvicorn 4 workers | 1820 | 78 ms |
| ASGI + uvicorn + asyncpg | 2130 | 65 ms |
These are results from our pilot (order processing and CRM integrations). The main thing is not to forget to enable keep-alive and tune the workers.
Practical tips
- In production, run
gunicornwithuvicorn.workers.UvicornWorker: this is the standard way to deploy;uvicornorhypercorncan also be used directly. - Use
asyncpgandpsycopg3for the database to avoid blocking. - Watch the context: do not mix synchronous and asynchronous ORM queries.
- Look for third-party libraries that already support
asyncinstead of writing manual wrappers. - Introduce timeouts and cancellation:
asyncio.wait_forsaves you from hung integrations.
Async Django takes discipline, but it pays off when your API lives in a dense integration network.