Caching and Compression

Caching with mod_cache and Compressing with mod_deflate

mod_cache_disk stores backend responses that their own Cache-Control allows. The backend's /report takes 300 ms and sends max-age=60; the vhost adds CacheEnable disk "/app/report", CacheHeader on (an X-Cache header) and CacheLock on:

Three requests for the same pageApache config
for i in 1 2 3; do
  curl -s -D h -o b -w '%{time_total}s  ' http://127.0.0.1:8103/app/report
  grep -iE '^(X-Cache|Age):' h | tr -d '\r' | tr '\n' ' '; cat b
done
Output
0.308996s  X-Cache: MISS from localhost report built at 07:05:37 by app1
0.001132s  Age: 1 X-Cache: HIT from localhost report built at 07:05:37 by app1
0.001448s  Age: 1 X-Cache: HIT from localhost report built at 07:05:37 by app1

Hits were 270 times faster. CacheLock lets one request refill an expired entry while the others get the stale copy. The trap is CacheQuickHandler, on by default: a hit is served before authentication and Require run, so set it off for anything behind access control.

mod_deflate is an output filter, so it compresses proxied responses too: with Ubuntu 225 's stock deflate.conf, a 24,000-byte text response from the backend arrived as 101 bytes of gzip. Compress in one layer only; Deflate and Brotli covers mod_deflate and mod_brotli in full.