Use CDN to reduce server load

Story begins

Recently, I attended a Rails class of 4th year IT students. During that session, students learn about Twitter's Bootstrap library . There are two ways to import the library into a web page, one is to use a link at the "end of the world" like this.

2 is on the homepage of Bootstrap , download to your server and import the file from its own server

When I asked to compare two ways, which is better, most of the students thought that the second way was better, the reason was because of it: "Faster?", "My server only sends requests 1 once is done "@@

The second reason is definitely wrong, because the first request will receive the HTML file, the browser will process the file and find the link to continue sending the request, in this case, at least 2 requests, in which request stuff two to retrieve the CSS file. So the first reason "Faster?". Is it true? Is it because the same server returns the HTML file so I think it will be "fast"?

The answer in most cases is "no"! (Only mostly … ?)

The reason why, we will learn about the link "take the first place" there! https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css

That's CDN !

What is CDN ?

CDN stands for Content Delivery Network , roughly translated as "Information Distribution Network". CDN is a system of distributed servers around the world, bringing content to users as quickly as possible based on the geographical location of users and servers hosting information.

CDN plays an important role in increasing the speed of data transmission to users, especially with websites intended to grow globally. A simple example is when a user in Hanoi wants to receive a file, if the file is transferred from the US, it will take half the world to reach the destination. Obviously the road is too long and will take a lot more time if it is sent from Singapore or Japan.

Screen Shot 2016-09-28 at 10.14.18 AM

The image above is a network model of the cloudflare.com website, a CDN service website. There are many servers scattered around the world, and it would be lucky if you live in Hanoi and receive files from a server in Singapore, instead of from Chicago @@

How does CDN work?

Principle of operation

The basic principle of CDN is scattered data in many servers around the world (static files like images, videos, Javascript, CSS, …). Server with geographical distance to the nearest user will be used to send data to the user.

Screen Shot 2016-09-28 at 10.17.10 AM

The figure above shows a simple model of CDN. There is a main server (Original server) that acts as an intermediary to receive requests, data, and distribute requests and data to edge servers. The user closest to the next server will receive the data returned from that server. The word "server" in the above figure is only simulated, because the designated points will have a data center with dozens of servers operating day and night, with the role of ensuring CDN is always active.

CDN was born about 15 years ago but only recently has it really grown when the number of users and internet resources increased constantly and the design has made certain breakthroughs. Some CDN service providers familiar with programmers are BootstrapCDN of Twitter, Amazon CloudFront of Amazon, Microsoft Windows Azure CDN , …

The question is how can the CDN find the server closest to the user to allow the file to be sent from, not from another server. Obviously we only have a single link, sending a request from the browser, while there seems to be a lot of servers?

We also learn how to route in CDN.

Request Routing

According to the analysis of WindowsITPro website, CDN has three routing methods: DNS Request Routing ( Routing using DNS), Transport Layer Request Routing ( Routing at transport layer), and finally Application-layer Request Routing (Routing at the application layer). All three methods are quite complex and require high knowledge of network protocols. In addition, this is just an analysis of the researchers, not of the CDN service provider itself (I know that he always advertises his algorithm first @@), so I will only "Try" to explain a technique that I understand the most, is Transport Layer Request Routing .

Screen Shot 2016-09-28 at 10.27.14 AM

