Enforce HLS cache for live streaming
Give the segments and the playlist of an HLS stream their own cache settings and rules, from Azion Console or the Azion API.
An HLS stream is delivered as two kinds of file, and they do not want the same cache. You give each one a cache setting of its own and apply it with a Rules Engine rule that matches the file extension, from Azion Console or the Azion API v4.
A segment file is written once and never changes, so it takes the longer cache TTL. The playlist is rewritten as the stream advances, so it takes a TTL of a few seconds. The playlist TTL this guide uses is below 60 seconds, which requires Application Accelerator on the application.
Prerequisites
- An application that delivers the playlist and the segments of your stream. To create one, refer to Applications quickstart.
- A domain on the workload that serves the application. For more information, refer to Workloads.
- Application Accelerator turned on for the application. For the steps, refer to Configure cache policies for an application, whose first task turns the module on.
- Access to Azion Console, for the Console procedures. Refer to Access Azion Console.
- A personal token, for the API procedures.
Video files stored in Object Storage can serve as the origin of the stream: upload the video files, then point the encoder that produces the HLS output at the bucket.
Create the cache setting for the segments
The segments keep a cache TTL of 60 seconds and a browser TTL of 0 seconds. To create the setting:
Access Azion Console > Applications, then select the application that delivers the stream.
In Name, enter hls-segments.
Under Browser Cache, select Override cache settings and set the TTL to 0.
Under Cache, keep Override cache behavior selected and in Max Age enter 60.
The toggle is in the same section. It adds a second cache layer between Azion’s cache and your origin.
The setting appears in the Cache Settings list.
Create the cache setting for the playlist
The playlist keeps a cache TTL of 5 seconds, so a client reading it receives the segments the encoder has already written. To create the setting:
In Name, enter hls-playlist.
Under Browser Cache, select Override cache settings and set the TTL to 0.
Under Cache, keep Override cache behavior selected and in Max Age enter 5.
Both settings now appear in the list, and neither applies to a request yet.
Apply the settings with rules
Each setting needs a rule that matches its file extension. To create the rule for the segments:
Enter cache-hls-segments.
Under Criteria, select ${uri}, the matches operator, and enter .*.ts as the argument.
Under Behaviors, select Set Cache Policy, then select hls-segments.
Repeat the procedure for the playlist: name the rule cache-hls-playlist, enter .*.m3u8 as the criteria argument, and select hls-playlist as the cache policy.
Both rules now appear in the list, and each applies its setting to the files its pattern matches.
Configure through the API
The same two settings and two rules are created with four requests. To create the cache settings:
The API answers with HTTP 201 and the new setting under data:
Send the same request with "name": "hls-playlist" and "max_age": 5 under modules.cache. The response carries its own id.
To apply each setting with a rule:
Replace [CACHE SETTING ID] with the id of the hls-segments setting:
The API answers with HTTP 202 and the rule with state set to pending:
Send the same request with "name": "cache-hls-playlist", the argument .*.m3u8, and the id of the hls-playlist setting.
The stream is now cached by kind of file: each segment for 60 seconds, the playlist for 5 seconds.