KeepAlive
In this page:
Why Reusing Connections Matters
Opening a TCP connection (and, for HTTPS, completing a TLS handshake on top of it) has real overhead. A page with 40 assets that opened a fresh connection for every one of them would pay that cost 40 times over. KeepAlive keeps the connection open after a response so the next request can reuse it immediately.
The Directives
KeepAlive On turns the feature on (it's on by default in modern Apache). MaxKeepAliveRequests caps how many requests can be served on one connection before it's closed (0 means unlimited). KeepAliveTimeout sets how many seconds Apache waits for another request on an idle connection before closing it.
The Trade-off
A longer KeepAliveTimeout helps a browser reuse connections across several page assets, but every kept-alive connection ties up a worker/thread waiting, even when it's doing nothing. On a busy server, a timeout that's too long can exhaust available workers under high traffic; a value that's too short defeats the point of KeepAlive in the first place.
How It Interacts with the MPM
How costly a long KeepAliveTimeout is depends heavily on which Multi-Processing Module (MPM) is in use -- covered in the next lesson. The prefork MPM ties up a whole process per kept-alive connection, making a long timeout expensive; event was specifically designed to handle keep-alive connections far more efficiently.
Example: A sensible KeepAlive configuration
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- Setting KeepAliveTimeout very high (30+ seconds) on a busy server, tying up workers on idle connections and reducing how many concurrent visitors the server can actually serve.
- Disabling KeepAlive entirely to "simplify" things, which usually makes real-world page loads slower since every asset now needs its own new connection.
- Tuning KeepAliveTimeout without considering which MPM is active -- the same value has very different costs under prefork versus event.
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: