---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
part: In practice
promise: Each layer is independent.
contents: cost-ladder, cross-layer-failures, modern-django, directing-agents
---

# Where Django leaks

The full measured latency and query ladder of a single endpoint, the failure modes that cross layer boundaries in production, and the sharp Django 6.x shifts that trip up older mental models.

For thirteen chapters we opened Django `6.1.3` one subsystem at a time — boot, middleware, URL routing, security, models, QuerySets, SQL compilation, transactions, migrations, templates, forms, sessions, auth, the Admin, built-in services, and tests. Each layer has a crisp Python abstraction that makes it look cleanly sealed off from the layers above and below it.

Production does not respect module boundaries. We opened this book in [One request, unwrapped](introduction.md) by watching a clean twelve-line view quietly explode to `642` SQL queries inside `Template.render` (`django/template/base.py:Template.render`). The slowest endpoints and the nastiest outages almost always happen at **seams** just like that one, where two clean layers interact: a lazy `QuerySet` (`django/db/models/query.py:QuerySet`) leaking SQL queries inside a template `{% for %}` loop, a `Task.enqueue` (`django/tasks/base.py:Task.enqueue`) leaking a ghost job out of a rolled-back `django.db.transaction.atomic` block, a single synchronous middleware trapping an `ASGIHandler` (`django/core/handlers/asgi.py:ASGIHandler`) chain on thread hops, or a test `Client` (`django/test/client.py:Client`) quietly skipping CSRF validation that fails in production.

## One endpoint, measured from URL match to HTML close

```text figure=cost-ladder
Full-stack latency and SQL ladder on desk/ GET /editions/2026-11-25/ (25 stories, Django 6.1.3, 7 trials)

1. Where time goes in one healthy request (3,885.4 us total · 25 stories):
   1. URLResolver.resolve()             10.0 us  ( 0.3%)
   2. 8-layer MIDDLEWARE stack          72.6 us  ( 1.9%)
   3. ORM (3 optimized SQL queries)   2494.4 us  (64.2%)
   4. Cached template render (25 rows)1308.3 us  (33.7%)

2. End-to-end GET /editions/2026-11-25/ across 5 modes (25 stories, 7 trials):
   page cache hit (0 SQL)              0.10 ms  (0.02x — 41x faster!)
   healthy WSGI (3 SQL)                4.13 ms  (1.00x baseline)
   uncached templates (3 SQL)          4.67 ms  (1.13x slower)
   ASGI + sync middleware (3 SQL)      7.26 ms  (1.76x slower)
   lazy template N+1 (81 SQL!)        26.59 ms  (6.44x slower)
```

*Every microsecond, share percentage, and SQL count is parsed directly from `ch14-bench`. Notice the decisive ratio: routing plus all eight middleware layers cost **`82.6 us` (`2.2%`)** combined, while ORM queries and template rendering consume **`97.9%`** of a healthy request — and one unprefetched template loop inflates SQL `27x` (`3 -> 81`) and latency **`6.44x`** even on zero-latency in-memory SQLite!*

## Putting all fourteen chapters to work

We close the book in four focused stops: first we examine the complete measured latency and query ladder of our newsroom project's edition endpoint; next we reproduce five classic cross-layer production leaks live on the same application and show each Django `6.1` guardrail; then we assemble a concise field guide to the modern Django `5.x`/`6.1.3` shifts that contradict older tutorials and model training data; and finally we distill all fourteen chapters into six sharp engineering questions for reviewing code written by teammates or AI coding agents.

We start with the full-stack numbers in [The measured latency and query ladder of one endpoint](leaks/cost-ladder.md).
