Popular posts
Solving Server Performance Nightmares
Posted by Scott on 4th December, 2007 | 5 commentsSo your site (or blog) became popular overnight and now your server is dying. If your site isn’t responsive and available to your users 24/7/365, then you’re losing money. Worse, if you don’t know the best ways to handle this problem when it happens, you’re likely to have a nervous breakdown as you watch helplessly as you lose visitors, RSS subscribers, and dollars, due to an outage or performance problem.
Fear not, I’ve endured this torture many times so you don’t have to. Some of these tips are best implemented before you have a crisis, and others will bail you out of a jam:
Monitoring Services
If you don’t know that it’s broken, you can’t fix it. If your site is down or performing poorly and you’re off at the movies, oblivious to what’s happening, that’s a problem. There are a ton of monitoring systems that will frequently ping your site from different geographic locations and alert you to downtime. I like Alertra. Alertra is very customizable. It will email you or call you at multiple points of contact when there is an outage, you can select different monitoring intervals (with the fee increasing as the interval decreases), you can set “do not disturb” hours for certain contacts like phone numbers. I use Alertra as a backup to another free monitoring service because it’s so customizable and has so many notification options.
DNS Failover
DNS Made Easy is a DNS hosting provider that offers a feature called DNS Monitoring and Failover. Sign up with them to manage the DNS for your domain, and they will monitor for any outage and alert you via email (much like Alertra). But with their DNS Failover solution, you can actually redirect traffic to a new IP within minutes, if your server goes down.
What I use for ResellerRatings is DNS Round Robin with Failover. That means I have three webservers, and three different IP’s, with identical mirrored content on each server. The DNS for ResellerRatings.com contains those 3 randomly rotating IP’s, so that users are distributed across the three servers transparently. If one or more servers fail, Failover takes it out of the DNS IP list, so that users are only distributed to the working 2 servers. This is a “poor man’s load balancing” solution, which is software based and very cheap. I was paying $600/mo for a hardware load balancer and junked it in place of this solution instead for both redundancy and distributed load.
So… Instead of having one powerful webserver, you could setup two lower end webservers and distribute the load between the two with round robin/DNS failover. That way, you get essentially the same performance of a more powerful machine, while adding the redundancy of a second machine in case one fails - that buys you time to fix the failed machine without being completely offline.
Auto Daily Backups
Many web hosts offer a daily backup service that you don’t even have to think about. I highly recommend setting that up for your site and any databases. If you have to remember to back up, you probably won’t do it, so it’s best to put it on autopilot.
PHP Code Solutions (Caching, Disabling Scripts)
There are lots of PHP coding solutions to an overloaded server. My two favorite are: caching and disabling non-essential apps during periods of insane load.
Sometimes our Danasoft.com site gets buried in requests for the dynamically PHP generated Danasoft signature image. That same server hosts other sites though, so to prevent Danasoft from bringing down other more important sites and services, we’ve modified Danasoft’s PHP scripts to shut down and stop serving the dynamic signature image if the server load goes above 3.5. That can be very helpful to prevent the server from becoming totally bogged down and unresponsive, and it’s something that I don’t even have to think about because it’s automated. Clearly, disabling a service isn’t the most desirable thing to do, but it can save you in a pinch and prevent more imporant services from going down.
To disable a script if server load goes over 4.0 (for instance), put this at the top of the script:
if (PHP_OS == 'Linux' AND @file_exists('/proc/loadavg')AND $filestuff = @file_get_contents('/proc/loadavg')){$loadavg = explode(' ', $filestuff);if (trim($loadavg[0]) > 4.0){exit;}}
Caching is my absolute favorite solution to any and all server performance problems. Apache is great at serving static content, but dynamic PHP scripts and MySQL queries will bring a server to its knees. Why generate the same page to different users over and over again if the content isn’t changing? Wordpress has a great plugin called wp-cache which you should implement right away. For all other PHP scripts, you’ll need some coding savvy or a good programmer to implement it, but PHP Cache Lite is a relatively simple way to cache dynamic pages. With cache lite, you simply add some code to the top of the script that bypasses the bulk of the script and prints out a cached version, then regenerates the cache at intervals you set (every 30 seconds, 5 minutes, etc.). This way, a PHP/MySQL heavy script is only running once every 30 seconds instead of once for every visitor.
Last Resort: Hardware Upgrades
There’s almost always a software solution to a performance problem: better MySQL queries, optimizing MySQL table indexes, optimizing the my.cnf file, optimizing Apache’s httpd.conf, implementing PHP caching, offloading MySQL server to a second dedicated server, offloading images to a dedicated images server with a streamlined installation of Apache or thttpd etc, but when all else fails, adding RAM or a faster processor may be a necessity. It also goes without saying that if you’re on a shared server, it’s easy to quickly outgrow it, requiring a dedicated box. It really pays to know your stuff and become educated at managing a dedicated server so that you don’t have to rely on others (which costs money and causes delays) to manage it for you.
Popularity: 20%
Tuesday, December 4th, 2007 at 2:07 am and is filed under Web Business. If you like this post why not subscribe to my full text RSS feed. You can leave a response, or trackback from your own site.

[…] Solving Server Performance Nightmares By Scott For all other PHP scripts, you’ll need some coding savvy or a good programmer to implement it, but PHP Cache Lite is a relatively simple way to cache dynamic pages. With cache lite, you simply add some code to the top of the script that … W Revenue dot Com - http://www.wrevenue.com […]
“Apache is great at serving static content…”
Actually Apache is *crap* at serving static content as each static file request requires a full Apache instance, loaded with mod_php and other memory-hungry modules, to send back the content to the browser.
A better solution is to put a light weight web server (lighttpd/nginx/litespeed) at the front to serve static content, and then either proxy to Apache for dynamic content, or use PHP/FastCGI. This way you’ll use much less memory, which can be by memcache to cache content or MySQL to cache query results.
I’ve had success with a lean compile of Apache Worker for true static content like html pages and images. For threaded & dynamic content, Apache is a necessity, and my point was just that a static page serves up with much less memory and CPU overhead than a dynamic page.
Caching content that is generated via script can be a huge saver. A better caching method may be to use memcache. Unlike cache lite that uses the filesystem memcache is memory resident. Anything you cache is stored in the systems memory. This can be many times faster since the disk is taken out of the performance equation. The down side is that you have to be able to install services on the server that you are hosting your website on.
Very good tips, Scott. As someone who has been around computers since long before the Web I am amazed/appalled by the level of service that even “big name” sites still acceot as “ok”. Servers do not have to crump when a blog page gets “Digged” and data centers (if their data architect ever had a cogent thought) do not have to shut down whole banks of servers to change a network switch. We (bloggers,site developers, users, hosting providers, et al,) should demand better of ourselves and our industry.
I really liked the round-robin with failover idea. Seems to me that even a small-time hosting provider could partner with s couple other small competitors and offer near-perfect web page avaiolability (up time) for very little cost.