What does a TTL of 0 means in CloudFront and an intro to CloudFront

A technical article but with simple words (I hope).

In AWS words:

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment.

The benefits of a CDN, which is to reduce the load on the origin (i.e. less servers to buy) and better serve readers (data is closer to them).

The idea is that there are files stored somewhere web accessible (i.e. the origin). CloudFront makes copies of those files and place them all over the world (tech: it caches the files) resulting in a copy being available close to the requester. On the web a fraction of a second is important, people leave website for being slow.

The main configuration aspect of CDNs is refresh; how often CloudFront should check if a new copy of the file is available? The interval of checks is guided by a parameter called TTL. TTL stands for Time To Live, and indicates the interval of validity of a file; while valid CloudFront will serve its copy (from the cache), when invalid CloudFront will ask the origin for an updated version of the file.

Now in the past (long time ago) CloudFront minimum TTL was 30 minutes, meaning that only files being consistent for more than 30 minutes could be cached. Images, that tend not to change frequently were perfectly suited, but dynamic pages (i.e. where the content changes everytime, like a news page) could not benefit from CloudFront. This has changed.

So here enters TTL = 0. A TTL of 0 seems stupid, CloudFront will ask everytime the origin for a copy of the file, reducing to null the benefit of a CDN. But this would be true if webservers were dump, but they are not, they are one of the oldest type of server and infinite improvement have been made. When asked correctly, like “hey server, I’ve a version of this file that is 2 seconds old, is there a new one?” (i.e. a GET request with an If-Modified-Since header) a webserver can answer with Yes here is the new file or No you have the latest version (i.e. the webserver return a http code 304 Not Modified). Exchanging short strings like this is makes a TTL of 0 being a really good idea when you have content that can change at any time, but might not.

The best example of such behaviour is when I press refresh in 5s intervals to see if an email has arrive.

Inspiration comes from: https://stackoverflow.com/questions/10621099/what-is-a-ttl-0-in-CloudFront-useful-for

comments powered by Disqus