Gustavo Gondim
Gustavo Gondim

Reputation: 1643

Is it better to use Cache or CDN?

I was studying about browser performance when loading static files and this doubt has come.

Some people say that use CDN static files (i.e. Google Code, jQuery latest, AJAX CDN,...) is better for performance, because it requests from another domain than the whole web page.

Other manner to improve the performance is to set the Expires header equal to some months later, forcing the browser to cache the static files and cutting down the requests.

I'm wondering which manner is the best, thinking about performance and if I may combine both.

Upvotes: 27

Views: 36473

Answers (5)

Igal Zeifman
Igal Zeifman

Reputation: 1146

Agree with @Anthony_Hatzopoulos (+1)

CDN complements Caching; also in some cases, it will help optimize Caching directives.

For example, a company I work for has integrated behavior-learning algorithms into its CDN, to identify and dynamically cache generated objects.

Ordinarily, these objects would be un-Cachable (i.e. [Cache-Control: max-age=0] Http header). But in this case, the system is able to identify Caching possibilities and override original HTTP Header directions. (For example: a dynamically generated popular product that should be Cached, or popular Search result page that, while being generated dynamically, is still presented time over time in the same form to thousands of users).

And yes, before you ask, the system can also identify personalized data and very freshness, to prevent false positives... :)

Implementing such an algorithm was only possible due to a reverse-proxy CDN technology. This is an example of how CDN and Caching can complement each other, to create better and smarter acceleration solutions.

Upvotes: 1

tess hsu
tess hsu

Reputation: 81

Above those experts quotes, the explanation are perfect to understand CDN tech and also cache I would just provide my personal experience, I had worked on the joomla virtuemart site and unfortunately it will not allow update new joomla and virtuemart version cause it was too much customised fields in product pages, so once the visitor up to 900/DAY and lots user could not put their items in their basket because each time to called lots js and ajax called for order items takes too much time

After optimise the site, we decide to use CDN, then the performance is really getting good, along by record from gtmetrix, the first YSlow Score was 50% then after optimise + CDN it goes to 74%

https://gtmetrix.com/reports/www.florihana.com/jWlY35im

and from dashboard of CDN you could see which datacenter cost most and data charged most to get your improvement of marketing:

enter image description here

But yes to configure CDN it has to be careful of purge time and be balancing numbers of resource CDN cause if it down some problem you need to figure out which resource CDN cause

Hope this does help

Upvotes: 0

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10557

Ultimately it is better to employ both techniques if you are doing web performance optimization (WPO) of a site, also known as front-end optimization (FEO). They can work amazingly hand in hand. Although if I had to pick one over the other I'd definitely pick caching any day. In fact I'd say it's imperative that you setup proper resource caching for all web projects even if you are going to use a CDN.

Caching

Setting Expires headers and caching of resources is a must and should be done 100% of the time for your resources. There really is no excuse for not doing caching. On Apache this is super easy to config after enabling mod_expires.c and mod_headers.c. The HTML5 Boilerplate project has good implementation example in the .htaccess file and if your server is something else like nginx, lighttpd or IIS check out these other server configs.

Here's a good read if anyone is interested in learning about caching: Mark Nottingham's Caching Tutorial

Content Delivery Network

You mentioned Google Code, jQuery latest, AJAX CDN and I want to just touch on CDN in general including those you pay for and host your own resources on but the same applies if you are simply using the jquery hosted files cdn or loading something from http://cdnjs.com/ for example.

I would say a CDN is less important than setting server side header caching but a CDN can provide significant performance gains but your content delivery network performance will vary depending on the provider.

This is especially true if your traffic is a worldwide audience and the CDN provider has many worldwide edge/peer locations. It will also reduce your webhosting bandwidth significantly and cpu usage (a bit) since you're offloading some of the work to the CDN to deliver resources.

A CDN can, in some rarer cases, cause a negative impact on performance if the latency of the CDN ends up being slower then your server. Also if you over optimize and employ too much parallelization of resources (using multi subdomains like cdn1, cdn2, cdn3, etc) it is possible to end up slowing down the user experience and cause overhead with extra DNS lookups. A good balance is needed here.

One other negative impact that can happen is if the CDN is down. It has happened, and will happen again. This is more true with free CDN. If the CDN goes down for whatever reason, so does your site. It is yet another potential single point of failure (SPOF). For javascript resources you can get clever and load the resource from the CDN and should it fail, for whatever the case, then detect and load a local copy. Here's an example of loading jQuery from ajax.googleapis.com with a fallback (taken from the HTML5 Boilerplate):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.2.min.js"><\/script>')</script>

Besides obvious free API resources out there (jquery, google api, etc) if you're using a CDN you may have to pay a fee for usage so it is going to add to hosting costs. Of course for some CDN you have to even pay extra to get access to certain locations, for example Asian nodes might be additional cost then North America.

Upvotes: 78

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47068

For public applications, go for CDN. Caching helps for repeated requests, but not for the first request. To ensure fast load on first page visit use a CDN, chances are pretty good that the file is already cached by another site already. As other have mentioned already CDN results are of course heavily cached too.

However if you have an intranet website you might want to host the files yourself as they typically load faster from an internal source than from a CDN. You then also have the option to combine several files into one to reduce the number of requests.

Upvotes: 7

jdi
jdi

Reputation: 92647

A CDN has the benefit of providing multiple servers and automatically routing your traffic to the closest location to your client. This can result in faster delivery, optimized by location.

Also, static content doesn't require special application servers (like dynamic content) so for you to be able to offload it to a CDN means you completely reduce that traffic. A streaming video clip may be too big to cache or should not be cached. But you don't neccessarily want to support that bandwidth. A CDN will take on that traffic for you.

It is not always about the cache. A small application web server may just want to provide the dynamic content but needs a solution for the heavy hitting media that rarely changes. CDNs handle the scaling issue for you.

Upvotes: 4

Related Questions