fromMarch 2013
Column:

Caffeinated Drupal

Introduction to CDNs
0

A high-performance website is like a great cup of coffee: Both are heavenly when conditions are optimal, but there are small things that can quickly turn them from an enjoyable experience into something that leaves a very bitter aftertaste. With the right tools and preparation, it's possible to create a website (or cup of joe) that can be dependable (like that first sip of coffee in the morning), fast to respond (like a quick shot of espresso), and scalable for many visitors (like that fancy cappuccino machine at Williams Sonoma, the one you casually mention to all your friends as your birthday approaches).

This is the first in a series of columns on the subject of “Caffeinated Drupal,” where I'll cover Drupal performance issues, always being sure to relate the topic to my favorite beverage.

Let's get started, shall we? Grab a steaming cup of java (or iced, if you prefer) and we’ll set out to learn about Content Delivery Networks (CDNs) and how they can give a huge speed boost to your website, with minimal configuration. Not sure which coffee to choose today? I recommend a lovely Ethiopian Sidamo served as a cappuccino — nothing like that powerful fruit flavor with undertones of bittersweet chocolate — topped with a dab of perfectly-foamed milk.

For those of you who aren't familiar with the concept of a CDN, picture it as a bunch of geographically dispersed web nodes which act as a front-end to your website. By bringing content physically closer to the end user, the improved speed of your load times can be staggering. You can also deal with traffic spikes much easier and without having to massively scale up your infrastructure — saving you money both on hardware and bandwidth.

There are many CDN providers, some much larger than others in terms of servers, geographic locations, and network presence. There are also various types of CDN plans, the most common being either a flat rate for a set amount of traffic, or a pay-as-you-go fee based on how much traffic you transfer. Typically, the larger the CDN network, the more expensive it will be. The benefit of using a larger CDN network is it will have end points in close physical proximity to more of your visitors, thereby serving your content to visitors faster than if they had to traverse multiple network links. You can think of the larger CDN companies a lot like Starbucks in the coffee world — you can never go too far without coming across a Starbucks (or two or three).

Once you've purchased a plan on a CDN, the next step is to integrate it with your website. There are two main methods used by CDNs for getting your content onto the CDN servers: origin push and origin pull (“origin,” in this case, means your web server). With the push method, you are responsible for manually or programmatically uploading your content to the CDN. The pull method is just the opposite; the CDN will automatically request content from your server when it receives a request and doesn't have that content already cached. The pull method is generally much easier to set up, since it doesn't take any configuration on your end outside of changing your URLs to point to the CDN. So how can we set up Drupal to interact with a CDN? It's easy with the Drupal CDN module.

Once you install the CDN module, you will be able to configure settings which tell the module which URLs to re-write your content to. For example, you'll likely set up domains such as cdn1.example.com and cdn2.example.com and point those to the CDN servers (this will be described in the setup documentation for your CDN provider). The CDN module will then automatically re-write URLs using your CDN hostname(s) for you so that instead of serving static content from your web server, it will be pulled from the CDN instead. For example, an image tag which references http://example.com/sites/default/files/test.png will be automatically re-written so that it uses http://cdn1.example.com/sites/default/files/test.png, which serves the image to your visitors from the CDN.

Wow, that was easier than ordering a double soy no-foam latte at your neighborhood coffee shop!