Apache · Configuration and operations
How does keep-alive work and how should you tune it?
Short answer
Keep-alive reuses a TCP connection for multiple requests, reducing handshake and TLS cost. Enable it, set a short KeepAliveTimeout and a sensible MaxKeepAliveRequests, and align the timeout with upstream load balancers.
Without keep-alive, every request opens a new connection and, over HTTPS, repeats the TLS handshake. With keep-alive, the client can send several requests on one connection. The trade-off is that idle connections occupy resources until they time out or the client closes them.
KeepAlive On
MaxKeepAliveRequests 200
KeepAliveTimeout 5
<IfModule mpm_event_module>
MaxKeepAliveRequests 500
</IfModule>Tuning guidance
- Event MPM handles idle connections cheaply, so keep-alive is usually a clear win
- Prefork ties up a process per idle connection, which can exhaust workers
- A timeout of a few seconds is common; very long timeouts waste capacity
- If a load balancer's idle timeout is shorter, it may close connections Apache still considers open
- MaxKeepAliveRequests caps reuse; set it high enough to benefit but low enough to recycle
- HTTP/2 multiplexes streams over one connection, so keep-alive behaviour differs
Mismatched timeouts cause intermittent 502 errors. A proxy may reuse a connection just as the backend closes it. Keeping the proxy's idle timeout slightly shorter than the backend's, or enabling retries for idempotent requests, reduces the race.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What problem does keep-alive solve?
- 2Why does keep-alive interact badly with prefork?
- 3How do timeout mismatches cause 502 errors?
- 4How does HTTP/2 change the picture?
Say it in the interview
Keep-alive reuses a connection for multiple requests, saving TCP and TLS setup. I enable it and use a short timeout, because idle connections still consume resources. With event MPM that cost is low; with prefork it can exhaust workers. I also align the timeout with load balancer idle settings to avoid race-driven 502s.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Apache mock interview and get scored feedback on depth, clarity and confidence.