Time-to-live (TTL) refers to the amount of time that a resource is cached on Alibaba Cloud CDN edge nodes. When the TTL of a cached resource ends, the resource on the edge nodes expires. Requests that attempt to access expired resources are redirected to the origin server. The retrieved resources are returned to the clients and cached on the edge nodes. You can add a cache rule for static resources based on file directories or file extensions.
Considerations
- When you update a file on the origin server, we recommend that you add version numbers
to the name of the file. This way, you can differentiate between file versions.
To differentiate file versions after updates, we recommend that you add version numbers to file names. This way, each file version has a unique name. For example, you can name a file img-v1.0.jpg before it is updated and img-v2.1.jpg after it is updated.
-
The resource TTL affects the back-to-origin routing frequency. Set a proper TTL based on your business requirements.
A short TTL may cause frequent back-to-origin routing and increase loads on the origin server. A long TTL may cause resources on the edge nodes to be outdated.
- Cached resources that are infrequently requested may be removed before they expire.
- If an edge node retrieves a static file from an origin server, the node processes the file based on the priorities of the cache rules. For more information, see Priorities of Alibaba Cloud CDN cache rules.
Procedure
Priorities of Alibaba Cloud CDN cache rules

- If the response carries the
pragma:no-cache
,ache-control:no-cache
,cache-control:no-store
, orcache-control:max-age=0
directive, the static file is not cached. - Alibaba Cloud CDN follows the TTL for cached resources, or the TTL for HTTP status
codes set in the Alibaba Cloud CDN console.
NoteIf a request matches multiple cache rules, only one rule is applied in the following order of priority: weight > rule creation time.
- If you create multiple cache rules, we recommend that you set a unique weight for each cache rule to define their priorities. A higher weight indicates a higher priority.
- Cache rules that have the same weight are prioritized in order of creation time, regardless of the rule type. The rule with the earliest creation time applies.
- Alibaba Cloud CDN follows other cache rules set on the origin server. Headers in responses
from the origin server are in the following descending order of priority: Cache-Control
> Expires > Last-Modified > ETag.
- The response carries the
Cache-Control
header and the directive ismax-age
, which is set to a value greater than 0, for example,cache-control:max-age=3600
. - The response carries the
Expires
header, for example, expires:Tue, 25 Nov 2031 17:25:43 GMT. - If the response header carries the
ETag
orLast-Modified
header, the TTL is calculated based on the following rules:- If the response carries the
Last-Modified
header, TTL = (Current time - Last-Modified) × 0.1. If the result is between 10 seconds to 3,600 seconds, the result applies. If the result is less than 10 seconds, the TTL is 10 seconds. If the result is greater than 3,600 seconds, the TTL is 3,600 seconds. - If the response carries only the
ETag
header, the TTL is 10 seconds.
- If the response carries the
- The response carries the
- If the response does not carry the
ETag
,Last-Modified
,Cache-Control
, orExpires
header, the static file is not cached on the node.
HTTP caching mechanisms
- Cache TTL
When a client requests resources from a server, the client and server define the TTL of the returned resources that are cached on the edge nodes. The resources expire when the TTL ends.
HTTP provides the following types of headers that can be used to define the cache TTL.
Header Protocol version Description Example Type Pragma HTTP/1.0 The Pragma header specifies whether a resource is cached. If Pragma is set to no-cache, the resource is not cached. Pragma is compatible with servers that use only HTTP/1.0.
Pragma:no-cache Request and response Expires HTTP/1.0 The Expires header specifies a date and time. Cached resource expire at the specified time. If Expires is set to an invalid value, such as 0, the resource has already expired.
Expires: Wed, 21 Oct 2022 07:28:00 GMT Response Cache-Control HTTP/1.1 The Cache-Control header can be set to different directives to control the caching behaviors. Most mainstream clients, such as browsers, use Cache-Control to control the caching behaviors. The following directives specify that files are not cached:- Cache-Control:no-cache
- Cache-Control:no-store
- Cache-Control:max-age=0
The following directive specifies that files are cached for 1 hour: Cache-Control:max-age=3600.
Request and response - Resource tags
The first time a client requests a resource from a server, the server adds a tag to the response headers. The next time the client requests the resource from the server, the tag is used to identify the requested resource. The header of the subsequent requests carries this tag. If the server checks the tag and confirms that the requested resource is not updated, the HTTP 304 status code is returned to the clients. The clients retrieve the resource from the local cache. If the server detects that the tag is different from that of the resource on the server, the server informs the clients that the resource is updated or has expired. In this case, the clients must retrieve the latest version of the resource from the server.
HTTP provides the following types of header that can be used to control cache versions.
Header Protocol version Description Example Type Last-Modified HTTP/1.0 Last-Modified indicates the last time when a resource is updated. Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT Response ETag HTTP/1.1 The ETag header is the unique identifier of each version of a resource. ETag indicates whether a resource has been updated. If the resource has been updated, the server does not need to return a complete response.
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4" Response - Content negotiation
Caching software uses keywords to index objects on disks. In HTTP/1.0, URLs are used as keywords. However, different resources may point to the same URL. To differentiate them, clients must provide more information, such as the Accept-Language and Accept-Charset headers. HTTP/1.1 introduced the Vary response header to implement content negotiation. The Vary header lists the request headers that must be included to implement content negotiation.
In content negotiation, Vary is used to differentiate variants so that the clients can retrieve the desired variants.
Header Protocol version Description Example Type Vary HTTP/1.1 Examples- A server uses
Vary: Accept-Encoding
to inform the recipient, such as a CDN edge node, that the requested resource has two variants. One of them is compressed and the other is not. When the client sends requests to Alibaba Cloud CDN for the same resource, the browser with an outdated version requires the resource to be uncompressed to prevent incompatibility. The browser with the latest version requires the resource to be compressed to reduce data transfer. - The server uses
Vary: User-Agent
to identify the browsers that initiate the requests and inform the recipient, such as a CDN edge node, of the browser types. The CDN edge node caches variants based on the browser types.
Vary: Accept-Encoding
Vary: Accept-Encoding,User-Agent
Response - A server uses
Configuration examples

demo.aliyun.com
. When the edge nodes retrieve the resource http://demo.aliyun.com/image/example.png
, each of the rules is a match. In addition, the rules have the same weight. In this
case, the rules are prioritized in order of creation time. The rule with the earliest
creation time has the highest priority. Therefore, the rule that is set for the /image
directory is applied because the rule is created the earliest. 