90% of my Traffic is Lightspeed fast, but I need more!

Lightning fast NEW scalable Magento Hosting. Easy, powerful, on-demand. (Guides)

What’s going on?

First, understand from a high-level that Lightspeed caches entire pages. It’s like taking a picture of a group at a family reunion. Instead of having to get the family back together every time you want to show that particular page (or see that group of people), you just bring up the photo you took. With Lightspeed, instead of loading PHP, running through Magento, and making DB calls to generate a page, Apache simply returns the static HTML generated for that page when it was first cached (Or that picture was taken). This means much faster page loads and much less work for Apache/PHP (or whatever web server setup you use).

For typical Magento installations, a small portion of your traffic requires you to change something (like adding cousin Larry to the picture), which can require getting the whole gang back together (PHP/Magento/MySQL). In the case of Magento, this happens for a small number of your users when a user logs in, adds something to their cart, starts checking out, or you’re in the admin panel, which renders new HTML for  every single page load. With Lightspeed, a fully re-rendered page is required only occasionally for your customers. If your admin is “too slow”, you probably have problems that a hardware upgrade could address.

NOTE: If you’re not yet in production, and ALL pageloads are slow, Lightspeed is most likely not properly configured and not running at all. If you’re not sure, add a URL variable to the end of the page address “debug_front=1″ — IE http://mysite.com/?debug_front=1. Lightspeed is running if you get a “success” message. If not, you’re not configured correctly.

If occasional page loads “requiring the whole gang” are still too slow, better server hardware and/or configuration may be required. A possible solution may be available by simply talking with your hosting provider or IT department for more information about different hardware and/or optimizing PHP, MySQL, or Apache alternatives.

While a hosting provider may be able to help, Lightspeed provides another way to reduce the number of “family reunions”. However, this solution is more technical for your developer. It’s called hole-punching.

What can I do?

To go beyond 90% of Lightspeed traffic on a Lightspeed installation, mix and match the following options with careful consideration of the labor, skill and monthly costs required for each:

1. Consider changing your server configuration or hardware - This can be expensive, but without technical knowledge, it’s an easy way to make everything proportionally better. If you meet these requirements, you’re probably okay hardware wise.
2. Hole-punch your site - The time and skill required isn’t “quick” or “easy” in comparison to a 30 minute, basic Lightspeed install (several hours to several days, depending on your developer), but in the long run it’s a cheaper solution than higher monthly hosting costs.

What can I do with server configuration? – Hardware seems expensive? We know! You can do so many things with server configuration! Here are two of the biggest points of slowdown we’ve seen (may not apply to everyone):

1. PHP Accelerator- Look into a PHP accelerator or different engine if you’re using modPHP. If you have a provider or IT department that can configure this, it will help.
2. Ditch Apache - You may have always trusted Apache, but that might mean that you’re getting up there in years:) In terms of web servers, Apache is old and outdated. Informed critics will agree that it has weak points and Magento experts will tell you that Apache can crumble in certain scenarios. There are a ton out there and they aren’t hard to use (Nginx, Litespeed (not our module), SimpleHelix, etc…)

What about hole-punching? – Hole punching is loading only small dynamic elements into a fully cached page (adding cousin Larry to the photo without having to take a new picture). Magento reloads the entire page when dynamic content is required for a shopping cart or logged in users, for example. Hole-punching can be very powerful, combining the benefits of high-speed caching and dynamic content, but this process is not for beginners. Here’s how that can be done.

Down To Business

Warning: I said hole-punching isn’t for beginners. It really can be complex. Even the most skilled Magento developers may require a couple of hours to successfully punch holes in a site they’re deeply familiar with. If you’re still reading, but not in that category, you’re either willing to brave the amount of time for the increased benefit (faster pages to users with product in their cart), or you will be talking to your developer or IT department about this process.

If you’re still not deterred, let’s continue. We’ll walk through punching a single hole as an example. It’s a very common punch around the links at the top of a page to show if a user is logged in and if they have any items in their cart. After that you should be able to punch holes wherever you wish, with some practice.

When hole-punching, I usually split things into the punch and the fill. It provides opportunity to debug along the way. I’ve separated example files in this way so there’s no confusion:

HolePunching_Example.zip

For most of these steps I’ve taken I added HTML/XML comments with the word “TinyBrick” into the code around lines that I’ve added to the default files in Magento.

