Discuss a project

Blog / Backend

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

  1. Update Django to 5.x and set ASGI_APPLICATION in your settings.
  2. Switch to uvicorn or hypercorn and drop the legacy WSGI layer.
  3. Replace your drivers: psycopg3 in async mode, asyncpg, redis.asyncio from redis-py (aioredis was merged into redis-py and is no longer developed separately).
  4. Check your middleware: synchronous classes need to be wrapped in sync_to_async or rewritten.
  5. 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 sync and async ORM 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-asyncio tests and use httpx.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 locust or k6 to 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 gunicorn with uvicorn.workers.UvicornWorker: this is the standard way to deploy; uvicorn or hypercorn can also be used directly.
  • Use asyncpg and psycopg3 for the database to avoid blocking.
  • Watch the context: do not mix synchronous and asynchronous ORM queries.
  • Look for third-party libraries that already support async instead of writing manual wrappers.
  • Introduce timeouts and cancellation: asyncio.wait_for saves you from hung integrations.

Async Django takes discipline, but it pays off when your API lives in a dense integration network.

Shall we discuss your project?

Tell us about your task and we will come back with an estimate of the time and the cost within two working days.