WSGI vs ASGI servers

Concurrency in Python services Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

4 servers × 9 gunicorn workers × a pool of 5 connections each. How many database connections at peak?

Python 3.13
servers, workers, pool = 4, 9, 5
print(servers * workers * pool)

Options
Hint 1 of 1

Multiply all three.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 180

Correct. Multiply every layer; this is often more than the database is configured for.

Each server runs its own 9 workers, and every worker process keeps its own pool. Connections multiply at each layer: 4 × 9 × 5 = 180. Compare that with the database's max_connections.

Why not the others?
  • B 45. That's one server.
  • C 20. That ignores workers.
  • D 36. That counts servers × workers but leaves out each worker's pool of 5.

30-second answer

WSGI is the synchronous Python web interface: each request occupies a worker (process or thread) until it returns, as with Flask or Django under gunicorn. ASGI is the asynchronous one: an event loop per worker handles many requests concurrently and supports WebSockets, as with FastAPI or Starlette under uvicorn. Either way you run several worker processes per machine for CPU parallelism, often around one per core for async workers, more for sync ones with I/O waits.

Practise saying it · 30 s. No recording, just a timer

WSGI or ASGI?

Say your answer out loud, then check which of these points you covered:

  • Sync worker per request
  • Event loop per worker
  • Several processes per machine
  • Connection math

Deep dive

Gunicorn's docs suggest (2 × cores) + 1 sync workers as a starting point; measure.

Total database connections = workers × pool size per worker; keep it under the server's limit.

Long requests on sync workers block that worker; async workers suffer if code blocks the loop.

Django supports async views, but sync ORM calls in them need sync_to_async.

Common mistakes

  • One worker process on an 8-core machine.
  • Huge pools per worker that exhaust PostgreSQL connections.
  • Async framework with blocking libraries everywhere.

Senior take & follow-ups

Contrast the request models, then worker sizing and connection math.

Follow-up questions

Think of your answer first, then open the hint.

  1. When pick ASGI?

    Show hint

    Many concurrent I/O-bound requests, WebSockets, streaming.

  2. What limits sync throughput?

    Show hint

    Workers × threads: each in-flight request holds one.

Suggest a fix if something here is wrong or out of date.