Doing what I do for a living, I tend to see the same mistakes made over and over. Luckily where I work I am in a position to have a conversation with the person that made the mistakes, offer up a little bit of education, and most times they are very receptive to it. I am also in a position where I can help them to transition from their currently slow and possibly buggy solution to something that gives them the speed and performance that they want. This entire post is going to be targeted at Linux powered servers but many of the points will apply equally as well to Windows powered servers.
This one is a little bit long so if you want the full disk, click the link
I am going to start with the absolute number one mistake that I see. I encounter this one almost every day, and it is surprisingly the easiest one to fix.
1 ) Don’t underestimate the resources needed to do what you want to do.
Where I work we offer virtualized servers in a variety of sizes. There are many companies that do this. Most of them, their smallest offering has about 256 or 512 MB of RAM. These servers are intended to be used as a quick platform to try things out and get acquainted with how things work. Do not try to run your production website on one of these small servers. What ends up happening is that you use up all of the available RAM and your server will start to do what’s called swapping. Swapping is where your server needs to store some data temporarily but it is out of the (very fast) RAM so instead it uses the (in comparison very slow) hard drive. When this happens the whole thing grinds to a very slow pace if it doesn’t stop all together… but wait! there’s more! If you then run out of swap space (you only have so much) your server would crash. Linux doesn’t like crashing so instead it will just not allow the action that would use up the last of the swap space to run. This typically results in the application crashing.
If you are hosting a website and the web server (typically Apache in the Linux world) is the thing that tries to go over that hard limit, this can cause Apache to crash. BAM! website offline. Most websites use a database (typically MySQL) and if the database is that thing… the database can crash. Now your website is technically online, but it will be missing most of its content and probably be full of error messages.
Word to the wise, be realistic about the resources that are required for whatever you are doing. Keep an eye on your server… That brings us to…
2 ) Monitoring monitoring monitoring
The closer your server is to using all of it’s resources, the more often you need to check on it to make sure that it is running well… the more important that server is, the more you need to keep an eye on it to make sure that it is working well… you get the picture.
Some hosting companies will have ways that you can set up monitoring for your server and your website right in their control panel and some don’t. I would recommend at a minimum having some kind of monitoring system that is external to your server (that is, something that isn’t running on your server that is watching your server). Pingdom is pretty popular for this if you only need a small amount of monitoring. Other options to look into are setting up Nagios on another server or if you have multiple servers and it is a high priority that they are all working properly at all times you may want to look into something more like CloudKick.
If you are trying to get by on the cheap and run everything on a tiny server, keep a close eye on it because a traffic spike can cause it to start swapping and we already covered what happens then.
3 ) Use some form of caching
For some reason many people think that caching is a bad thing. This couldn’t be further from the truth (in most cases). This website uses caching extensively. It uses APC to cache PHP opcode, it uses Apache’s mod_expire to even have images and content cached in your local browser. It uses caching in the database. It uses caching in almost every part of serving this website to you. Thanks to this caching the website loads quickly, the server runs great, and your overall experience as the user is good (assuming that you like the content).
If you are running a PHP powered website, I would highly recommend looking into the APC extension. This allows for opcode caching, and while I am not going to get into what opcode caching is exactly, I will say that it basically allows your server to skip a step or two when rendering the content to send to your web browser. That’s pretty much what all caching does, allows a few steps to be skipped because they have already been done before.
If you are running a WordPress blog (like this one) there are some great caching plugins. This one is currently using W3 Total Cache but I have also used WP Super Cache in the past with great results.
If you are using an Apache web server, mod_expires can help out greatly.
4 ) Different types of scaling
In the hosting world, you will hear about this term scaling. Scaling is pretty much taking some kind of action to give your servers greater capabilities. There are two primary types and they are usually referred to as vertical and horizontal scaling.
Vertical scaling is when you upgrade your server. Think of this as putting in a faster processor or installing more RAM. In the virtual server world, most providers offer a way to resize your server to something more powerful right through a control panel.
Horizontal scaling is when you add more servers. Right now this site runs on just one server hosted by my buddies over at Slicehost (YAY slicehost!). If I were to move the database off of this server and put it on its own server, that would be horizontal scaling. At that point I would have two servers and if I added another web server and used a load balancer that would also be horizontal scaling.
Vertical = bigger servers… Horizontal = more servers… got it? great!
5 ) Load balance when needed
Making your server bigger and bigger will help to some extent, but there comes a time when getting a faster processor or more ram stops helping. This typically happens when either your hard drives are running as fast as they can and they can’t go any faster, or when the network connection to the server is running at the fastest speed that it can. Load balancing is sometimes considered to be the “black magic” of systems administration. This is primarily because while many people have heard of it, they don’t know how it works. It is amazingly simple…
You have a piece of hardware (or software running on a server) with a very fast network connection. Sitting behind that you have lots of servers that are all configured to serve the same content. The load balancer just hands out the task of serving that content to the end-user to one of the servers in the “pool” that it is connected to. See that… easy… don’t be afraid of load balancing.
This brings us to…
6 ) Don’t load balance wrong
When you load balance there are many things to take into consideration. You need to have something in place to make sure that all of the servers in the pool have the same content. You need to have some way of having your load balancer detect if one of the servers behind it go’s offline so it can stop trying to send users to that server. You need to have certain things commonly accessible to all servers… this one is the BIGGIE!
A common mistake that I see when people load balance a PHP website (although this happens in a similar way to sites that aren’t PHP as well) is that they forget about a thing called sessions. Sessions are a way of tracking your activities on a website. Don’t get all “OMG! people are tracking my internets” on me just yet. Without sessions, you wouldn’t be able to log into a website. You wouldn’t be able to add items to a shopping cart on a website. You basically wouldn’t be able to do anything specific to just you on a website.
By default PHP stores it’s sessions as a file on the hard drive. Lets run through what happens if you have 3 web servers (we will call them web1, web2 and web3) all sitting behind a load balancer and storing their sessions like this.
I go to randomwebsite.com, the load balancer gives web1 the task of serving me the content.
I log in to randomwebsite.com, the load balancer sends the login request to web2, which in turn starts a session (stored on it’s hard drive) and sends me back to a page with a nice “Hello cryptk” welcome message.
I click on a link, the load balancer gives the task of serving that content to me to web3, which does not have the session file (because it is on web2’s hard drive) and as such I am no longer logged into the website.
I think you can see that this is a problem. It is easily solved though. You can store your sessions in one common place for all of the web servers. I’m going to give you a hint… a network share of the place that the PHP sessions is stored is NOT the right way to do this. A much better option (and is practically the industry standard) is to have a server running Memcahce. All of the web servers are configured to put their sessions into this memcache server and they are all looking there for all session data. This means that if I run through the same scenario as above, when I log in on web2, the session in placed on the memcache server, and then when I get put on web3, web3 can find my session and I am still logged in!
This is definitely more common of a problem than you would think.
7 ) Don’t underestimate the price
If you are hosting a small website, and you have a background in this kind of work, you can absolutely get by with something inexpensive. If you are going to be hosting a large website and/or you don’t know what you are doing when it comes to server administration (be honest with yourself here), then understand that you only really have three options. First you could set up everything yourself and have it run poorly. Second you could spend quite a bit of time educating yourself on how to do things properly and possibly end up with something that runs well (this takes a lot of time… I have been learning this stuff for years and there is still more to learn). Or the final option… you can pay someone. This will be the fastest option to getting you something that runs right and runs well, just make sure that you are paying someone who actually knows what they are doing. That brings me to the last point of…
8 ) Rarely is the lowest bidder the best bidder
Understand that if you are paying $25 a year to someone to host your website, you are quite likely going to get $25 worth the work… The average free-lance fee for this kind of work is typically in the $100+ range… per HOUR. Going by that math, you would be getting somewhere around 15 minutes worth of someones time, possibly less. When I did freelance server work my hourly rate was $175 per hour for a cluster build-out. By that math, I probably wouldn’t even have finished the first paragraph for $25. There are some great ways to get this kind of work for far less money though. Find an actual company that does this kind of stuff as their main business. Companies do things to make a profit, one of the best ways to make profit is to get new customers and keep current customers. If you charge a lot of money, you can still get customers, but you likely won’t keep them. Companies have to keep their prices competitive. All of this works out to you as the user and consumer saving money.
Going with an actual company is going to be the right way to go for most people. If you need something specialized, then you are probably going to have to get a free-lancer with a background in what you are trying to do, but if you are just going to be doing web hosting, go with a company. Also note that while I am recommending going with a company, I am intentionally leaving out any mention of where I work (I do work for one such company though). Shop around… find someone who suits your needs… ask others who they went with and what they think of them.
On that note, the handy WordPress word counter has gone over the 2000 words mark… time to draw this one to a close. If you have any questions on any of this, you can use that Contact Me button at the top of the blog, or you can catch me on twitter @cryptkcoding
Steven Kirwan says:
Excellent weblog right here! Additionally your site lots up very fast! What web host are you using? Can I get your associate link on your host? I want my web site loaded up as quickly as yours lol
cryptk says:
It's hosted on a Slicehost server, so basically Rackspace Cloud Servers 😉 Sorry, I don't have any associate link. The big thing that makes it load fast is that I do web hosting for a living so I know how to make a web server scream 😉
touvet says:
Spot on with this write-up, I really suppose this web site needs rather more consideration. I’ll in all probability be again to read far more, thanks for that info.
hamsweb says:
Thanks for the pricelss information. Absolutly agree, rackspace is the way to go 🙂
BluePromoCode says:
Today, I see those same mistakes you write about at work. Your comments about caching and especially about load balancing were very helpful. One thing that is certainly, the lowest price is usually not the best.