Virtual Developer Workshop: Containerized Development with Docker
What is CDN?
A Content Delivery Network or CDN is a network of servers situated at key locations across the globe. This network maintains cached copies of files that are to be delivered to the client. When a client tries to access any file that is being maintained by the CDN, the server nearest to the requesting client fulfills the request. This technique is known as Edge Caching since the servers towards the "edge" supply the content.
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.min.js" ...> </script>
Benefits of Using CDN
In order to understand how using CDN can be beneficial to your web application, let's assume that you have a web server located in the US. Your web site developed in ASP.NET is hosted on that server and is using ASP.NET AJAX and jQuery libraries. Now let's further assume that clients from India are trying to use your web site. Without any CDN in the picture, all requests for the ASP.NET AJAX and jQuery libraries will be served by the single web server located in US. The files needed by the clients from India will be downloaded all the way from the US based web server.
Now consider the following figure.
Figure 1: Edge Cache Servers
Suppose that you have introduced CDN for ASP.NET AJAX and jQuery files and these libraries are now hosted in edge cache servers. Further assume that one of the edge cache servers is located in India itself. Now when a request originating from India is received, there is no need to fetch the files from the US based web server. Instead they can be readily served by the nearest edge cache server located in India. This will obviously improve the overall performance of your web site. If, for some reason, the edge cache server located in India is unavailable, the requests can be routed to other servers in the network ensuring high availability.
Consider a case wherein you have developed two web sites, both making use of ASP.NET AJAX and jQuery and you are not using CDN. If the same client tries to use both of the web sites, the client browser will download the ASP.NET AJAX and jQuery library files twice (once for each web site). See the following figure.
Figure 2: CDNDemo
Figure 3: CDNDemo2
The figure shows two web sites - CDNDemo and CDNDemo2 - making use of jquery-1.4.3.js file. Since the web sites are not using CDN, the browser will download the same file twice as shown by the FireFox cache above. With CDN in place both the web sites will refer the jquery-1.4.3.js file from the CDN. Now the browser will download just one copy of jquery-1.4.3.js because both the web sites are pointing to the same file.
Using Microsoft Ajax CDN for ASP.NET AJAX Files
Now that you know the benefits of using CDN, let's see how to use Microsoft Ajax CDN for referring ASP.NET AJAX files. If you wish to use ASP.NET AJAX, you must place a ScriptManager control on the web form. In ASP.NET 4.0, the ScriptManager control has EnableCdn property that governs whether to use Microsoft Ajax CDN or not. If set to True the ScriptManager control will emit <script> blocks that point to Microsoft Ajax CDN; otherwise scripts are served by the web server.
Figure 4: The ScriptManager control has EnableCdn property
If you see the HTML source of the web form after configuring the ScriptManager control to use Microsoft Ajax CDN, you will find script blocks as shown below:
Note that initially Microsoft Ajax CDN used to point to ajax.microsoft.com but now it has been changed to ajax.aspnetcdn.com. Though the former domain still works it is recommended to use the later domain in your websites.
Using Microsoft Ajax CDN for jQuery Files
Using jQuery library from Microsoft Ajax CDN is just a matter of adding a <script> block that points to the appropriate file from the CDN. The following <script> block shows how the jQuery library version 1.4.4 can be referred :
A CDN is a network of servers situated at key locations across the globe that serves cached copies of frequently accessed files. Microsoft Ajax CDN can be accessed at ajax.aspnetcdn.com. Using Microsoft Ajax CDN for ASP.NET AJAX and jQuery files can considerably improve the overall performance of your web site since the frequently accessed script files are served by edge cache servers located near the client end. A list of files hosted in Microsoft Ajax CDN can be found here.
About the Author:
Bipin Joshi is a blogger and writes about apparently unrelated topics - Yoga & technology! A former Software Consultant by profession, Bipin has been programming since 1995 and has worked with .NET framework ever since its inception. He has authored or co-authored half a dozen books and numerous articles on .NET technologies. He has also penned a few books on Yoga. He was a well-known technology author, trainer and an active member of Microsoft developer community before he decided to take a backseat from the mainstream IT circle and dedicate himself completely to spiritual path. Having embraced Yoga way of life he now codes for fun and writes on his blogs. He can also be reached there.