In this recipe, we will use the Apache module KeepAlive to speed up the display process of a web page containing a lot of images.
On a shared hosting environment, it's highly probable that your host has already done this. To check this, use the Network tab of Google Chrome Developer tools and look at the Response header; you should find a line like the following:
Keep-Alive: timeout=15, max=200
If this line does not appear, locate the apache2.conf
file (for us it was under /etc/apache2/
) on your web server and modify the following lines so it looks like the following:
KeepAlive On KeepAliveTimeout 15 MaxKeepAliveRequests 200
This setting can improve the load time by 50 percent for HTML files with a lot of images.
Using the web stressor Apache Bench, we will make 300 requests for a category page (we use the sample data provided by Magento), and display 30 of them per page. That means 30 images to display, plus the basic theme images.
The URL to do the test is http://YOUR-STORE/index.php/electronics.html?limit=30
.
In order to run this, benchmark yourself. You can run the following command on Linux:
sudo ab -n 300 -c 5 http://YOUR-STORE/index.php/electronics.html?limit=30
That means 300 requests with five concurrent users.
Type |
Min (milliseconds) |
Max (milliseconds) |
Median (milliseconds) |
Request/second |
---|---|---|---|---|
KeepAlive off |
444 |
862 |
478 |
1.98 |
KeepAlive on |
432 |
744 |
452 |
2.17 |
As you can see, the improvements aren't much but we have to assume that we can't see the full power of this setting by stressing our server ourselves. Nevertheless, according to the Page Speed, YSlow, and other tools that aim to optimize websites, this feature is a must-have. Of course, results can change widely considering your hardware.
The idea behind the KeepAlive mechanism is to use a single TCP connection for a set of multiple HTTP requests and responses. The aim of this technique is to economize the opening and closing time required for a TCP connection. Indeed, your server will open only one connection and send responses for many requests through it.