Archive
This post is archived and may contain outdated information. It has been set to 'noindex' and should stop showing up in search results.
VPS Random Intermittent Slow Responses to HTTP Requests (Apache)
Nov 29, 2015Web DevelopmentComments (0)
If your VPS Linux Apache web server is randomly responding slowly to requests, especially many concurrent requests such as a page with many images on it, here is a possible solution that worked for my VPS.


Most Load Fast, Some Load Slow


I encountered an issue with my server that resulted in strange slowness when serving multiple images on a page. Most images would load quickly as expected, but a few would load slow. The actual download of the data was nearly instant, but the waiting period, or time to first byte (ttfb), was sometimes as great as 2 seconds. Here is what the waterfall would look like:

Random Slow Response to HTTP Requests

Each refresh of the page (after cache clear) would result in at least a few random images loading slowly. After a great deal of research, I discovered that it was due to the way Apache was configured.


Apache Prefork Spare Servers


Most VPS Apache servers use the prefork Multi-Processing Module (MPM), including mine. In this setup, the main httpd instance spawns child httpd instances that handle requests from users. These child instances sit idle waiting for requests, so they can respond to them quickly. Each child httpd instance can handle multiple http requests, and will die and be recreated based on various timing and limit settings. This way, memory leaks are kept in check, since there isn't just one long-lived httpd process handling all requests.

Settings Fix
The settings that determine how many child httpd processes are created are startservers, minspareservers, and maxspareservers. The default values for these are 5, 5, and 10, respectively. For some reason, my server had these set to 1, 1, and 2. This caused the web server to respond slowly like seen above, since only 2 child httpd processes were handling many concurrent requests, when there should have been 10.

Changing these setting back to the Apache default fixed the random slowness. If you're experiencing a similar problem, you may want to check your settings.

startservers 5
minspareservers 5
maxspareservers 10


See Running httpd Processes in SSH


To see live how many httpd processes are running, log into your VPS via SSH and use the command:

ps auxw | grep httpd
You should see one httpd process under root user. This is the parent and spawns the child httpd processes. You should see a number of httpd processes equal to your maxspareservers setting under the nobody or apache user (depending on what user is used for apache).
Comments (0)
Add a Comment
No comments yet