What is Apache Keepalive Timeout? How to optimize this critical setting.

If you’ve struggled to fix web server slowdowns or crashes, you may have come across a setting for Apache called “Keep-Alive”. This is one of the most powerful — and frequently misused — features of apache. In this article, we’ll explain:

  • What is Apache Keepalive / Keepalive Timeout?
  • What configuration is optimal for best server performance?
  • What happens when this is set incorrectly / left at defaults?
Apache Keepalive Timeout Settings in cPanel
Apache Keepalive Timeout Settings in cPanel

What is Apache Keepalive?

To best understand, think of your web server like a sit down restaurant. Your website visitors are customers, and the server software is the staff. The server software has “server slots”, which you can think of staff members, that can handle requests. Much like a business cannot have infinite staff, so too your web server can only have so many “slots”, as each one takes up RAM and processor time.

However, you can have many more customers than you have staff members, because each customer needs help only at certain times on their visit — when they are ordering food, paying for the check, and so on. As well, you can have many more website visitors than you have “server slots”, because, most of the time, your visitors are reading your website, looking at images, and so forth. Much less of the time, are they clicking on something to request a new page.

So where does Keepalive come in here? Imagine the ordering process at a
restaurant. Your server asks what you want, writes down each item, and only leaves when you have finished ordering. Your server does not simply go back to the kitchen to put in your order, after every single item that you ask for — that would be very inefficient! At the same time, if you are having difficulty deciding what you want, the server will offer to come back later once you have decided — they do not simply wait forever for you to decide everything you want to order.

Keepalive and keepalive timeouts are very similar to this. A webpage is made up of many different things your web browser must ask for. There are many files made up of text, formatting information, advertising, images, and videos. Without keepalive, your web browser would need to “flag down” the server for each and every one of these requests. This can slow things down tremendously. If there are no free slots, it can be even longer of a wait. Instead, keepalive allows your web browser to “flag down” the server just once, and then ask for many different files and images one after the other. When the browser stops asking for new things, the web server frees up the “slot” for other visitors to use. The keepalive timeout dictates how long the slot will remain occupied, after the web browser stops asking for new things.

Hopefully that gives you an idea what Keepalive is, and what the Keepalive timeout represents: it is an amount of time where a single user’s web browser has the undivided attention of one of the small number of server “slots” available to process requests.

What configuration is optimal for best server performance?

The short answer is:

  • Keepalive should be turned on
  • Keepalive timeout should be set to 1 second
  • Keepalive requests should be 100 or more. Unlimited is usually ok.

Much like a server will not wait an hour for you to decide on dessert, a web server like Apache will not wait an infinite amount of time for your web browser to ask for new files or images. Unfortunately, the standard / default amount of time it will wait, is far too long.

A single webpage is typically made up of a couple dozen files or images, and takes a couple of seconds to load. The time it takes a web server to receive and process a single request, say, load a single image, is typically less than 1/10th of 1 second. The keepalive timeout is a timer that counts down, and is reset every time the web browser asks for a new item. So long as the browser keeps asking for more things, the timeout will not expire, even with a keepalive timout setting of 1 second.

However, the default setting for Apache Keepalive Timeout is 15 seconds. In computer terms, this is an eternity. If it takes 2 seconds to load a webpage, then, the slot is active for 2 seconds, serving requests, and then waits 15 seconds longer, waiting to see if the user needs anything else. If the user doesn’t click anything or try to load something new within those 15 seconds, the slot cannot do any useful work for any visitors. If you are lucky enough to get one of these precious slots — and hold onto it — you might load your second page 1/10th of a second faster this way. This is because you do not need to open a new connection to the server, which takes a small amount of time. But this is at the expense of having to wait a very long time to get access to a slot in the first place — chances are, all of the slots are being “used” for keepalives from other idle users.

It would be like if a waiter, after delivering your food, waited 15 minutes to see if you needed anything else, before working to help anyone else. Even 1 minute (for a waiter) or 1 second (for a web server) is longer than is necessary, but at least it would not be a total disaster. With even a keepalive timeout of 1 second (the minimum you can set it to), it is almost guaranteed that a web browser will be able to keep the same connection slot for the entire time it is loading the page. There is no reason to set a longer keepalive than 1 second, pretty much ever.

What happens when keepalive is set incorrectly or left at defaults?

In a word: disaster.

Imagine our waiter, dropping off your food, and then waiting 15 minutes before doing any other work. Ordinarily, this would cause the restaurant (or web server) to grind to a halt. Nobody would be able to flag down a waiter to take their order. Ready food would get cold waiting to be picked up.

Let’s say the restaurant manager solves this problem by scheduling more waiters to work that day. This has a two fold problem. One, this is very expensive. Two, there is not enough physical space in the restaurant, parking lot, break room, etc, to handle one server for every table. Servers and staff would trip over each other trying to go anywhere, and servers would make very little from tips if they only have one table at a time they are helping.

The situation is not much different for a web server. If you increase the number of connection slots to handle this issue, you will soon face serious problems. Each connection slot uses ram. Needing many more slots for the same users, your server can easily run out of ram and crash. As well, there is a problem called “context switching” with computers. Simply, each slot is a program, and, switching from one program to another is an expensive operation for a computer. If you need 1,000 programs running instead of 200, the amount of time spent switching between them starts to use more processor time than the time those programs get to spend doing actual work. This can be a very serious issue, even if you have plenty of ram and plenty of processing performance.

Look at our prior example: A webpage takes 2 seconds to load.

In one scenario, a keepalive of 1 second, then, a given slot is doing work for 2 seconds, and sits idle for 1 second. A total of 3 seconds is used for each page viewed.

In the second scenario (the default configuration), a keepalive of 15 seconds, then, a given slot is doing work for 2 seconds, and sits idle for 15 seconds. A total of 17 seconds is used for each page viewed.

This is nearly 6 times the number of connection slots needed for a given number of visitors to your website! This means 6 times the ram, and 6 times as much context switching. This can easily crash a webserver. Or, if you have set a low enough limit on the number of connection slots Apache can create, you have a different problem: Visitors have to wait for a ridiculous amount of time to view your website, even though the server has quite a lot of spare CPU capacity to handle additional visitors.

The end result is quite simple — keeping Apache Keepalive Timeout at its default of 15 seconds, will use 6 times as much ram as setting it to 1 second. Meanwhile, there is no benefit to users, even if you have enough ram. Finally, website reliability and performance for any web server with even a moderate number of visitors, will be drastically hurt.

In case you missed it, here are the optimal settings for Apache Keepalive and Keepalive Timeout:

  • Keepalive should be turned on
  • Keepalive timeout should be set to 1 second
  • Keepalive requests should be 100 or more. Unlimited is usually ok.

edit: The default Keepalive Timeout for Apache seems to now be 5 seconds, and is no longer 15 seconds, at least in cPanel. So the scale of the problem of leaving it at defaults is less than it used to be. Even so, this default of 5 seconds is not optimal. 1 second remains the best and most optimal Keepalive Timeout setting.