Cache prefreshing is the practice of refreshing your cached API content before its predefined time to live (TTL) expires. When you enable this feature, edge servers asynchronously refresh cached API content after a specified percentage of the content’s TTL elapses. This way API consumers do not have to wait for a response from the origin and get their requested content faster.
You may find this feature useful if your API content changes frequently and you rely on manual cache purges to update it. Cache prefreshing expires your outdated content automatically and saves you time and effort in periods of high demand.
If a client sends a request to your API after the asynchronous refresh begins and before the TTL expires, edge servers continue to serve the older content to the client until its TTL expires, or until edge servers receive the refreshed content from the origin.
In API Definitions, on the Caching settings or GraphQL caching settings page, you may enter the percentage of content’s TTL after which edge servers will asynchronously refresh the content in cache. You can define this percentage both at the API configuration and API resource levels.
If, for example, content has 10 minutes to live, and a response from the origin takes 30 seconds, the optimal setting would be 95% or lower of the content’s TTL. This way, edge servers refresh the content in enough time to let API consumers receive that content without having to wait for the response from the origin, but not so early that it increases load on the origin.
Consider the following when setting the cache prefresh percentage:
- The frequency of API requests
- Setting too high a percentage and having too few requests could mean the content expires and an API consumer must wait for the response from the origin. Setting it too low may result in unnecessarily high load on origin.
- The length of time for the origin to send the whole response
- If the origin has not finished sending the new content by the time the original content expires, API consumers will have to wait for the origin to respond.