When you have frequent problems playing back RTMP streaming videos or audios via AWS CloudFront (stuttering or stalling), you can use a simple trick to boost the performance by adding a cache header to the HTTP headers of your media in the S3 bucket. It is possible to add or change the HTTP headers of any file located on your S3 buckets/distributions.
You can add the cache header quite easily. I’ll show you how to do this with CloudBerry S3 Explorer and the AWS console. But let me first give a bit more information about caching on AWS CloudFront:
What is the function of cache-control?
The cache-control directive is a little piece of header information you can add to tell the CloudFront service how long a file should stay there before it needs to be uploaded to the Edge location(s) again.
Videos and audios are cached according to the value of the cache-control max-age directive. I.e.: by using the max-age value, you can lengthen the cache expiring time of a video or audio on those Edge location(s).
Default, this is set to 24 hours for RTMP distributions, which means the cache expires each day.
Therefore, each time the cache expires and the first visitor of a specific region in the World clicks the play button, the Edge server (Edge location) closest to the visitor’s location needs to make a request to the original S3 bucket to upload a copy of the video/audio to that Edge server.
Luckily, this only needs to be done once per cache session, but if your cache session is set to a month, it increases the likelihood that your media only have to be uploaded once in a month.
I said likelihood because the CloudFront service will evict objects that are not frequently used regardless of the max-age or expiry on the object to free up space for objects that are more popular.
However, this solution gives a good result in general, reducing the risk of stuttering or stalling for HD videos and lengthy audios.
CloudBerry Explorer has the advantage that you can set the HTTP header for a big amount of files in one go, whereas in the AWS console, you have to do this for each file separately. Here is a video tutorial (1:11) which shows you how to do it with CloudBerry S3 Explorer (further down a tutorial using the aws console):
How to add the cache-control max-age directive using CloudBerry Explorer
How to add the cache-control max-age directive using the AWS console
Is cache-control exclusively for RTMP distributions?
No, you can set the cache-control for any object. If you have files that do not change frequently, it helps to cache them too. Especially, if you have images in download distributions you can profit from this method in the same way as streaming videos and audios. On regular S3 bucket objects, you have to use the expire directive to force browsers to cache them. There is no advantage in using the max-age directive in that case because objects remain in the S3 buckets until you remove them manually, whereas objects in CloudFront distributions are removed frequently.
For more info, see: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html
With a big thank you to Mihai for the tip and Brian@aws.com of the AWS forum, who responded quickly to my enquiries.