How to enforce HLS cache for live streaming delivery
Azion Edge Platform allows you to deliver live streaming content in HLS format, being able to easily self-provision and configure your cache policies. This guide covers the step-by-step to enforce HLS cache, manage chunks and playlists caching, set up rules engines rules, and utilize Azion Edge Storage for optimal video delivery.
Requirements
- An existing Azion edge application, or create a new one.
- A domain associated with the edge application.
- Tiered Cache enabled in your account.
- Access to Azion Edge Storage for storing video content.
Enforcing cache policies for HLS
To enforce cache policies for HLS in the edge application, follow the steps as explained next.
In this example, an edge application and a domain linked to it has been previously created.
Creating a cache policy for the chunks
First, you must create a cache policy for the chunks:
- Open Azion CLI in your terminal.
- Get the details of an existing edge application using the list command:
azion list edge-application --details
- Enable Tiered Cache for your application:
$ azion update edge-application --application-id 1234 --l2-caching true
- Create an origin:
$ azion create origin --application-id 1234 --name "origin-edge" --origin-type single_origin --addresses "example.com" --host-header "example.com"
- Configure the cache policy for the chunks:
$ azion create cache-setting --application-id 1234 --name "chunks-policy" --browser-cache-settings "override" --browser-cache-settings-maximum-ttl 0 --cdn-cache-settings "override" --cnd-cache-settings-maximum-ttl 60
- Define Advanced Cache Key behaviors according to your needs. Check the optional flags for the create command.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
- Access Azion Console.
- Go to Products menu > Edge Application.
- Select your application.
- Open the Cache Settings tab and click the + Cache Setting button to create the new cache policy:
- Give it a unique name.
- In Cache Expiration Policies:
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
0
. - For Edge Cache Settings, select Override Cache Setting and define a Maximum TTL of
60
.
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
- Enable the Tiered Cache switch.
- In the Advanced Cache Key section, define the behavior of your application toward cache segmentation of objects.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
- Click the Save button.
- Access Real-Time Manager (RTM).
- Go to Products menu > Edge Application.
- Select your application.
- Open the Cache Settings tab and click the Add Cache Settings button to create the new cache policy:
- Give it a unique name.
- In Expiration Settings:
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
0
. - For CDN Cache Settings, select Override Cache Setting and define a Maximum TTL of
60
.
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
- Enable the Tiered Cache switch.
- In the Advanced Cache Key section, define the behavior of your application toward cache segmentation of objects.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
- Click the Save button.
Creating cache policies for the playlist
Now configure the cache policy for the playlist: $ azion create cache-setting --application-id 1234 --name "playlist-policy" --browser-cache-settings "override" --browser-cache-settings-maximum-ttl 0 --cdn-cache-settings "override" --cnd-cache-settings-maximum-ttl 5
- Define Advanced Cache Key behaviors according to your needs. Check the optional flags for the create command.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
Still on the Cache Settings tab:
- Click the + Cache Setting button again to create a new cache policy for the playlist:
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
0
. - For Edge Cache Settings, select Override Cache Setting and define a Maximum TTL of
5
. - Enable the Tiered Cache switch.
- In the Advanced Cache Key section, define the behavior of your application toward cache segmentation of objects.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
- Click the Save button.
Still on the Cache Settings tab:
- Click the Add Cache Settings button again to create a new cache policy for the playlist:
- For Browser Cache Settings, select Override Cache Setting and define a Maximum TTL of
0
. - For CDN Cache Settings, select Override Cache Setting and define a Maximum TTL of
5
. - Enable the Tiered Cache switch.
- In the Advanced Cache Key section, define the behavior of your application toward cache segmentation of objects.
- The recommendation is selecting Content does not vary, for Query String and/or Cookies, according to your needs.
- Click the Save button.
Utilizing Azion Edge Storage for HLS video delivery
To deliver MP4 videos using HLS and Azion Edge Storage:
- Upload your MP4 video files to Azion Edge Storage.
- Configure your encoder to convert MP4 files to HLS format and point to Azion Edge Storage as the origin.
- Follow the cache policy setup steps outlined above to ensure efficient video delivery.
Creating Rules Engine rules
- First, create a rule for the chunks:
$ azion create rules-engine --application-id 1234 --phase "request" --file ./chunks-rule.json
In the chunks-rule.json
file, include:
{ "name": "chunks-rule", "description": "This is a description for your chunks rule", "criteria": [ [ { "conditional": "if", "variable": "${uri}", "operator": "matches", "input_value": ".\*.ts" } ] ], "behaviors": [ { "name": "set_cache_policy", "target": "chunks-policy" } ]
}
- Now, create a rule for the playlist:
$ azion create rules-engine --application-id 1234 --phase "request" --file ./playlist-rule.json
In the playlist-rule.json
file, include:
{ "name": "playlist-cache-rule", "description": "This is a description for your playlist rule", "criteria": [ [ { "conditional": "if", "variable": "${uri}", "operator": "matches", "input_value": "\*.m3u8" } ] ], "behaviors": [ { "name": "set_cache_policy", "target": "playlist-policy" } ]
}
Still on the application page, open the Rules Engine tab:
First, create a rule for the chunks:
- Click the + Rule button.
- Give a name and a description (optional) to your rule.
- Select the Request Phase option.
- In Criteria, define
if ${uri}
matches.\*.ts
. - In Behaviors, select Set Cache Policy and add the policy for the chunks you created in the previous step.
- Click the Save button.
Now, create a rule for the playlist:
- Click the + Rule button.
- Give a name and a description (optional) to your rule.
- Select the Request Phase option.
- In Criteria, define
if ${uri}
matches\*.m3u8
. - In Behaviors, select Set Cache Policy and add the policy for the playlist you created in the previous step.
- Click the Save button.
Still on the application page, open the Rules Engine tab:
First, create a rule for the chunks:
- Click the New Rule button and select the Request Phase option.
- Give a name and a description (optional) to your rule.
- In Criteria, define
if ${uri}
matches.\*.ts
. - In Behaviors, select Set Cache Policy and add the policy for the chunks you created in the previous step.
- Click the Save button.
Now, create a rule for the playlist:
- Click the New Rule button and select the Request Phase option.
- Give a name and a description (optional) to your rule.
- In Criteria, define
if ${uri}
matches\*.m3u8
. - In Behaviors, select Set Cache Policy and add the policy for the playlist you created in the previous step.
- Click the Save button.
Done. Now you can configure your source and encoder pointing to Azion and stream your content, enforcing HLS cache.
Go to Live Streaming Delivery architecture