If you have trouble finding the files on your own server, and desperately want to ask me questions, that’s probably a big red flag that you’re in over your head.

Punching Holes

1. header.phtml (app/design/frontend/{your namespace}/default/template/page/html/header.phtml): As you’re probably already found, templates like this is where you put HTML. Here you’ll decide what content you need punched out of the cached version of the page. You’ll just need to surround the HTML you need to retrieve dynamically with the NOCACHE tags (refer to my example header.phtml for syntax). It’s important that you also add a key unique only to this hole punch. This can appear on many pages…so make it unique from any other hole punch key name you think you’d use in any other hole punches you want to make. Lightspeed will use that key to determine what dynamic content goes where. In the example header.phtml, I’ve done this around the links at the top where you would want to punch a hole for cart and logged in status.
2. page.xml (app/design/frontend/{your namespace}/default/layout/page.xml): I haven’t changed anything here, but you’ll need to see where I’ve gotten some of these values to understand the rest. If you look at line 41, you notice that it is a child block within the header block (lines 40-48). We’ll need the value of name (or “top.links”) later.
3. Next we test! If it all worked correctly, then after you cache your catalog (or CMS) page, simply login or add something to your cart. The links at the top should disappear or be punched from the cached page. If you’ve gotten this far, you should know that you just punched a hole in your cached page successfully. Congratulations!

Filling Holes

Because of the way Magento is built, filling holes with content usually just involves calling specific blocks again to let Lightspeed fill the hole-punches using those blocks. However, sometimes Magento will nest blocks, forcing you to load entire parent blocks and all their children just to access one child block. This is the case with the particular hole-punch we’ve chosen, but like we saw in page.xml, the parent block for the header only loads a few sibling blocks, so this shouldn’t be too labor intensive to fill properly.

1. First we’ll need to install the example module that I’ve built to fill hole-punches (feel free to build your own or add an action to an existing controller). Take the folder Hole-Filling/Delorum and copy it to the app/code/local folder. Also, take the Delorum_…xml file in the folder Hole-filling and copy it to the app/etc/modules folder. That should essentially install this quick little module, which you need to do only once. You can re-use this module to fill as many other hole-punches as you like. After it’s installed, test this module by calling the indexAction of it’s controller — http://yourbaseurl.com/lightspeedcontent/hole/index. If it was installed correctly, you should get a blank page that only displays the contents of an array with a “Hello World!” value in it. If not you’ll probably get a 404 error.
2. catalog.xml (app/design/frontend/{your namespace}/default/layout/catalog.xml – This applies to cms.xml as well for the cms_page node as well on a default Lightspeed install): This is the layout for the catalog pages and product pages. To tell Lightspeed to fill holes on this/these page(s) add the holecontent node to the pageCache reference you’ve already added (with the install instructions PDF). I’ve done this in the example catalog.xml on lines 77-83 and 160-166 (21-27 in cms.xml). This will tell Lightspeed where to go to fill the hole in each page layout.
3. HoleController.phpin the Hole Filling example folder (app/code/local/Delorum/Lightspeedcontent/controllers/HoleController.php): Nothing to do here right now, but let me explain. In here, you’ll craft the individual pieces of content that Lightspeed will use to fill each hole-punch. There are several methods of getting content to fill hole-punches. Which method you use is up to you. The way I’ve done it is fairly typical for this particular block.
You’ll notice the IndexAction (another example is in the documentation) contains an array called $content. In this array you’ll use the key you chose in step 1 of Punching as your array key in this associative array. The value is whatever dynamic content you want to load into that particular hole-punch (currently “Hello World!”). If you know PHP or Magento, you can probably imagine where this is going. Just to complete the example though, let’s continue.
4. Test time again. Repeat the test you did in step 3 of Punching. You should now get a “Hello World!” message instead of missing links at the top. We’ve now successfully filled a hole-punch. How does it feel?

After-note: Now I’ve inserted two copies of the $content array (in HoleController.php) because I’ve found the most success starting with a test message like “Hello World!” Once that’s working, insert the actual content you’d like to fill the punched hole. For completeness, you can uncomment the second $content array and line 13 in addition to commenting the first array again. If all goes well, this should properly fill the hole punched around those links with the original content.

Phew! I hope this example helps with your hole punching expedition. Feel free to provide feedback in the comments. While hole-punching can be difficult, my hope is that this short tutorial puts it within reach for more developers.

Tags: , , , , , , ,

Leave a Reply