In the community, many users who use cloud development report that “CDN traffic consumption is like water”.

There was a wake up over the top:

Those who are too scared to use high quality images:

Then the question arises, how to gracefully control CDN traffic consumption in cloud development? This article will chat with you in detail!

01. Pay by volume and pay by pipeline

To understand this, let’s take a look at the traffic billing model for cloud development.

To put it simply, pay-as-you-go is: how many quantities you run out, you are counted; But it doesn’t limit how much you can run out at the same time, so there’s no limit to peak traffic.

If you need to run 100 meters at the same time, then the peak will give you 100 meters, run at the same time, and the final cost will be 100 meters of money.

And the pipeline pay is to limit to you how wide the “road”, according to the “road” narrow charge, for example, you choose 1M network road, then according to the price of 1M to collect money; In use, you can only measure up to 1 Megabyte, and no higher. This maximum speed is called bandwidth.

But you still have to pay for the “road” even when the hump is less. In the same example, you need to run 100M at the same time, but the speed only allows you to run 1M, so the amount can be completed after 100 seconds, and the final cost is 1M road rent * rental time.

However, in real projects, there will always be peak and low peak of business, and it is almost impossible to keep the flow rate at a constant rate, which will cause the problem of waste of resources that the pipeline charging has the speed ceiling, but is still charging when idle.

Cloud development as Serverless cloud native integrated back-end service, the flow provided is paid according to the amount, does not support pipeline payment, does not limit the flow speed at the same time, therefore, if the developers use unreasonable will lead to too fast flow rate, and then cause the situation of excess fast flow.

Having understood the difference between pay-as-you-go and pay-as-you-go, let’s get down to business and talk about CDN.

02. What is CDN traffic?

When we use cloud development, we see the term CDN almost everywhere.

CDN is also known as content distribution network. Generally speaking, it is to copy files in your main storage (source station) to storage points (CDN nodes) in various places. When users access this resource, they can get it directly from the nearest storage points (CDN nodes).

Cloud-developed cloud storage and static web hosting naturally support CDN acceleration, so your users download files through the client and run traffic is CDN traffic.

The above CDN traffic is only applicable to the situation that files are downloaded from the public network through various channels, such as web browsers loading websites, clients downloading resources, external systems requesting files, and opening files through temporary addresses. CDN traffic is consumed.

However, if files are accessed from the Intranet, CDN traffic is not consumed. For example, files are accessed through fileID in cloud functions.

03. What is CDN back source traffic?

What about old files stored in CDN nodes when there are file updates in our storage? Let me introduce a point here — cache time.

Across a copy of the cache time is here at the save point node (CDN), effective time, such as two hours by default, so every time a file copy in the save point around the valid time is two hours, more than the time before receiving the request, the save point node (CDN) will discard outdated old files, to the main storage (source station) request the latest file, The traffic generated by this request is called CDN source traffic.

The cache time should be neither too long nor too short. If the CDN cache interval is too short, the data on THE CDN node will often fail, leading to frequent return to the source, increasing the load of the source station, thus affecting the overall transmission efficiency. If the cache interval is too long, serious service problems such as delayed data update may occur.

Cloud storage offers very granular cache time Settings that can be set individually for a file, a path, or even a file suffix.

In addition, multiple cache rule Settings have priority policies, making deployment more flexible.

The cloud storage system performs policy calculation from back to front. For example, the following cache configurations are configured for a domain name of the cloud storage system:

Now request the resource /test/abc.jpg in this path, which matches from back to front as follows:

If all files in the first rule are matched, the cache time is 2 minutes.

Match number two. Miss.

If the third match is made, the cache time is 300 seconds.

Match number four, hit. Cache time is 400 seconds.

Match number five, hit, and the cache time is 200 seconds.

04. How to properly control CDN?

We need to grasp a principle:

Reduce the size and use caching.

By reducing the size, we mean that the multimedia files we need in the development of applications should be compressed as much as possible if there are no special requirements (such as the original pictures sent by the camera library). Only by reducing the size of service resources can traffic resource consumption be fundamentally reduced.

In addition, when uploading multimedia resources, users can still use the platform or framework capability to compress the resources before uploading, so as to ensure that the resources are optimized and put into storage, which will reduce a lot of burden when requesting download.

To make good use of caching means that the same user and the same resource should not be requested more than once, and the local caching capability of the client should be properly used to cache all fixed resources. When the user enters again, the cached resource is used directly.

Then give you a yunyun senior jin sentence: cache with good, PV effect is only UV amount.

And when specific use, everybody still needs the business circumstance according to oneself to hold reasonably. For example, if your resources change frequently, cache optimization is not a good choice. Your resources can’t be compressed, and if you want to keep them the same size, they are not suitable for compression optimization.

05, endnotes

The above tips are for you who can’t handle CDN traffic. If you feel that CDN consumption is like water, user activity is not much improvement, you may need to check your waste. Thrift is the traditional virtue of the Chinese nation, cloud development is good, but also need to save the use of oh ~

Author: Senior Student Yunyun