Reducing Your Server Workload: Load Balancing, and Dodging Server Limits
|
| |
![]() | |
Today, I was exploring the fact that all my shared hosting accounts seem to have consistantly maxxed out CPU(not good for business). As I was waking up/fixing the problem, I decided to quickly write a brief tutorial on how to keep this to a minimum.
- Location, Location, Location
- Keeping your related servers within one datacenter has unlimited advantages. You can consolidate certain tasks to certain servers.
- It’s sometimes useful, so long as they are close to eachother, to keep one server doing all the heavy mySQL lifting. Few providers will suspend a mySQL action 1/2 way through for exceeding your CPU usage. They WILL suspend web pages though. So by using one account to query mySQL on another account, you can keep both accounts active.
- If you’re using a dedicated, consider getting a nearby VPS setup.
- This can be used for either mySQL, or the heavy video transactions
- If you’re getting an ungodly amount of traffic, set up a VPS as the entry point. Have software on 2 dedicated servers that consistantly reports in to the VPS with their CPU/Memory usage. This way, you can decide where to direct traffic. This is called load balancing
- Keeping your related servers within one datacenter has unlimited advantages. You can consolidate certain tasks to certain servers.
- Optimize your MySQL Queries
- Learn the different varieties of databases, particularily innodb. Decide what’s best for you.
- Probably the single best command I’ve ever seen in mySQL, is the phrase “LIMIT 1″. Use it whenever you’re just checking to see if something exists. Even if it can only exist once. This will terminate the mySQL query after it finds the first, saving your server lots of work.
- Use primary keys.
- Fight the urge to use ORDER BY, and suborders for large tables. If you really need to for a table >5 million rows, find a different way. Sorting schemes and regular expressions consume CPU like you wouldn’t believe.
- I swear I have seen people use ORDER BY an auto-increment field. Why. That is never needed.
- Avoid using ‘IS LIKE ‘%phrase%’. It fights a lot of the ways mySQL speeds along it’s searching, and is normally just a way to be lazy.
- Keep your maximum data sizes accurate.
- Cache that Shit
- I would really love to put a stoner joke in here, due to the word “cached”. It’s too early for me to think of one though.
- Whenever you’re syndicating an XML feed, or any other feed that requires an external HTTP call (which is a major ouch to your server), consider caching. Normally a daily, or weekly refresh on the data is exactly what the doctor ordered. Enough to keep the SE’s interested, but slow enough you don’t fry your server.
- Yup, you guessed it. You’re getting a code snippet. This code is untested, but gives the idea.
<?php
function getFeed($url, $feedName)//This will rotate the feed at the start of every week.
{
$date=date(”W-T”);//date is the week of the year-the year.
$fileName=”./$date-$feedName.txt”;//where the cache SHOULD be located for this week
if(file_exists($fileName))//check to see if this weeks cache is done
{
return(file_get_contents($fileName));//if it’s cached, return it
}
else
{
$data=file_get_contents($url);//not sure if this is how to get the feed. Should work though. Dreamhost dislikes file_get_contents, change it to curl.
$handle=fopen($fileName,”a+”);//open file for writing
fwrite($handle, $data);//write the new feed info
fclose($handle);//close the handle
return($data);//return the newfound data.
}
}
?>
- Yup, you guessed it. You’re getting a code snippet. This code is untested, but gives the idea.
- Using that script raw will clutter up your home directory, so be sure to clear it out every so often. I’m not writing that function for you. On the upside, it should only create a few dozen cache files per year, so it will take a long time to clutter up.
- Kick out the Cash: Get a Damn Server
- For Whitehat, HostGator’s reseller hosting is scalable, powerful, and not prone to the same limits as other hosts. Sign up, don’t resell. If you’d like, use the hosting link at the right in the blogroll
- Overloading a server makes your stuff inaccessible. You don’t get customers. The search engines penalize you. It’s not worth it. If the above tactics don’t work, get yourself some more hosting.
- For blackhat webhosting, take a lesson from Vietnam. Hide amongst the jungle of other sites. For those of you who are slower, that means stick to shared hosting. Is it a pain? Yeah. Is it a risk for shutdown? Yeah. But it’s much better than a Google IP-Ban. On one of my hosting accounts, they currently have over 2000 sites. That’s a bit too much splash damage for Google to ban the entire IP.
- Remember, when doing all of this server specialization and isolation, to avoid creating a “point of failure” (yes, that phrase again).
- Create(in your mind) different classifications and risk levels for sites. Pool servers only within those classifications, so if shit goes awry, you still have some sites that are up.
- Get a dedicated if you have needs other than hosting. VPS are $55.00, and qute frankly, are not so good. A good dedicated runs maybe $120-160. It’s worth it.
- Once again, if you’re worried about heat, talk to a reseller. They’re more open to shadier clients sometimes. Make sure they keep at least 20 servers at the datacenter, and make sure THEY get it cleared with at least one tech/admin(that’s all it takes). It costs more, but do it.
- For Whitehat, HostGator’s reseller hosting is scalable, powerful, and not prone to the same limits as other hosts. Sign up, don’t resell. If you’d like, use the hosting link at the right in the blogroll
- Realize How Web Hosts chart Your CPU Usage
- It’s an eternally sliding 1 hour window. So it’s exactly 1 hour behind you, all the time. If it’s 8:33 PM, the window goes back to 7:33 PM.
- If you’re running cron tasks that use a lot of CPU, keep this in mind. Run them at least 1 hour, 1 minute apart.
- Break up your databases. MySQL counts towards your CPU usage, not just scripts. I myself had to knock a 15 million row table down to 4 million to make it useful.
- Excessive Hosting Does not Exist
- If you think you’re using too much, optimize it. But don’t be afraid of too much hosting. For example, I currently have 3 hosting accounts, on 2 different providers. I have 2 donated VPS’s, and 1 dedicated server.
- If you’re feeling the financial strain, go into their HELP section, and figure out how much CPU usage you’re allowed. Then go to other “help” sections of different providers, and figure out what their CPU usage limits are. Maybe it’s time for a switch.
- Limit Your Outbound Connections
- In Addition to the caching suggestion above, restrict how many external PHP calls you make.
- It is also good practice to only connect to 1 mySQL server, and do so once throughout the course of a script. Pass the connection back and forth to different functions rather than reconnecting.
Hope this helps.





November 16th, 2007 at 12:00 pm
Reducing Your Server Workload: Dodging Server Limits…
Tips on how to lower your CPU usage and make your websites run more smoothly. Includes SQL optimization….
November 16th, 2007 at 7:52 pm
Is there a new Eli beeing born?
November 17th, 2007 at 12:46 pm
heh. We’ll see. I like to not completely spew the tricks though
Keep blackhat profitable.
November 18th, 2007 at 12:59 pm
*g* anyway. keep up the posts. i wish, i already could spit out knowledge like you do. still developing on the lower end of the ladder.
June 18th, 2008 at 5:29 pm
Hey XMCP what sort of training would one need to administer all you describe above?