Wednesday, March 21, 2012

Utilizing Public CDN for common scripts like JQuery


Are you in a dilemma to use public CDN or go with self-hosting for common scripts?

Take a quick and bold decision to go with public CDN. Using Public CDN is the best way to provide optimal experience to web users.

Some people think Google like enterprises don’t maintain servers as we do! Some time they are proven to be rite!!

Best practice to use Public CDN for common scripts
  1. Always use minified version on production
  2. First pull your script from the CDN
  3. Check for the script availability
  4. If script download fail, load script from your host
Example implementation:

Google CDN

<script
src="
http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" >  < /script>

<script >  
!window.jQuery && document.write(' < script src="js/jquery-1.7.1.min.js" > < \/script > ')
</script>

Other Popular Public CDN

MICROSOFT

JQUERY


Do you want know why? Want to know what's under the hood? Never settle, go ahead and dig deeper, I am of your kind..

Why? What is the benefit of using a public CDN?

To understand this you need to know the basics of Internet and Browsers.

Let’s start with internet connection standards defined on HTTP 1.1 specification by W3C.

Browsers limit the number connections to the same domain irrespective of no. of tabs opened. Following are some findings from the internet for maximum number of connection limits.

Firefox 2.x       : 2
Firefox 3.x       : 6
IE 6/7               : 2
IE 8                  : 2 on dialup,
IE 8                  : 6 on broadband
Chrome            : 2
Safari               : 2
Opera 9.x         : 4
This is as per the HTTP 1.1 specification (which is currently live on 20th March, 2012) in the RFC 2616 Section 8 (Connections) under 8.1.4 (Practical Considerations) in the last paragraph.
Clients that use persistent connections SHOULD limit the number of simultaneous connections that they maintain to a given server. A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy. A proxy SHOULD use up to 2*N connections to another server or proxy, where N is the number of simultaneously active users. These guidelines are intended to improve HTTP response times and avoid congestion.
So when you load a page, browsers make only specified no. of concurrent connections to the server to pull resources. So serving static content from different domain origin will save your domain quota on same domain origin. I’ll explain more on improving performance in a separate post.

The next big advantage of using public CDN is from the Browser behaviour / feature. Most browsers support strong cache mechanism to optimize network usage and improve user experience.

Caching is a mechanism to capture and store the recent or most commonly accessed static content in a small storage space. The storage space mentioned here is a browser cache in the client machine. It could probably be a temporary internet folder.

For example if an image or script file is downloaded from the internet it is preserved in a temporary internet folder with its origin for a span of time. The span could be determined by the headers. Defining headers go beyond the scope of this topic so we’ll discuss it in a different post. So when next time the same file is requested, it is available to the browser from the local machine cache instead of downloading from the server.

Assume, the user visited a site, say SiteA which use public CDN for JQuery script file. Now the user visits your site and we assume you use the same CDN for JQuery script file as the SiteA used. This time the JQuery will be available to the user from his local machine cache and it is instant. This makes your site work faster.

Let’s go back to Advantage of Internet. It is on CDN technology. CDN is an abbreviation for Content Delivery Network. You can treat this like a server backed by many number of servers from geographically diverse locations. When you seek a resource the server helps to serve it from the geographically closest server. For the any file copied to the host server will be replicated to servers inside the provider CDN all over the world. Basically if time of travel reduces speed of delivery increases.

2 comments:

  1. Thanks buddy you helped me take a solid decision. Honestly I was in a confusion for a long time.

    ReplyDelete

Share your comments