Time-to-live (TTL) refers to the amount of time that a resource is cached on Alibaba Cloud CDN edge nodes. The default TTL value for cached resources is 10 seconds and the maximum is 3,600 seconds. Expired resources are automatically removed from edge nodes. Requests that attempt to access these resources are considered invalid and redirected to the origin server. Then, Alibaba Cloud CDN retrieves the requested resources from the origin server and caches them on the edge nodes. You can set a TTL value for static resources based on file directories or file extensions.

Background information

The following figure shows the cache policies of the resources that are cached on edge nodes. Cache policies
Whether a cache policy is set on the origin server Whether a cache policy is set in the console Caching behaviors on edge nodes
Yes, the cache policy is set to cache-control:no-cache or cache-control:no-store. N/A Follow the cache policy that is set on the origin server. Resources retrieved from the origin server are not cached on edge nodes. Each request is redirected to the origin server. This decreases the content delivery efficiency.
Yes, but the cache policy is not set to cache-control:no-cache or cache-control:no-store. Yes
Follow the cache policy that is set in the console. The priorities of the cache policies in the console are in the following descending order: weight > file extension > directory.
Note
  • A resource retrieved from the origin server by Alibaba Cloud CDN may match two cache policies that have the same weight. One cache policy is based on file extensions and the other is based on directories. In this case, the cache policy that is based on file extensions applies because it has a higher priority.
  • If a resource matches two cache policies that have the same settings, for example, both are based on files extensions or directories, and have the same weight, the cache policies are applied in a random order.
No Follow other cache policies that are set on the origin server.

If the response header carries the cache-control and expires cache policies, the cache-control policy has a higher priority than the expires policy. In this case, only the cache-control policy takes effect.

No Yes
Follow the cache policy that is set in the console. The priorities of the cache policies in the console are in the following descending order: weight > file extension > directory.
Note
  • A resource retrieved from the origin server by Alibaba Cloud CDN may match two cache policies that have the same weight. One cache policy is based on file extensions and the other is based on directories. In this case, the cache policy that is based on file extensions applies because it has a higher priority.
  • If a resource matches two cache policies that have the same settings, for example, both are based on files extensions or directories, and have the same weight, the cache policies are applied in a random order.
No Follow the default cache policy of Alibaba Cloud CDN. By default, the minimum TTL value is 10 seconds and the maximum is 3,600 seconds. The default cache policy is:
  • If the response from the origin server contains the Last-Modified header, which indicates the last time when the resources on the origin server were updated: default TTL value = (current time - last_modified) × 0.1.
  • If the response from the origin server does not contain the Last-Modified header but contains the ETag header, the default TTL value is 10 seconds.
  • If the response from the origin server does not contain the Last-Modified or ETag header, the retrieved resources are not cached on edge nodes.

Scenarios

Edge nodes respond to requests based on the cache status. If the requested resource is not cached on edge nodes, the request is redirected to the origin server to retrieve the resource. After the resource is retrieved from the origin server, an HTTP 2XX status code is returned to the edge node. The retrieved resource is cached on the edge node and returned to the client.

TTL values apply to the following scenarios:

  • The resources on the origin server are updated but the cache of edge nodes is not updated.
  • Requests are frequently redirected to the origin server because the resources cached on edge nodes frequently expire. This causes the cache hit ratio to decrease.
  • The loading time is increased.

Usage notes

  • To differentiate resources before and after they are updated on the origin server, we recommend that you use different names for resources before and after they are updated when you configure a TTL value for static resources. You can use version numbers to differentiate the resources. For example, you can use img-v1.0.jpg and img-v2.1.jpg to name a resource before and after it is updated.
  • The TTL value affects the amount of network traffic redirected to the origin server. The data transfer fee varies based on the amount of back-to-origin network traffic. Set a proper TTL value based on your business requirement. A small TTL value may cause edge nodes to frequently redirect requests to the origin server and increase the amount of network traffic on the origin server.
  • Cached resources that are infrequently requested may be removed before they expire.

Procedure

  1. Log on to the Alibaba Cloud CDN console.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column of the domain name.
  4. In the management pane of the domain name, click Cache.
  5. On the Cache Expiration tab, click Create Rule.
  6. In the Create Expiration Rule dialog box, set the parameters.
    Set a TTL value
    Parameter Description
    Type You can select Directory or File Extension. Select a type based on your business requirements.
    • Directory: specifies resources that are in a specified directory.
    • File Extension: specifies file types of resources.
    Object
    • If you select Directory, take note of the following rules:
      • You can enter only one directory. You can use a forward slash (/) to specify all directories.
      • You can enter a full path. It must start with a forward slash (/), for example, /directory/aaa.
    • If you select File Extension, take note of the following rules:
      • You can enter one or more file extensions and separate them with commas (,), for example, jpg,txt. File extensions are case-sensitive.
      • You cannot use an asterisk (*) to specify all file types.
    Expire In Specify the TTL value of the cached resources. You can set the TTL value to at most three years. Take note of the following rules:
    • Specify a TTL value of one month or longer for static files that are infrequently updated, such as images and application packages.
    • Specify a TTL value based on your business requirements for static files that are frequently updated, such as JavaScript and CSS files.
    • Specify a TTL value of 0 seconds to disable caching for dynamic files, such as PHP, JSP, and ASP files.
    Weight Specify the weight of the cache policy. The weight indicates the priority of the cache policy. Valid values are 1 to 99. A greater value indicates a higher priority.
    Note
    • We recommend that you set a unique weight for each cache policy. If two cache policies are of different types but have the same weight, the cache policy that is based on file extensions has a higher priority than the other. If two cache policies are of the same type and have the same weight, the cache policies are applied in a random order.
    • If you have created multiple cache policies for a cached resource, only the first matched rule is applied.
  7. Click OK.

    After you create a cache policy, you can Modify or Delete it on the Cache Expiration tab.

Examples

In the following examples, three cache policies are created for the accelerated domain name example.aliyun.com. Policy 1 has the highest priority. If Policy 1 is applied, other policies are skipped.
  • Policy 1: Type is set to File Extension, Object is set to jpg,png, Expire In is set to 1 Month, and Weight is set to 90.
  • Policy 2: Type is set to Directory, Object is set to /www/dir/aaa, Expire In is set to 1 Hour, and Weight is set to 70.
  • Policy 3:Type is set to Directory, Object is set to a full path /www/dir/aaa/example.php, Expire In is set to 0 Seconds, and Weight is set to 80.

Related API operations

BatchSetCdnDomainConfig