In the WeChat open community, a lot of partners who use cloud development have encountered the situation of “CDN traffic consumption like flowing water”.
There is an excess of waking up:
Those who are “scared” by the high quality image loading:
Do not panic if you encounter the above situation, it is likely that your posture is wrong!
So the question is, how to gracefully control CDN traffic consumption in cloud development? This article will talk with you in detail!
Pay by volume and pay by pipe
To understand this, let’s take a look at the traffic billing model for cloud development.
In simple terms, according to the amount of billing is: you have a number of out, you calculate the amount of a small amount; But it doesn’t limit the amount of traffic you can run at one time, so there’s no limit on 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 is 100 meters.
Pipeline pricing limits the width of the “road” you can drive. For example, if you choose a 1M network road, you will be charged as much as 1M. When you’re using it, you can’t go any faster than 1 Megabyte, and that maximum speed is called the bandwidth.
But you have to pay for the road even when it’s not too low. In the same example, you need to run 100 meters at a time, but the speed is only able to drive you to 1 meter, so it will take you 100 seconds to complete the journey. The final cost is 1 meter of road rent * the length of the rental time.
However, in real projects, there will always be business peaks and peaks, and it is almost impossible to keep the flow at a constant rate all the time. This will cause the problem of resource waste when the pipeline billing has a ceiling of speed but is still billing when it is idle.
Cloud development as Serverless cloud native integration back-end service, the traffic provided is billed according to the volume, does not limit the traffic speed at the same time, therefore, if the developers use the unreasonable will lead to too fast flow, and then cause the “blink of an eye, the traffic is excessive” situation.
Now that we know the difference between pay-as-you-go and pay-as-you-go, let’s move on to CDN.
What is CDN traffic?
When we use cloud development, we see the term CDN almost everywhere.
CDN is also called Content Delivery Network (CDN). Generally speaking, it is to copy the files in your main storage (source station) to local storage points (CDN nodes). When a user visits this resource, it can be obtained directly from the nearest storage points (CDN nodes).
Cloud development for cloud storage and static website hosting naturally supports CDN acceleration, so your users download files through the client and run the traffic is CDN traffic.
The above CDN traffic is only applicable to the situation of speeding up the download of files from the public network through various channels. For example, the browser loads the website, the client downloads resources, the external system requests files, and opens files through various visits from temporary addresses all consume the CDN traffic.
However, if the file is accessed on the internal network, CDN traffic consumption is not taken, such as accessing the file through fileID in the cloud function.
What is CDN backflow?
When we have file updates in our storage, what about the old files stored on the CDN node? One point of knowledge here is 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 back 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, which increases the load of the source station and then affects the overall transmission efficiency. If the cache interval is too long, it can cause serious business problems such as delayed data updates.
Cloud storage provides very granular cache time Settings for a file, a path, or even a file suffix.
In addition, there are priority policies in the multiple cache rule Settings, making the deployment more flexible.
Cloud storage is configured in a back-to-front configuration mode for policy calculation. For example, a cloud storage domain has the following cache configuration:
Now request the resource /test/abc.jpg in this path, which matches from back to front as follows:
Match all files in the first one, hit, the cache time is 2 minutes.
Match second, miss.
Match the third item, hit, the cache time is 300 seconds.
Match fourth, hit, the cache time is 400 seconds.
Match the fifth, hit, the cache time is 200 seconds.
How to control CDN reasonably?
We need to follow a principle:
Reduce the size and make good use of the cache.
Downsizing means that when we develop an application, we need to compress the multimedia files as much as possible if there is no special requirement (such as sending the original image of the camera gallery). Only by reducing the size of business resources can we fundamentally reduce the consumption of traffic resources.
In addition, when users upload multimedia resources, they can still use the platform or framework to compress the resources and then upload them, ensuring that the resources are optimized into the storage, which will reduce a lot of burden when they request downloading.
The good use of cache means that the same user, the same resource as far as possible do not request more than once, to reasonable use of the local cache capacity of the client, all fixed resources will be cached. When the user re-enters, the cached resource is used directly.
For example, the official WeChat applet provides a picture cache configuration, opened after all pictures are cached, the next time read the same resource, directly read from the cache.
Documentation link: http://mrw.so/6wT3TR
To give you a network golden sentence: cache with good, the effect of PV with only the amount of UV.
And in the specific use, we still need to grasp according to their own business situation. For example, if your resource changes very frequently, it is not suitable for cache optimization; Your resource cannot be compressed, and if you want to maintain its original size, it is not suitable for compression optimization.
conclusion
The above strategy to you can not hold the CDN traffic partners, if you feel that CDN consumption such as water, users are active but not much improvement, you may need to have a good look at where they waste. Diligence and thrift are the traditional virtues of the Chinese nation. Although cloud development is good, it also needs to be economical in use
Small program · Cloud development “error monitoring” function award research
We sincerely invite all cloud developers to participate in the research on the “Error Monitoring” function of applet and cloud development, and we will send you a small gift if you participate.
Looking forward to your valuable suggestions, please scan the QR code in the picture below to participate!
Product introduction
Cloud development (TCB) is the cloud native integrated development environment and tool platform provided by Tencent Cloud. It provides developers with high availability, automatic elastic expansion of back-end cloud services, including computing, storage, hosting and other serverless capabilities, which can be used for integrated cloud development of a variety of end-end applications (small programs, Official account, Web application, Flutter client, etc.), which helps developers to build and manage back-end services and cloud resources in a unified way, avoiding tedious server construction, operation and maintenance in the application development process, and allowing developers to focus on the implementation of business logic with lower development threshold and higher efficiency. Open cloud development: https://console.cloud.tencent.com/tcb?tdl_anchor=techsite product documentation: https://cloud.tencent.com/product/tcb?from=12763 technical documentation: https://cloudbase.net?from=10004 technology exchange and Q group: 601134960 Latest news pay attention to WeChat public account [Tencent Yunyun Development]