---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
part: Presentation and systems
promise: Built-in services stay out of your way.
contents: cache-backends, cache-middleware, signals, tasks, storage, i18n-and-timezones
---

# Caching, signals, tasks, and files

Cache middleware ordering traps, the hidden synchronous cost of `Signal.send`, Django 6's native `django.tasks` framework, file storage backends, and thread-local localisation.

Beyond requests, the ORM, templates, forms, and auth, every production Django application leans on five horizontal services that ship inside core: `django/core/cache/`, `django/dispatch/`, Django `6`'s brand-new `django/tasks/`, `django/core/files/`, and `django/utils/translation/` + `django/utils/timezone.py`. On the surface they look like effortless helpers — drop in a cache middleware, connect a `post_save` receiver, decorate a background task with `@task`, attach a `FileField` (`django/db/models/fields/files.py:FileField`), or flip a timezone.

Open the source and each service reveals one sharp operational constraint that routinely bites production teams. `UpdateCacheMiddleware` (`django/middleware/cache.py:UpdateCacheMiddleware`) and `FetchFromCacheMiddleware` (`django/middleware/cache.py:FetchFromCacheMiddleware`) must wrap `"MIDDLEWARE"` at opposite ends of the onion so `"Vary"` headers are learned after `SessionMiddleware` and `LocaleMiddleware` finish; `Signal.send` (`django/dispatch/dispatcher.py:Signal.send`) is a strictly synchronous Python loop (and `weak=True` silently garbage-collects local receivers); Django `6.1`'s `Task` (`django/tasks/base.py:Task`) and `TaskResult` (`django/tasks/base.py:TaskResult`) enforce JSON normalization and module-level importability across `ImmediateBackend` (`django/tasks/backends/immediate.py:ImmediateBackend`) and `DummyBackend` (`django/tasks/backends/dummy.py:DummyBackend`); `FileDescriptor` (`django/db/models/fields/files.py:FileDescriptor`) wraps paths into lazy `FieldFile` (`django/db/models/fields/files.py:FieldFile`) proxies and intentionally never deletes files when rows are deleted; and active languages and timezones live in `asgiref.local.Local` ambient state.

## Why whole-site caching splits into two opposite middleware

```text figure=cache-middleware-tracks
Page cache middleware ordering & hit speedup on desk/ edition_front (Django 6.1.3, 7 trials)

1. Why UpdateCacheMiddleware is first and FetchFromCacheMiddleware is last:
   Request phase (top-down):  UpdateCache (no-op) -> Session/Locale (LANGUAGE_CODE) -> FetchFromCache (HIT: 0 SQL!)
   Response phase (bottom-up): edition_front (3 SQL) -> Locale/Session (Vary) -> UpdateCache (learn_cache_key + set)

2. Measured edition_front latency: uncached vs cache middleware (7 trials, median us):
   cache miss (render + learn + set)     2386.8 us  (0.93x — miss overhead)
   uncached edition_front (DB+tmpl)      2231.6 us  (1.00x baseline)
   cache hit (FetchFromCacheMiddleware)    91.1 us  (24.50x faster · 0 SQL)
```

*Every SQL count and microsecond bar is parsed directly from `ch12-services` and `ch12-bench`. When the two middleware sit at opposite ends of `"MIDDLEWARE"`, a cache hit serves `"desk/"`'s front edition in **`91.1 us`** (**`24.50x` faster** and `0` SQL queries vs. `3`), while `UpdateCacheMiddleware` still sees `"Vary: Cookie"` plus `"Set-Cookie"` on personalized responses and refuses to cache them!*

## Five horizontal services in six stops

We start inside `CacheHandler` (`django/core/cache/__init__.py:CacheHandler`), `BaseCache.make_key` (`django/core/cache/backends/base.py:BaseCache.make_key`), and `LocMemCache` (`django/core/cache/backends/locmem.py:LocMemCache`) pickling, then trace the two-piece page-cache middleware and `learn_cache_key` (`django/utils/cache.py:learn_cache_key`). Next we open `Signal` (`django/dispatch/dispatcher.py:Signal`) and measure the hidden N+1 cost of synchronous `"post_save"` receivers on `"desk/"`.

Then we examine Django `6`'s native `django/tasks/` architecture — running `"desk/"`'s `"build_edition_digest"` through `ImmediateBackend`, `DummyBackend`, and `django.db.transaction.on_commit` — before finishing Part III with `StorageHandler` (`django/core/files/storage/handler.py:StorageHandler`), `FieldFile` laziness, and `asgiref.local.Local` language and timezone boundaries.

We start with cache backends and key versioning in [Cache backends, key versioning, and stampedes](services/cache-backends.md).
