Configuring Yandex Cloud CDN when the origin is unavailable
To ensure your CDN resource continues delivering content when the primary origin is unavailable, enable caching, configure origin shielding, and connect a standby origin in advance. If your application supports older file versions, you can also allow serving stale content from the cache.
How Yandex Cloud CDN handles origin unavailability
CDN servers located at various points of presence deliver content directly to users. An outage at the origin data center does not automatically stop content delivery through the CDN.
If your CDN resource only has one origin that becomes unavailable, the outcome depends on the current state of the cache:
|
File state |
Expected behavior |
|
The file exists in the CDN server cache, and the cache TTL has not expired. |
Cloud CDN continues to deliver the file. |
|
The file is not in the cache. |
Cloud CDN must retrieve the file from the origin, or from the shielding server if origin shielding is enabled. |
|
The cache TTL has expired. |
Cloud CDN must refresh the file from the origin. |
|
The cache TTL has expired, and delivery of outdated content is enabled. |
If the |
|
File caching is disabled. |
To get the file, each user request requires an available origin. |
If there is a cache miss and the origin is unavailable, the user will get an error. In case of a timeout, this may be 504 Gateway Timeout. On other failures, the error code may vary.
Note
Each point of presence has its separate cache. A file cached on one CDN server may not exist at other points of presence. For details, see Content caching.
To improve resource fault tolerance:
- Enable caching on the resource.
- Enable origin shielding.
- Prepare a standby origin.
- Add the second origin to the origin group.
- Allow delivery of outdated content, if applicable.
- Complete the setup before an outage occurs.
- Test the standby origin.
Enable caching on the resource
- In the management console
, select the folder where your resource is located and navigate to Cloud CDN. - Click the resource name.
- Navigate to the Caching tab and click Edit.
- Under CDN, enable CDN caching.
- Select the setting type: Same as origin or Custom settings. Set the cache lifetime.
- Click Save.
For a step-by-step guide, see Configuring caching.
Tip
Make sure your files are cached correctly. Check the Cache-Status response header returned by Cloud CDN: the HIT value indicates the file was delivered from the cache.
Caching behavior depends on the resource settings and origin response headers. Browser caching is configured separately and does not replace CDN server caching.
If caching is disabled or a file is excluded from caching, Cloud CDN will access the origin each time the file is requested. Set your cache TTL based on how frequently your content updates.
Enable origin shielding
Origin shielding adds an intermediate caching layer between the CDN servers and origins. If a file is not in the CDN server's cache, the request goes to the shielding server first. If a valid cached copy exists there, no requests are sent to the origin.
The shielding server also consolidates duplicate requests for a single file. This reduces redundant content downloads from the origin and lowers its load.
Origin shielding can significantly improve caching efficiency, especially when users request the same files through different points of presence. The result depends on request patterns and cache settings. For details, see Origin shielding.
To enable shielding:
- Open the resource in Cloud CDN.
- Navigate to the Origin shielding tab.
- Click Activate, select an available location, and confirm.
- Make sure the status has changed to
Active.
It may take up to 15 minutes to apply the settings.
Warning
Origin shielding is billed separately. A full monthly fee is charged on the day the feature is enabled.
Learn more in this guide on enabling shielding.
Use origin shielding in addition to origin redundancy. If a file is missing from both the CDN server cache and the shielding server, the CDN must fetch it from an available origin.
Prepare a standby origin
To retrieve files when the primary origin is down, you need a secondary origin containing a copy of your content.
For VMs, you can use a different availability zone. To protect against a full cloud platform outage, host your standby origin in another cloud or independent infrastructure.
Note
A duplicate Object Storage bucket cannot serve as an independent backup for service-wide outages.
Before adding a standby origin, confirm the following:
- The files are accessible at the same paths as on the primary origin.
- All file copies are up-to-date.
- The origin is accessible to Cloud CDN, considering network restrictions and access permissions. Use an up-to-date list of Cloud CDN networks. If origin shielding is enabled, contact support
to get the list of the shielding servers’ networks. - Protocol configurations and the setting of HTTPS TLS certificates and
Hostheaders are compatible with both origins. - The standby origin can handle the load if the primary origin fails.
Tip
When using different buckets, make sure to check the Host header. A value supported by one of the buckets may be incompatible with the other. For details on how to implement this setup, see the Host header section.
Add the second origin to the origin group
In the management console
The following options are available:
|
Priorities |
Behavior |
|
Primary and primary |
Requests are distributed using the round-robin method. Both origins handle requests. |
|
Primary and standby |
Cloud CDN sends a request to the primary origin first. If a retryable error occurs, the request fails over to the standby origin. |
In round-robin mode, individual requests are balanced across the origins. A single file is never fetched from two origins at the same time. For more information, see Origins and origin groups.
Note
Failover takes place only during connection errors, timeouts, and responses with HTTP codes 500, 502, 503, and 504. Responses with 403 and 404 codes do not trigger a failover. For example, if a file is missing from the primary origin, Cloud CDN will not automatically look for it on the backup origin.
Once configured, add the group to the CDN resource.
It may take up to 15 minutes for settings to apply to the CDN servers. Wait for them to propagate, then test the standby origin. During failover, response times may increase while waiting for the primary origin to time out.
Allow delivery of outdated content, if applicable
For some types of content, delivering an outdated file version is often better than returning an error. In this scenario, you can allow Cloud CDN to deliver a cached copy after its TTL expires if the origin is unavailable.
When using the API, you can configure this behavior through the options.stale CDN resource parameter. Here is a configuration snippet example:
{
"options": {
"stale": {
"enabled": true,
"value": [
"error",
"timeout",
"http_500",
"http_502",
"http_503",
"http_504"
]
}
}
}
This example allows the CDN to deliver stale content during network errors, timeouts, and the specified HTTP status codes.
For a new resource, use the create REST API method for the Resource resource or the ResourceService/Create gRPC API call. For existing resources, use the update REST API method for the Resource resource or the ResourceService/Update gRPC API call.
Example of modifying a CDN resource using curl
export IAM_TOKEN=$(yc iam create-token)
curl \
--request PATCH \
--header "Authorization: Bearer $IAM_TOKEN" \
--header "Content-Type: application/json" \
--url 'https://cdn.api.cloud.yandex.net/cdn/v1/resources/<resource_ID>' \
--data '{
"options": {
"stale": {
"enabled": true,
"value": [
"error",
"timeout",
"http_500",
"http_502",
"http_503",
"http_504"
]
}
}
}'
For information on the IAM token and CDN resource ID, see the following pages:
Note
Delivery of outdated content is only possible if a cached copy exists. If the file was never cached or has been removed, an available origin is required.
The conditions for outdated content delivery and origin failover are configured independently. Adding an error code to stale does not automatically enable origin failover for that code.
Complete the setup before an outage occurs
During an outage, management operations for Cloud CDN may be unavailable along with your origins. This includes viewing resources, updating configurations, cache purging or pre-fetching.
Add your standby origin in advance and wait for the settings to apply. This enables Cloud CDN to automatically route traffic to the standby origin if the primary one goes down. You will not need to modify settings If the management console or API become unavailable.
Warning
Do not purge the cache when origins are unavailable unless absolutely necessary. Purging deletes all cached copies, and Cloud CDN may be unable to retrieve them again.
Test the standby origin
For testing, use a separate, dedicated test CDN resource. You can create it by copying the configuration of your production resource. Copying is restricted to a folder. Make sure this new test resource has its own domain name and a CNAME record.
- Place identical test files on both the primary and standby origins.
- In the test resource, set the production standby origin as its sole primary origin. Use a separate origin group to avoid modifying your production resource group.
- Wait for the settings to apply.
- Send the exact same request through the test CDN resource as you would through production: use the same file path, query parameters, HTTP method, and required headers. Change only the CDN resource domain name to the test domain. Before testing, make sure the response is not fetched from a pre-populated cache of the test resource or shielding server.
- Compare the HTTP status code, content, and key response headers with those of the production resource.
- Check the standby origin logs to confirm request delivery.
This test confirms the backup origin is accessible to Cloud CDN and delivers content correctly.
To further test automatic failover, create a separate group with two test origins of the primary and standby priorities. Connect this group to your test CDN resource and wait for the settings to apply. Simulate a failover error, e.g., a 503 response, on the primary test origin. Request a file that is yet uncached by both the CDN and shielding servers. Check your standby origin’s logs to confirm it received the request. Restore the primary origin after testing.