(Obligatory preface: For the better part of the last 4 years I’ve spent my days (and nights) installing, configuring, and tweaking servers that power one of the more trafficked political sites on the web. It all started with one Windows box and ended with eight LAMP servers, load balancing, firewalls, database replication, and enough Ethernet cables to circle the earth a few times over. The result: 150 page views per second and 300 Mbps, without breaking too much of a sweat.)

“Our website is getting hit with too much traffic. What do we do now?” CEOs, managers, and tech dudes have all asked this question a thousand times. Being in the business of server hosting, we’re asked this question every day. So, we thought it might be helpful to take you through the lifecycle of scaling a website – from shared hosting, to dedicated hosting, to managed hosting.

It’s easy to argue that shared hosting is the simplest way to get a website up and running. Pricing usually starts under $10, and the setup is fairly straightforward and automated. When a few weeks or months go by with no problems, you become confident that the masters of the Internet have finally made it so that website hosting “just works.” But then you publish a video of your pet poodle wearing tights and dancing to Michael Jackson’s “Thriller” and your website becomes an internet sensation. Then it crashes, and words like “DoS” and “hack” start getting thrown around. You break out into a cold sweat and find yourself running around like a squirrel in traffic.

Enter, dedicated hosting. Instead of competing for resources on a shared server with 30 other websites, you’re on your own. Which means just that: you’re on your own. Here are some tips for surviving in this environment:

  • Most sites that run popular web software like Drupal and Wordpress are heavily database-driven, and it makes sense that this is one of the first things to cause your site to buckle under heavy load. Fortunately, memcached is here to help. It stores content in memory, which is a lot faster to access than database files written on disk. It’s a snap to configure and setup, and it can speed the delivery of your pages a hundredfold.
  • Combine your CSS and Javascript into as few files as possible. The more files you have, the slower a website is and the more resources your server needs to deliver them.
  • Get a hold of a CDN for static content like images, Javascript, and CSS. (If you’re a Voxel client, you have one already!) Your big dedicated server should be generating HTML pages, not delivering 1×1 spacer gifs. Think Hulk Hogan benchpressing a feather. It’s not pretty.
  • Tweak, tweak, tweak. There are a number of Apache and MySQL configuration settings that you can use to your advantage. Increase Apache’s MaxClients, turn off KeepAlive (if you’re using a CDN for static files), and increase the number of max_connections for MySQL. Google your way to a better performing server. As you tweak, make small, incremental changes and monitor the effects that they have.
  • Monitor your server’s internal heartbeat through the use of server metrics (e.g. Nagios or Munin), to find out what part of your server is dying first. (Voxel uses the very useful Ubersmith.) Is it disk I/O, RAM, or the processor? Sometimes, a simple hardware upgrade may solve the current bottleneck.

With some tech experience and a little luck, you should be able to deliver tens of thousands of page views every day. But that’s not always enough, and you might need more — more gear, more help, or more experience. If you don’t have the budget to employ in-house tech staff, and don’t want to spend late nights researching random disk I/O patterns, then you have to look to a managed hosting company. Managed hosting companies — like us! — take over the day-to-day server and application management and use their experience to help scale your setup.

The first step in a scalable managed hosting deployment is usually “role separation.” This might sound like an industry code word for “really expensive,” but it allows you to independently and intelligently scale the part of your system in the right place — allowing you to tweak the hardware resources that need it — which ultimately saves you money.

Scaling a typical LAMP stack setup will first separate your web/application servers (i.e. Apache and PHP) from your database servers (i.e. MySQL). Most hosting companies will recommend that you deploy a pair of these web/application servers and use load balancing to equally distribute visitors between the two — if one goes down, they’ll get directed to the server that’s standing. This also means you can load balance between 2, 3, or 15 web/app servers at the same time. Once these servers are tuned and humming, you can focus on the thorny database sever. If you’re running a dynamic site with lot’s of comments, updates, new content, etc., you really can’t skimp your money on the database. It’s one thing that will eat ram and hard disks for lunch (and come back for seconds!).

Managed hosting firms have all kinds of great tricks up their sleeves to help you scale your app to thousands of hits per second and beyond.  The trick, of course, is doing this within budget. So take a look at more on-demand setups like CDN’s and Cloud Computing (more on that later!), which can help you ramp up when the visitors are hitting your crazy poodle video over, and over, and over.

Related articles by Zemanta

Reblog this post [with Zemanta]

Posted in General Posts | 1 Comment »

One Response to “Scaling the LAMP Stack – Confessions of a PHP Dev Turned Sales Guy”

  1. Matthew OgstonNo Gravatar Says:

    Great article Nathan – it would be very useful to see this article turned into a follow-up series of best practice recommendations where you explore some of your discussion points in more detail – particularly CDNs and load balancing.

Leave a Reply

Make a Comment

Your Name

Email

Homepage

Comment