This topic describes how to use veFaaS to trigger CDN refreshing and preloading based on TOS.
Scenario introduction
Most internet applications adopt a static-dynamic separation architecture, which stores static resources such as videos, audio, pictures, and JS scripts in Torch Object Storage (TOS) and uses CDN to accelerate access. When the resources in TOS are updated or upgraded, the content cached on CDN nodes needs to be synchronized to ensure visitors always get the latest update.
CDN allows refreshing and preloading cached data manually. However, manual operations are inefficient and error-prone. The veFaaS can help you realize automatic CDN refreshing/preloading when TOS changes, greatly improving maintenance efficiency.
Background information
- CDN: Content Delivery Network (CDN) provides content providers with transmission acceleration services for static content such as images, audio, video, and webpages, delivering a better user experience. For more information, see What is CDN.
- Refresh: delete cached content or mark it as expired on the CDN node. When visitors request that content on the node afterward, the node will pull the latest content from the original sites, return it to visitors, and save a copy as well.
- Preload: CDN nodes actively pull specific files (usually large files such as videos or installation packages) from the original site and cache them on the nodes. By doing that, the nodes can instantly start file transmission to visitors on receipt of requests. This reduces the delay caused by nodes fetching files from the original sites and avoids excessive download pressure on those sites when too many download requests for large files come in simultaneously.
Prerequisites
- Content delivery network
- Volcano Engine CDN has been activated.
- The site domains that need acceleration have been added and bound to your TOS bucket (i.e., using the TOS Bucket as the original sites). For details, see Configuring domains for acceleration.
- Permission for using veFaaS has been obtained.
- To use the logging feature, you need to apply for permission in advance to use Volcano Engine Log Service.
Limits
- Preloading and refreshing are only applicable to files, not directories currently.
- A function can either perform preloading or refreshing, but not both simultaneously.
- CDN has a daily refreshing/preloading quota limit. Tasks cannot be performed if the quota has run out. For details, see Refresh Quota and Preheat Quota.
Procedure
Step 1: Create a function using a code template
Use the vefaas-golang-cdn-refresh-preload code template to create a function, and configure the AK, SK, and other information required by the code as environment variables.
- In the top navigation bar, select the target region.
- On the Function page, click Create function.
- Click Create to create the function.
Step Two: Release the function
Release the function code and configuration.
- On the function details page, click Release in the upper right corner.
- Select Latest as the function version. Other options can be configured as required.
- Click Confirm to start releasing the function. The function is successfully released if you see the following information on the page.
Step 3: Configure the TOS trigger
Configure the rules of TOS trigger for the function.
- Switch to the Trigger tab and click Create trigger.
- Click Confirm to create the TOS trigger.
Validating results
- On the trigger tab, select the TOS trigger you just created, click the Bucket name to open the corresponding Bucket page. Click Upload file to upload a file from your local device.
- On the configuration page of the function, click Resource management tab. Click Logging to view the real-time log. You can find messages in the log indicating the refreshing/preloading task has been successfully submitted.
- If logging is enabled for the function, you can go to the Logging tab. On this tab, the same messages can be found as evidence of success.
- Go to the CDN console and click Refresh & Preheat in the left navigation bar, then select Operation Log tab on the page. The information shown in the following screenshot indicates the refreshing/preloading tasks have been performed successfully.
- Send GET requests to a URL which has been refreshed. The first request will get a response from the TOS of the original site, but the subsequent requests will get from the CDN cache.
- Send GET requests to a URL which has been preloaded. The first request will get a response from the CDN cache.
tip
You can determine whether the CDN cache is hit by the X-Bdcdn-Cache-Status field in the response header.
- X-Bdcdn-Cache-Status: TCP_HIT indicates a hit.
- X-Bdcdn-Cache-Status: TCP_MISS indicates a miss.