1
头图

Overview

CDN cache problem encountered when the front and back ends are not separated ( freemaker ). There are mainly the following two problems:

  • html obtained by the page request is the link of the old version number script
  • script script link is the new version number but the old script code is pulled

problem analysis

html obtained by the page request contains the old version number script link

Before analyzing the problem, we must first know the following knowledge points:

(1) freemaker page project is to back-end services to ftl processed into html returned

(2) During deployment, the ftl file will be traversed, and all the script links will be marked with the version number

// 构建前 supplierQuoteDetailPaging.ftl

<!DOCTYPE html>
    <head>
        <script type="text/javascript" src="${Global.getConfig("web.app.static.url")}/js/supplierQuoteDetail.js"></script>
    </head>
</html>


// 在`Jenkins`构建后会对请求静态脚本的`url`加上版本号 supplierQuoteDetailPaging.ftl

<!DOCTYPE html>
    <head>
        <script type="text/javascript" src="${Global.getConfig("web.app.static.url")}/js/supplierQuoteDetail.js?version=1638706227856"></script>
    </head>
</html>

(3) The back-end is a cluster service, and the deployment adopts rolling release, which means that the node service is updated batch by batch during deployment, until all the instances in the cluster are updated to the new version, instead of being updated in full at once

When the project is still deployed, because the service uses rolling release, during this period the new service and the old service will exist at the same time. If you visit the page at this stage, the page interface may hit the old service or the new service. When it hits the old service, the script html is marked with the old version number; when the deployment is completed, all instances in the group All are updated to the new version, the page request hits the new service, and the script html is marked with the new version number.

部署后html中不是最新的版本号.png

Solution: Refresh the page after the project is deployed

2, script script link is the new version number but the old script code

Normally, after deploying the project, the browser requests the static script file on CDN CDN is no script file corresponding to the new version number in the 061b05e4562413 cache, it will pull the new script from the back-end service, and then CDN When doing a cache, the following script request is directly returned CDN

However, if the deployment is not completed and the browser is accessed, at this stage, the new service and the old service exist at the same time. When the script corresponding to the new version number CDN , it will go to the service request. The request hits the old service (the service response has nothing to do with the version number), the old service returns the old script, and then CDN caches the old script corresponding to the new version number, so that each subsequent request pulls the script cached on CDN Therefore, the above-mentioned problem arises.

新版本号未拉取到新脚本.png

Our actual solution is that maintains two release tasks (repetitive batches) for this type of project, that is, deploys twice. Let's first assume the mapping relationship between the version number and the script of the three deployment process:

version numberscript
Last deploymentV1.0J1.0
Repeat batch 1V1.1J1.1
Repeat batch 2V1.2J1.2
Note: There is no change in the codes of repeated batch 1 and repeated batch 2, and the J1.1 J1.2 exactly the same

If the repeat batch 1 deployment is not completed, the access goes through the wrong hit process, CDN caches <V1.0,J1.0> , then any subsequent access to get the wrong script code. When the rolling release of the repeated batch 1 service is completed, the scripts on the service are all J1.1 . If the repeated batch 2 is deployed again, the request when the deployment is not completed may hit the script J1.1 or J1.2 . J1.2 J1.1 and 061b05e456261b are exactly the same, the CDN cache <V1.0,J1.1> or <V1.0,J1.2> , the results are correct; if the request is initiated when the deployment is completed, the hit script is J1.2 , CDN cache <V1.0,J1.2> , the result is also correct.

In addition, there are some other solutions, such as CDN refresh and preheat

CDN refresh and warm up

CDN provides resource refresh and warm-up functions. Through the refresh function, you can force the CDN node to return to the source and obtain the latest files; through the warm-up function, you can warm up popular resources during peak business periods to improve resource access efficiency. The difference between cache refresh and cache warm-up is as follows:

  • Cache refresh: After submitting the cache refresh request, CDN node will be forced to expire. When a user requests a resource CDN CDN will directly return to the origin site to request the corresponding resource and return it to the user, and cache it.
  • Cache warm-up: After submitting the cache warm-up request, the origin site will actively cache the corresponding resources to the CDN node. When the user requests for the first time, he can CDN node, and there is no need to go back to the origin site to request.

