cdn--or not?

Source: Internet
Author: User
Tags free cdn jquery library

Recently learning bootstrap, in the knowledge of the search bootstrap see someone asked Bootstrap basic package size, the impact on performance will be very large, see two ways to reduce the impact on performance:

    1. Selectively partially loaded, bootstrap with less and sass versions, can load only the required parts according to business requirements, bootstrap for different interactive functions also provides the corresponding JS file, do not have to directly load bootstrap.min.js
    2. Using CDN to load

I haven't learned less and sass, so the first way I'm not, but the concept of CDN I used to watch "JavaScript DOM programming Art" This book has been approached:

Content distribution Networks (CDN, Content Delevery network) can solve the problem of distributing shared libraries. A CDN is a server-made network that is used to store some common content in a decentralized way. Each server in the CDN contains a copy of the library, which is distributed across different countries and regions of the world in order to achieve bandwidth utilization and speed up downloads. When a browser accesses a library, a public URL is used, while the bottom of the CDN provides the appropriate files through the most recent and fastest server, which solves the bottleneck problem in the whole system.

One of the respondents to this question shared an article: "7 reasons to use a Content Delivery Network", I google this article, found that the same author also wrote another article: "7 reasons not for use a Content Delivery Network ".

So this CDN, in the end we use or not? I read these two articles and summarize here:

Seven reasons to use a CDN

    1. Browser from the server download CSS, JS and pictures and other files to connect with the server, and most browsers for the same domain name for the download file of the number of concurrent connections limited to 4, which means that if you want to download the fifth file must wait for one of the first four files have been loaded, if the first 4 files are large, The fifth file will be waiting for a long time, the entire page loading speed is limited by this. CDN can be used to load files through different domain names, so that the number of concurrent connections to download files greatly increased.
    2. A type of jquery library file is now widely used, and if the user's browser visiting your site is already loaded with jquery through the same CDN as your Web site, it will not have to be re-downloaded because the jquery file is already cached.
    3. Perhaps your site hosts are performing well, but should not be more capacity and scalability than big companies like Google, Microsoft, and Yahoo, and they offer a CDN with better availability, lower network latency, and packet loss rates.
    4. A CDN can provide a local data center so that users who are far away from your site's main server can download files quickly and easily.
    5. Allows you to connect to a specific version of the CSS file or JS library file, you can request the latest version according to demand.
    6. Many commercial-paid CDN can provide usage reports, which can be supplemented by your own website analysis reports.
    7. CDN can allocate load, save bandwidth, improve the performance of your website, reduce the cost of hosting your website, usually free of charge.

Seven reasons to use a CDN

    1. In the development phase, the CDN file cannot be loaded if it is in a disconnected environment.
    2. Some free CDN files may be full-featured packages or library files, may be more than hundreds of KB in size, and many times the site only needs to use some of them, you can use your own optimized, smaller CSS and JS files, let users download and execute faster, Of course you can put your own files on a private CDN, but this will require additional effort and expense.
    3. Although some popular CDN files are more likely to be cached beforehand, but not necessarily, some mobile devices may be small and inefficient, and the benefits of CDN are not obvious, especially if you can store files smaller than CDN files on your local server.
    4. Due to geographic, legal, policy, and commercial barriers, your area may block the domain name or IP address of some popular free CDN services.
    5. When there is a problem with the CDN, there is a backup plan, which is your local file, and this stability-aware redundancy will increase the workload and complexity of the development.
    6. If security is important to your site, do not use a public CDN, because when you remotely request files from the CDN, your Access source information is sent in the past, and some remote JS files may be modified to collect your user or system information, and when you use the HTTPS protocol, The CDN you can choose is even more limited.
    7. When you control the file to Google, Microsoft, Amazon or other big internet companies, you will consider their collection of your information, which may be over-thought, but in the Internet this line of suspicion is not bad.

Do you want to use a CDN or not?

After reading the author of the above two articles, I basic to the CDN good and bad have a preliminary understanding, and then I saw the author wrote a third article: "Should you use a Content Delivery Network?" So should we use a CDN anyway?

  1. Several cases where the CDN is not explicitly used:

    1. You are building an internal network application that is not connected to the external Internet;
    2. For applications like the banking system, security and privacy are top priorities, leaving all source files and server locations fully in their control;
    3. Applications that you develop for a company or a country that restrict access to certain CDN domain names or IP addresses.
  2. CDN Performance gains for low-traffic small sites are not obvious, and if you don't need high-bandwidth video and audio files, it might be easier to put your files together.

  3. For high-traffic sites, CDNs can greatly improve performance, but if your users are mobile-focused, the smaller files they might have optimized are faster to download and execute than the large files on the CDN.

  4. In practice, the speed at which a user loads a page with a CDN file and a local server file is compiled through JavaScript to determine whether a CDN or local file is selected over time.

  5. For important files, it is best to provide redundancy for local files in case the CDN file is not available. Take jquery for example:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery1.4.3/jquery.min.js"></script><script>!window.jQuery && document.write("<script src=\"scripts/jquery-1.4.3.min.js\">" + "<\/scrript>")

    This is done by judging whether the Window.jquery global object exists to determine if jquery was successfully loaded via a CDN, unsuccessfully document.write loading the local jquery file by means of the method, and note that the escape character ' \ ' is used to prevent the browser document.write from going inside the method. </script>"as an end tag.

  6. Some files that are not critical to the system, such as font files, CSS resets, JS widgets, background images, and so on, may be suitable for use with CDNs, because even if CDN files are unavailable, the site can be degraded smoothly.

Summarize

See here, I think you also feel that the CDN is good, just to use it well, or should follow some of the principles mentioned above, simply put, in practice, some popular library files and font files using CDN is good, for large sites, using a private CDN can also greatly improve speed and reduce costs, But if your application has high security, privacy, and reliability requirements, make a tradeoff between CDN performance improvement and security considerations.

Resources

    1. How feasible is bootstrap used to build a front-end layout for a large internet site?
    2. JavaScript DOM Programming Art 2nd edition
    3. 7 reasons to use a Content Delivery Network
    4. 7 Reasons to use a CDN
    5. 7 reasons not to use a Content Delivery Network
    6. Should use a Content Delivery Network?
    7. What is a CDN? What are the advantages of using a CDN?

cdn--or not?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.