Caching is the process of storing data locally for future use. For example, web browsers, like Chrome and Firefox, employ this for making websites load faster the next time you visit them. This is done by storing cacheable content on your disk when you visit a website. The next time you visit the same webpage, the browser will pull the content from your disk. This improves performance and reduces bandwidth because the browser does not have to download all static content again.
At the heart of the content delivery network (CDN) is caching; static content like images, videos, and other web assets will be stored close to your users on so-called 'edge nodes' that are geographically distributed.
This has the following advantages:
- Network latency is reduced because the content is moved close to the user and reduces the distance the data has to travel.
- Offload; your own server(s) do not have to serve this data. This will reduce your application costs.
How long is my content stored?
Files (objects) are cached on one or more Point of Presence (PoP) which is a data center with multiple cache servers. When content gets ingested a time to live (TTL) number will be attached that specifies how long the content will live in the CDN cache. These caches typically implement a Least Recently Used (LRU) cache and it will remove the content once it expires or, in the case of a very busy PoP, gets evicted due to low popularity. Tinify CDN has a minimum cache time of 1 month but the LRU cannot guarantee that the content will stay 'hot' due to the reasons above.
Managing cache times
Tinify CDN is optimized for caching content that does not change. Web developers can use the HTTP cache headers to set cache durations. The object TTL that is in the header 'Cache-Control max-age directive' will be respected by the CDN if the max-age is above 30 days or below 1 year. Tinify CDN defaults to a max-age of 1 month and at this time, does not allow shorter cache times.
Example Cache-Control header which caches an object for 5259600 seconds (2 months)
Cache-Control: public, max-age=5259600
Please note that most web servers do set the appropriate Cache-Control headers for you.
Query String Caching
Query strings are ignored unless they are used for Tinify CDN features, like resizing.
Efficient caching strategies
CDN deliverable objects should be treated as immutable / forever cacheable. This can of course be a problem when you need to make changes to certain assets. Therefore it is recommended to create unique file names for new deployments, for example by including a hash in the filename. Many front-end tools already offer features that can do this for you.
It is good practice and a much faster approach to rely on a unique naming scheme than on short cache times and CDN purges.