There are two types of refresh: URL refresh and catalog refresh.

  • URL refresh: By providing files in the directory, the CDN node is forced to return to the source obtain the latest file, within 5 minutes of the effective time, the API interface RefreshObjectCaches .
  • Directory refresh: By providing the directory and all files in the directory, the CDN node is forced to return to the source to obtain the latest files. The effective time is within 5 minutes, and the API interface is the same as above.

calling this interface on

  • Support post request, the parameters use the form form.
  • Refresh preheating interface includes RefreshObjectCaches refreshing interface and PushObjectCache preheating interface.
  • The same ID can submit up to 2000 URL refreshes and 100 catalog refreshes per day.
  • Each request can only submit up to 1000 URL refreshes.
  • Up to 50 requests per second.

img

In addition to the above figure, there is a parameter Action with a value of RefreshObjectCaches , and the return value is consistent with the preheat interface, as shown below:

img

Sample code:

https://cdn.aliyuncs.com?Action=RefreshObjectCaches
&ObjectPath=abc.com/image/1.png\nabc.com/image/2.png
&ObjectType=File
&<公共请求参数>

warm support only full URL warm-up, warm-up is not supported directory , the specified resource initiative preheated to CDN of L2 two nodes , the user can directly access the first cache hit. Effective time is within 5 minutes, API interface PushObjectCache .

Call PushObjectCache to actively warm up the content of the origin site to the L2 Cache node. Your first access can directly hit the cache to relieve the pressure on the origin site. Before calling this interface, please note:

  • Support post request, parameters use form form.
  • Refresh and preheat interface includes RefreshObjectCaches refresh interface and PushObjectCache preheat interface.
  • The same ID can submit up to 500 URL per day for preheating.
  • Each request can only submit up to 100 URL preheating.
  • Up to 50 requests per second.
  • The maximum limit of the preheating queue for a single ID is 100, which is preheated according to the order of submission. If the queue tasks accumulate to 100, you need to wait for the submitted warm-up request to be completed before submitting a new one, so as to keep the queue size never exceeding 100.
  • CDN of L2 Cache node set up in L1 Cache between the node and the source station, the source station to help you relieve stress.

img

img

http(s)://cdn.aliyuncs.com/?Action=PushObjectCache
&ObjectPath=abc.com/image/1.png\nabc.com/image/2.png
&<公共请求参数>

Q&A

Q: What should I do if the cache fails to warm up?

A: The possible reasons for the cache preheating failure are:

  • When you perform centralized preheating of a large number of files, it may cause your origin site's bandwidth resources to be full. Please try to perform the preheating in batches as much as possible. You can also increase the preheating efficiency by expanding the bandwidth of the source station.
  • Check whether the cache expiration time corresponding to the resource is 0, if it is 0, disabling the cache will cause preheating failure;
  • cache-control configuration of the source site private、no-cache、no-store will cause CDN fail to cache and cause preheating failure. If not configured, the default is private.
  • Currently, preheating directories, dynamic files, and cache expiration time of url are not supported.
  • When you preheat multiple URL separated by ";", you need to switch to the English state, the ";" in the Chinese state will cause preheat failure

Q: After refreshing and warming up operations, why the accessed files are still old?

A: It may be that the interval between refreshing and warming up your cache is too close, causing the refresh to fail. It is recommended that the interval between refreshing and warming up is more than five minutes.

refer to:
HUAWEI CLOUD CDN
CDN refresh and warm-up


记得要微笑
1.9k 声望4.5k 粉丝

知不足而奋进,望远山而前行,卯足劲,不减热爱。