Main menu

Pages

What is a CDN?


How the CDN works?

Purpose of using a CDN

Faster site loading speed

The CDN service is a service for delivering files using a large number of edge servers (also called cache servers). Therefore, the companies that operate CDN services have cash servers all over the world. When an access to a CDN occurs, the CDN system automatically selects the server that is the closest physical distance to the accessing user. By delivering the file from that server, the response time can be shortened and the loading speed of the site can be expected to increase.

The difference in access speed depending on the physical distance depends on the data size, but as a guide, it is said that there will be a difference of several milliseconds to a dozen milliseconds in Japan and 100 milliseconds or more overseas.

Also, as a side effect, many CDN services support HTTP2, and even if your server does not support HTTP2, by using CDN, files are delivered from the cache server with HTTP2. , It may be possible to increase the reading speed.

Traffic distribution

Another big advantage is that the cache server on the CDN side handles some requests, which reduces the load on the server prepared by the company. Therefore, if the response of the server prepared by the company is slow due to high load, it can be expected to speed up by distributing the traffic.

How the CDN works

Cache server and origin server

In the CDN, the server (cache server) prepared by the CDN side and the server (origin server) prepared by the company cooperate to deliver the file.

Specifically, the user's request is first sent to the cache server, and if the file specified by the URL is not found in the cache server (such as the first request), the cache server retrieves the file from the origin server, the server. After saving to, it will be delivered to the user.

When the user accesses the same URL from the second time onward, the file in the cache server is delivered to the user without making a request to the origin server because the file is already stored in the cache server.

Therefore, the "acceleration" and "traffic distribution" in the previous section will be realized when the file exists on the cache server, that is, from the second access.

DNS settings

We explained in the previous section that user requests are first sent to the cache server, but by default the domain of the CDN service is used to access the CDN's cache server. Therefore, in order to access the cache server in the domain used by the service, it is necessary to change the DNS (Domain Name System).

* DNS: A system that can associate an IP address set on a server with an arbitrary name (space). For example, you can access the server with a character string by associating www.example.com with the IP address of the server, 192.41.192.129.

Specifically, add a new CNAME (canonical name) record to the DNS that manages your domain. CNAME is a function to set an alias for a domain, and you can register the alias of the domain you manage with this as the (sub) domain of the CDN service. This setting allows users to reach the cache server when they access the appropriate domain.

Cache coverage

Although it is a CDN cache server with great advantages, it is recommended to cache completely static files such as files and images with few changes, instead of caching all requests. The reason is that if you cache the HTML of pages whose contents change frequently (for example, pages with comment fields), when a user writes, even if it is changed on the origin server, the cache server This is because the contents before that will be delivered, so it may appear to the user that the changes have not been reflected.

So how do you specify the scope? Most CDN services allow you to specify the cache target using the HTTP method or the file extension. For example, since the POST method is used to add data, it can be limited to the GET and HEAD methods, and the data can be saved in the cache server only when the extension is jpg, css or png.

Disadvantages of CDN

Possibility of cash accident

As mentioned in "Cash target range", I mentioned that pages whose contents can change even with the same URL are not recommended for cache targets, but the reason is not only because the content is delayed, but also because of the leakage of personal information. Can also be connected.

For example, suppose a user visits the membership information page while the cache server is empty. Then, the cache server saves the HTML containing the member information of that user, and when another user accesses the member information page, the URL is the same, so the cache server does not contact the origin server and the previous user's The information will be returned as it is. In this way, the member information of a certain user will be visible to all users.

File update time difference

In "Cache target range", I mentioned that the cache target can be specified by the HTTP method or the file extension, but you can also set the cache retention period. With this, even a file that says "It is not changed frequently but may be changed about once a month" will be delayed for up to a week by setting the cache time to one week, but it is efficient on the cache server. You can also update the file while delivering the file to.

If the cache retention time is shortened, the cache contents will be updated with almost no delay in updating on the origin server, but the load on the origin server will not be so different from before the CDN was introduced, so the expected update time for each file It is necessary to select an appropriate value in consideration of. You can also manually delete the cache by specifying the URL if necessary.

Such settings are available for most CDN services, but the cache deletion time (the time from when a manual deletion request is received until all cache servers reflect the deletion request) and the fineness of the setting items. , The ease of setting differs for each service, so it is necessary to consider which service to use in consideration of that area.

Security measures by CDN

So far, we have explained the convenient aspects and usage of CDNs, but CDNs also have one aspect of security measures, and among them, the most popular one is the countermeasures against DDoS attacks. A DDoS attack is an attack method that puts a heavy load on a specific service by sending a large number of requests from a large number of machines. The reason why a CDN is a countermeasure against DDoS is that, as mentioned in "Traffic distribution", the cache server of the CDN, which has a large number of some requests, takes over. Thanks to this, even if your server has specifications that can not withstand the load of DDoS, most attack requests reach only the cache server, so you can avoid problems such as service down.

summary

So far, I have explained the advantages, disadvantages, and simple mechanism of CDNs. Once you understand how it works and how to use it, CDNs can enjoy various benefits such as load control and faster site without changing the origin server at all. Why don't you consider introducing it once while paying attention to the above-mentioned disadvantages?

Comments