If you liked this post, please subscribe to my feed. Thanks for visiting!
Archive for November, 2007 Page 2 of 10
I’ve started a section on this blog called “My Code” that features code I’ve written. My first release is doConditionalGet(), a generic PHP function that implements HTTP’s Conditional GET mechanism. This function facilitates making dynamic PHP pages cacheable by HTTP accelerators, browsers, or other caches. Read more about it here.
Suggestions, comments, criticisms are very much welcome!
The following tutorials taught me how to make PHP pages cacheable. If you do not manually set your PHP page’s HTTP headers, that page is generally uncacheable by browsers, reverse proxies, HTTP accelerators, etc. That might make sense for highly dynamic pages or personal/sensitive information, but there are plenty of situations where PHP generated content can and should be cached.
- Ned Martin’s Site Guide — Caching
- PHP, HTTP/1.1 dates and Conditional Get
- Implementing support for Conditional Get with PHP
- Cache-Friendly PHP Applications (I think this is copied from a book without permission
) - PHP Cache Control script
- Cache Control with PHP
There is a typo in Ned Martin’s guide: “Last-Modified” is hyphenated! (Took me forever to figure out why I couldn’t set that header.)
Use the Cacheability Engine and the Live HTTP Headers extension for Firefox to debug as you go. Remember that shift-clicking reload/refresh forces the browser to fetch a fresh copy of the page.
Apologies for the short and sporadic posting. I’m busy hacking Zenphoto for another project.
P.S. Another plug for my host NearlyFreeSpeech.NET; Squid HTTP acceleration on shared hosting for mere pennies rocks! The availability of Squid made me look into HTTP caching for PHP pages.
As you may have noticed, this blog became very sluggish recently. Fortunately, I’ve narrowed down the cause of the lag to the WordPress internal object cache, which is supposed to increase site performance, but in my case it slowed down page load times by more than 10x, sometimes much more.
My blog’s symptoms:
- Disabling all plugins and reverting to the default theme and displaying only 1 blog post on the homepage did not really affect my page loadtime.
- The load delay was not an issue with PHP/MySQL at all. My page was completely executed and outputted by PHP in about 0.5-1 second (according to various plugins that showed the script execution time), but it took 10x that to cache the entire page to disk or load 100% of the page in a browser.
- The page load lag has nothing to do with the browser on the client side. I tried Firefox, IE, Safari, and even external tools like Pingdom Full Page Tester.
- The browser loaded 95% of the page and for some reason stalled for a long time on the footer section; making the footer empty didn’t help, and profiling the footer itself showed that PHP executed it in 0.06 seconds.
On a whim, I removed the following line that I inserted in wp-config.php to get back to a vanilla WP setup.
define(ENABLE_CACHE, true);
This line enables WP’s built-in object cache and is supposed to improve performance slightly. Problem solved!
This object cache supposedly caches database queries to disk to reduce CPU and database overhead. This should increase performance in most cases unless your disk I/O performance is very poor, like Dreamhost’s NFS storage system. This feature seems to be undocumented and had security issues in the past, but is commonly recommended as a safe way of increasing performance. I have heard that the object cache hardly improves performance, but I’d never imagined that it would slow my site down by 10x or more.
Disabling the object cache brought my page load times back to normal, with all my plugins enabled. I suppose the feature is disabled by default for a reason. If your blog is suffering from performance issues and your plugins or themes are not the cause, try disabling the object cache (if you enabled it). Comment out
define(ENABLE_CACHE, true);
in wp-config.php if needed.
However, some of the performance improvements in the upcoming WordPress 2.4 apparently have to do with the object cache. /me confused.
I will try to find out more about what exactly the object cache does and what it did to kill my site’s performance. Thanks again, Jeff from NFSN, for helping me troubleshoot my site’s performance issues.
I’d like to share the results of my research into low-cost webhosting. Don’t worry, none of these links are affiliate/referral links; I have nothing to gain from recommending these hosts.
The cheapest webhosting solutions generally come in the form of managed shared hosting services, where you are given a limited access account on a server shared by tens or hundreds of other clients like you. I recommend two shared webhosts, NearlyFreeSpeech.NET (NFSN) and MediaLayer, and one VPS host, Slicehost. This blog is currently hosted by NFSN.
NFSN’s most distinctive feature is its utility computing or pay-per-usage model. Instead of a flat monthly fee, they charge you for the precise amount of bandwidth and diskspace you use, no more, no less. You pre-pay any amount you want, and money is taken out of your account as your website consumes resources. When your account runs out of funds, your website shuts down (you can create alerts to notify yourself when funds run low). This blog, which only gets 50-100 daily visitors so far, is still running on the $1 payment I made a few weeks ago. For smaller sites, NFSN is cheaper than the most oversold shared hosts on the planet.
NFSN has another distinctive feature (that for some reason it does not advertise much): clustered and redundant backend systems. NFSN employs load-adaptive clustering systems that allow it to handle traffic surges like Digg/Slashdot effects by moving or mirroring your site onto other servers. Squid proxy servers are deployed for load balancing and content caching. To quote an NFSN employee: “You might find a shared hosting provider with more redundancy and fault tolerance built into their network than we have, but I seriously doubt it.”
Here’s a nice review of NFSN. And you can always try them out yourself, for free! Signing up gives you $0.10 credit (not sure about the amount, I think it was $0.10) which is plenty for you to setup your site and play around with the features on NFSN. No strings attached, no payment information needed.
MediaLayer claims to be the best of the best when it comes to shared application hosting. They are not interesting in just serving static content; they pride themselves in giving your dynamic web applications the best performance possible. To provide optimal performance, MediaLayer deploys the LiteSpeed web server (a proprietary web server compatible with Apache but significantly more efficient), and uses Zend Optimizer (which optimizes PHP code on the fly) and eAccelerator (which caches PHP opcode to avoid unnecessary recompilation), very unusual for shared hosts. They also guarantee 100% of your promised resources because they do not oversell. I don’t have personal experience with MediaLayer, but from their features and stellar reputation, I’d love to give them a try.
There is another type of webhosting solution that is often neglected when it comes to low-cost hosting: Virtual Private Servers (VPS). With a VPS, you get a virtual environment that operates as if you have a dedicated server, but in fact you are in control of a virtual portion of an actual server. VPS packages are generally more expensive and perform much much better than shared hosting packages because you get a much larger portion of a server’s resources, and you have full control over your own VPS, with root access and even the ability to install your own operating system (not always).
If your budget can afford $20/month in hosting fees, look no further than Slicehost’s VPS service. On Slicehost, your site will perform like a large commercial site hosted on state-of-the-art dedicated servers. With Slicehost, you can get the kind of service that used to cost hundreds and thousands of dollars a month. The catch is that you must know how to setup, configure, and administer your web server. Managing a VPS is a lot easier and risk-free compared to managing a dedicated server, especially with user-friendly tools provided by Slicehost, but it is definitely not for novices who don’t want to get their hands dirty. Another catch is that because Slicehost purchases new hardware strictly based on the number of new customers, and because of Slicehost’s wild success, there is currently a waitlist for new customers. It is well worth the wait though.
Other quality hosts I’m less confident about: A Small Orange, VPSLink.
Finally, I leave you with a warning: steer clear of incredibly oversold webhosts promising impossible amounts of resources for low prices. The usual suspects include Dreamhost, HostGator, Site5 and many others.
Many of these hosts have tried to convince everyone that the impossible 500GB diskspace + 5 TB bandwidth plans for $6 are not too good to be true. Some say it’s all marketing, competing over ignorant consumers. Some say that their 600,000-strong customer base allows them to get incredible discounts that can be passed along to clients. Others say that 99% of their customers underutilize their resources but pledge to continue servicing the select few customers who do use all 500GB of diskspace or 900GB of bandwidth. HostGator even promises to move any client who crashes his shared server to a semi-dedicated server. (Sources: 1, 2, 3)
Unfortunately, an hour spent Googling reveals that all these promises are worth shit. Maybe they really will move your site if you crash your server and bitch to the CEO and threaten to sue. But what if you just slow your server to a crawl but don’t crash it? What if you can’t get through to the CEO? Don’t have the money or guts to threaten legal action? The bottomline is that in 99% of the cases, you will run afoul of some “reasonable” CPU or memory usage restriction clause long before you can use even a tiny fraction of the 900GB promised you; you will be promptly shown the door.
Don’t count on good and knowledgeable tech support since these hosts easily have tens of thousands of customers, so unless they hire every expert sysadmin on the planet several times over, the person you end up dealing with is likely to be an entry-level tech support dude reading from a script.
These cheap oversold hosts also ensnare new customers into signing long-term contracts with them right off the bat. Wow, 500GB diskspace for $6? Sorry, only if you pre-pay for 10 years. You want to pay monthly? No problem! $11/month please + $50 signup fee. Are you kidding me?!

