Welcome!

Open Source Authors: Bruce Johnston, Colin Walker, Reuven Cohen, Timothy Fisher, RealWire News Distribution

Related Topics: Open Source, PHP

Open Source: Article

Put Your PHP App on Steroids

Optimizing with APC: Alternative PHP Cache

Ian Selby's Gen X Design Blog

Nothing's cooler than writing a bad-ass site or application and watching it gain popularity and a significant user base. By the same token, nothing's more frustrating than watching your app fall on its face when its running under high load. If you're like me, you know how disheartening this can be, as it usually means that it's time for a hard dose of reality: your code probably isn't as awesome as you thought it was.

Or is it? There’s a whole slew of things that a person could point a finger at regarding slowly running code. The most-oft thing that gets called out is code with a lot of overhead (a ton of includes, excessive logic, and the like), and right behind that is poorly designed databases (unoptimized indexes, no indexes, ridiculous amounts of joins, blah, blah, blah). Well, let’s assume for a minute that you’ve got a fair amount of experience under your belt, so you know your code is pretty damn optimal, and you also have a DBA buddy that took a look at your database and helped you tweak it up a bit. What do you do if this isn’t enough? Before you say, “throw more / better hardware at that mo-fo”, why not take a moment and learn about APC: Alternative PHP Cache…

OK, as usual, let’s make sure we are aware of a few general understandings before we get into the goodness of APC. It is very important to understand that despite all your optimization work, you’re always going to run into two major hurdles when it comes to keeping your app running blazing fast… that’s just the way it is. True, there’s a lot of things I’m glazing over here, and there’s even more points that could be debated… if you want to talk about it, feel free to contact me, I love talking code. For the sake of brevity and clarity, however, we’re going to steer clear of the nitty-gritty and look at things in a much broader scope. So, anyway, those two hurdles: memory, and database bottle-necking. You’re always going to need more memory, and at some point, your database is going to be slowing your app down. But before you get ready to get that beefier server, or a separate database server, or anything else (as much fun as new toys can be), let’s take a look at what can be done with your existing hardware and code.

I think it’s a fair statement that a lot of applications retrieve the same information from a database over, and over again, and that it’s also safe to say that this information gets updated at predictable intervals. Wouldn’t it be nice if we could store this data somewhere so we don’t have to keep pinging the database over and over for the same thing? It would also be pretty cool if we could leave that data stored, and update it not only in the database, but in the “storage area” as well? Good news kids, you can certainly do that… all with the magic of caching… APC caching!

I’m going to show you how APC can be leveraged to cache your application data, thus eliminating a ton of repetitive trips to the database, and thus removing your bottleneck! As an added bonus, I’m also going to do my best to explain some of the other goodness of APC and caching in general, as a lot of the articles floating around the web assume you simply understand what “opcode caching” and other stuff like that means (I certainly didn’t for a while).

Hopefully, if you’ve written an app that would benefit greatly from APC, you understand how PHP works. If you don’t, here’s a quick review… PHP is an interpreted language, which means that the code you write isn’t actually compiled, like actual application code is. The PHP code you write is compiled at the time it’s requested, and it’s compiled every time. Obviously, this is a really fast process, but I’m sure you can see how it’s also problematic. Well, that’s why APC was developed (by one of the guys that developed PHP, in fact). APC keeps that PHP code you’ve written compiled in memory, or caches it. This is what an opcode cache is, nothing more than a cache of pre-compiled code. By simply installing APC and enabling it, you will probably see a noticeable speed improvement in your app. But there’s a lot more we can do with APC to speed up an app, and we’ll get to that shortly, but I think I need to answer a more pressing question: “How the hell do I get and install APC?”

More Stories By Ian Selby

Ian Selby is owner of Gen X Design. He has been developing web sites professionally for over 7 years, and has always tried to stay on the cutting edge of web trends and technologies. He believes that usability and the user experience are the most important thing when writing web applications. He has a passion for all things Web 2.0, loves shiny icons and gradients, and lives in the San Francisco Bay area. He works for Aptana.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.