The above figure is a general description for Transport Layer Request Routing . To match the above CDN model, we temporarily consider POP1 as Original Server, and POP2 as an Edge Server. Transport Layer Routing System (TLRS) is available at each server. The user's request path will be as follows.

  1. After receiving the results from the DNS Server, the user sends the request to the IP address of POP1 (Original Server). This is a virtual IP address (Virtual IP – VIP)
  2. The TLRS on the POP1 layer will check the user's information, check out its Edge Servers to see if the server is the most appropriate server to return the packet. After determining the appropriate Edge Server, POP1 will forward the user's request to the Edge Server (POP2).
  3. After checking the information sent from POP1, POP2 will change its source IP address according to the virtual IP of POP1, then send the file back to the user. When receiving the file, the user (more precisely the user's browser) still believes that it receives it from POP1 but continues to send the next request (if any).

The above is a simple explanation for the routing mechanism of CDN. Interested readers can learn more by themselves ?

The role of CDN in reducing server load

Above readers have obtained the basic concept of CDN. Perhaps you have envisioned the relative benefits that it brings, I would like to summarize the benefits of CDN in reducing the server load as well as improving the user experience, bringing a speedy website. load faster.

  • Speed ​​up, save bandwidth
    As mentioned at the beginning of the article, with each request to the links contained in the first HTML file, including CSS, Javascript, video, images links, the browser will send a request to that address. In case we use the CDN, instead of the request on our server, the browser will send the request to another server. Thereby reducing the traffic saved to the current server. And as mentioned above, this server will be optimized to be "closest" to our computers, thereby ensuring the best speed.
  • Files can be stored in the browser cache
    Today, many websites use CDN. For example, jQuery or Bootstrap. If the user visits the site using your jQuery library, but the computer they have visited on a website also uses that library with the CDN provider like you, their browser will not need to send Where the request goes, it will reload from the cache. Thereby, the speed improved unexpectedly ?
  • Increase the number of files loaded at a time
    There is an interesting thing about browsers, which is that browsers will limit the number of requests to a domain at a certain time, this number is usually 4. So if you have a request for number 5 to the same domain, The browser will wait for at least 1 of the 4 requests running to be completed before going to request 5. Therefore, by dispersing the request to another server (CDN), you not only reduce the number of requests on your server, but also give the browser a chance to make another request at the same time.
  • Versions are managed
    Version control is the available function of CDN services. As a web developer, you can easily identify which file with which version to send to users without worrying about loss or error.
  • Extremely good infrastructure
    Believe it or not, no matter how good a hosting service is, I am still not sure if it is better than the giant Google, Amazon, and Microsoft. Therefore, you can fully believe that the file will always be delivered to the destination when using the services provided by top CDNs. Refer to the popular CDNs here: http://www.cdnreviews.com/popular-cdns/
  • User Analytics (User Analytics) Another great thing is that CDN providers also add user traffic analysis functionality. Rent a CDN service, which means you have a user analysis service, to know who, from where, and when to access your service. Thereby there are appropriate measures to improve the system function

A few notes about using CDN

Go back to the story question at the beginning of the article, when I concluded that, if you put the CSS file on your server, it will usually load slower than using the file from CDN. Why is it "common" when above we have listed a series of benefits of CDN? OK, I would like to give some notes according to personal experience and understanding.

  • CDN is not always faster, if the files are accessed only through your website (the ability to cache will be low), or when you have a server fast enough so the speed can be faster than the server's CDN. (For example, if the user accesses from Vietnam and the server you set in Vietnam, it may be faster than the CDN, because in my opinion, the latest CDN server is located in Singapore, not Vietnam).
  • CDN is not always a good choice, if you only want to target users to a certain country. In other words, when you don't intend to globalize your website, using CDN is redundant and unnecessary. Imagine that you have a website for Vietnamese people, but put a promotional video in Los Angeles server, then … don't know what to do @@ (if your service is also directed to overseas Vietnamese, it is reasonable. ?)
  • Not all companies can afford to build their own CDN system, because it's too complicated, requiring participation of many stakeholders in many countries. Therefore, the use of available CDN services is an appropriate option. However, you should make the right choice, besides the price, the security issues are also important. The danger can be stalking when your Javascript file is edited, installing malicious code when reaching the user side, endangering both your users and your website. Therefore, I recommend that you do not use it alone. If you use it, you should choose a reliable supplier.
  • The last thing, when you deploy the server with static files (Javascript, CSS, Video, Photo, etc.) needs to be pushed onto the CDN server, which means your deploying work adds a level of complexity and a level of risk. ro, so make sure your deploy mechanism is as good as possible to avoid causing unnecessary errors.

References

ITZone via Viblo

Share the news now