Automated CDN cache purge for OSS updates
In modern web applications and static site deployments, front-end resources are typically hosted on Object Storage Service (OSS) and distributed through a CDN. When static resources in OSS are updated, users might still access stale content if the CDN edge cache is not purged. This solution leverages the OSS trigger of Function Compute (FC) to automatically invoke a function when a file changes. The function then calls the CDN purge API, automating the cache purge process. This approach reduces operational costs while improving system responsiveness and reliability.
Key benefits
This solution uses the event-driven architecture of Alibaba Cloud Function Compute and Object Storage Service to automate CDN cache purging. Its key benefits include:
-
Intelligent debouncing for API stability
To avoid overloading the CDN purge API during high-frequency file uploads or changes, the solution uses a "buffer + delayed aggregation" strategy:
-
File change events do not immediately trigger a purge request. Instead, the solution writes change records to a temporary buffer file.
-
Within a one-minute time window, multiple changes are batched and merged into one or a small number of purge tasks.
-
This effectively prevents API overload in scenarios such as CI/CD batch deployments and automated syncs, which improves overall system reliability.
-
-
Flexible management for multiple domain names
You can deploy different Function Compute triggers to set distinct purge policies for different OSS buckets and paths:
-
Each domain name has its own buffer files and aggregation task files, preventing interference.
-
You can configure dedicated purge policies for different business lines, such as web, H5, and mini-programs.
-
This simplifies permission isolation, auditing, and troubleshooting, meeting enterprise-level multi-tenant requirements.
-
-
High reliability and observability
The system is designed with fault tolerance and observability in mind:
-
If an aggregation or API call fails, the task state is persisted to allow for automatic retries.
-
All operations—including event reception, buffer writes, aggregation, and API calls—are logged in detail to Simple Log Service (SLS). The logs contain key information such as timestamps, file paths, domain names, and execution results.
-
Operators can quickly troubleshoot issues, which ensures eventual consistency after every change.
-
-
Low cost, zero maintenance, and high reusability
The fully serverless architecture significantly reduces resource and personnel costs:
-
No servers need to be provisioned or maintained. You pay only for what you use, based on invocations, with zero cost when idle.
-
Intermediate buffer and aggregation files have an automatic lifecycle policy, such as expiring after one hour, to avoid long-term storage costs.
-
The core logic is encapsulated in a lightweight function that relies only on standard cloud services, making it easy to migrate and reuse across other projects or regions.
-
How it works
This solution uses the event-driven mechanism of Alibaba Cloud Function Compute and Object Storage Service to automatically purge the CDN cache. The workflow is as follows:
-
Event listening: An OSS event notification rule on a bucket triggers an FC function when an object with a specified prefix or suffix is uploaded (PutObject), overwritten (PostObject), or deleted (DeleteObject).
-
Function execution: The triggered FC function receives the OSS event message, parses the full URL of the changed object, and merges the URLs into an aggregation file in one-minute batches. The aggregation file is stored at a specified path in OSS.
-
Purge submission: The function reads the URLs of the changed objects from the specified path and calls the CDN API to submit a cache purge task. This ensures users receive the latest content.
-
Logging and monitoring: All trigger and execution processes are logged to Simple Log Service for auditing and troubleshooting.
Billing
This solution involves the following Alibaba Cloud products and their main billable items:
-
Function Compute (FC): Deployment is free. You are billed on a pay-as-you-go basis for resources such as invocations, execution duration, and outbound internet traffic. For more information, see Function Compute billing overview.
-
Object Storage Service (OSS): Deployment is free. You are billed on a pay-as-you-go basis for resources such as storage space, traffic, and number of requests. For more information, see Object Storage Service billing overview.
Procedure
-
Log on to the Function Compute console. In the navigation pane, click AppCenter.
-
In the Basic Application Templates section, select File Processing. Find and click the Automatically Purge the CDN Cache for OSS File Updates template.
-
On the overview page, review the application's functionality and notes. Then, click Deploy Project in the lower-left corner to go to the parameter configuration page.
-
Configure the parameters as described on the page, then click Deploy Project in the lower-left corner and wait for the deployment to succeed.
Parameters include: region (for example, China (Hangzhou)), OSS bucket name (Required), OSS events (defaults to
oss:ObjectRemoved:DeleteObject,oss:ObjectRemoved:DeleteObjects), RAM role ARN (Select AliyunFcDefaultRole and ensure the role has the AliyunOSSFullAccess and AliyunESAFullAccess policies attached. You can click One-Click Authorization to grant them), OSS file directory, OSS file suffix, ESA site ID (Required), ESA accelerated domain name (Required), OSS temporary directory (defaults tooss-esa-purge-buffer/), and OSS aggregation directory (defaults tooss-esa-purge-final/).
Verify the results
After the deployment is complete, verify the results as follows:
To optimize performance, the CDN purge process may take up to one minute to start after an OSS object changes. This delay is due to the debounce aggregation window.
-
Log on to the CDN console.
-
In the left-side navigation pane, click Purge and Prefetch.
-
On the Records tab, view the new purge tasks.
When the Status of a task is Completed and its Progress is 100%, the URL has been successfully purged.