Starlette ASGI — Core Concepts

What Starlette actually is

Starlette is a lightweight ASGI framework and toolkit created by Tom Christie (also the author of Django REST Framework). It provides the core building blocks for async web applications in Python: routing, middleware, sessions, WebSocket support, and background tasks.

The critical thing to understand is the ASGI part. ASGI (Asynchronous Server Gateway Interface) is a spec that defines how Python web apps communicate with web servers. It replaced the older WSGI spec by adding support for async/await, WebSockets, and HTTP/2.

How ASGI differs from WSGI

WSGI works like a function call: the server calls your app with a request, your app returns a response. One request, one response, synchronously.

ASGI works like a conversation: the server and your app exchange messages over time. This enables:

  • Long-lived connections — WebSockets stay open for minutes or hours
  • Async I/O — your app can await database calls without blocking other requests
  • Server-sent events — streaming responses to the client
  • HTTP/2 support — multiplexing multiple streams over one connection

Core components

Routing maps URL paths to handler functions. Starlette uses a Route and Router system:

  • Path parameters (/users/{user_id}) are extracted automatically
  • Routes can be nested using Mount for sub-applications
  • Both function-based and class-based views are supported

Middleware wraps every request/response cycle. Starlette ships middleware for CORS, GZip compression, HTTPS redirects, trusted hosts, and sessions. Custom middleware follows a simple pattern: receive the request, optionally modify it, call the next handler, optionally modify the response.

WebSocket support is first-class. You define WebSocket routes alongside HTTP routes, with accept(), send_text(), receive_text(), and close() methods.

Background tasks let you run work after the response is sent. Useful for sending emails, logging analytics, or cleanup — the user gets their response immediately.

The FastAPI relationship

FastAPI wraps Starlette and adds automatic data validation (via Pydantic), OpenAPI documentation, and dependency injection. Every FastAPI app is a Starlette app. This means:

  • Starlette middleware works in FastAPI
  • Starlette’s TestClient works for testing FastAPI
  • Performance characteristics are identical
  • You can drop down to raw Starlette when FastAPI’s abstractions get in the way

When to use Starlette directly

Choose Starlette over FastAPI when you want minimal overhead and don’t need automatic validation or docs. Common cases: microservices with simple endpoints, WebSocket servers, proxy services, or when you’re building your own framework layer.

Choose Starlette over Django when you need async-first design, WebSocket support, or when your app is API-only without needing Django’s ORM, admin panel, or templating.

Common misconception

Many developers think Starlette is “just the thing under FastAPI” and not worth learning directly. But understanding Starlette means understanding how your FastAPI app actually works — its routing, middleware stack, exception handling, and request lifecycle. When something breaks in production, that knowledge is invaluable.

Performance reality

Starlette consistently benchmarks among the fastest Python web frameworks. In TechEmpower benchmarks, it handles tens of thousands of requests per second for JSON serialization. But the real-world advantage isn’t raw speed — it’s concurrency. An async Starlette app serving 1,000 concurrent users waiting on database queries uses a fraction of the memory that a sync framework would need (which would require 1,000 threads or processes).

The one thing to remember: Starlette is the async foundation layer of modern Python web development — learning it means understanding how FastAPI, and the ASGI ecosystem as a whole, actually works under the hood.

pythonweb-frameworksasgiasync

See Also

  • Python Aiohttp Client Understand Aiohttp Client through a practical analogy so your Python decisions become faster and clearer.
  • Python Api Client Design Why building your own API client in Python is like creating a TV remote that only has the buttons you actually need.
  • Python Api Documentation Swagger Swagger turns your Python API into an interactive playground where anyone can click buttons to try it out — no coding required.
  • Python Api Mocking Responses Why testing with fake API responses is like rehearsing a play with stand-ins before the real actors show up.
  • Python Api Pagination Clients Why APIs send data in pages, and how Python handles it — like reading a book one chapter at a time instead of swallowing the whole thing.