About The Performance of ASGI Compared to WSGI

Analogous WSGI and ASGI views might take the same time to return a response when handled individually. The difference becomes apparent with multiple concurrent visitors.

Let’s assume it takes 1 second to load data from PostgreSQL, render, and return a home page for a single visitor. Now consider 100 visitors arriving at the same time.

WSGI with 10 worker threads:

  • Only 10 requests are handled concurrently, while the remaining 90 are queued.
  • Each batch of 10 requests takes 1 second to process.
  • The last requests in the queue may take up to 10 seconds to receive a response.

ASGI with 1 worker using asynchronous operations:

  • All 100 requests are initiated concurrently. Each request "awaits" the PostgreSQL query without blocking the event loop.
  • All responses are served within the same 1 second, provided PostgreSQL is configured to handle 100 concurrent queries (default max_connections value).

Tips and Tricks Programming Dev Ops Architecture Django 5.x Django 4.2 Django 3.2 Gunicorn ASGI WSGI NGINX Unit Uvicorn Daphne Hypercorn