Using Cache Purge to Keep Your Website Content Fresh and Responsive

Content Delivery Networks (CDNs) accelerate web traffic across the internet through servers residing in strategic locations (known as points of presence or PoPs) across the globe. Each PoP has a number of caching servers, each of which contains a cached version of your website or application. By bringing content closer to each visitor, CDNs improve performance and reduce load on the origin server.

Caching is the raison d’etre for CDNs.

The reason for this is a CDN’s effectiveness can be measured by the cache hit ratio, which is the percentage of requests the CDN was able to serve out of its own cache. The higher the caching ratio, the better your website or application performance.

CDNs typically cache static files, such as images, JavaScript files and CSS. This type of content rarely changes and can be heavily cached.

Like what you’re reading? Subscribe to our blog for the latest in networking and security.

Keeping your content fresh

While response and fast load times are crucial for users, the freshness (or accuracy) of your content is no less important for your business reputation.

Caches are refreshed based on caching policies each website defines which determine the amount of time a given resource remains in the cache. When that duration expires, the content must be retrieved from the origin server. If the content in the origin server has changed, the new version will be uploaded to the cache.

In certain situations, however, you may not want to wait for the caching period to expire. An example of this would be an update on your website’s pricing page or a bug on an online game. That’s where cache purging comes in. Purging refers to the active removal of a resource from the cache without waiting for the predetermined cache expiry time. As soon as a user requests the purged resource, the CDN will cache a copy of the updated content from the origin server.

When to use rapid cache purge

If you’re the owner of a blog or an online academic journal whose content doesn’t change once it’s uploaded to the site, you most likely can do without purging since your content is not time-sensitive.

Other types of websites, such as news and sports sites with live updates, or e-commerce sites with frequent pricing changes, would benefit from caching policies that reflect the almost continuous information updates. This ensures that content is always fresh without requiring frequent cache purges.

However, there are situations in which rapid purging can be critical for your business and your users. Consider, for example, the following “crisis” scenarios:

  • You run an e-commerce site specializing in jewelry and you’ve run out of inventory for a popular ring. You need to temporarily remove the item from your online catalog until your supplier delivers new stock. Waiting for the cache to refresh is going to result in disgruntled customers and an increase in customer service calls.
  • You’ve uploaded new content to your website around an important product launch and discovered a bug in one of the pages. A major PR campaign is underway driving thousands of daily visitors to your site and you can’t afford a mishap. In such a case, the content must be corrected and replaced immediately with the updated version. Failure to do so could result in substantial reputation loss.
  • You’ve just updated or patched your online game and want to be sure that the latest version is available to your users. If the old version contained a critical error, your users are likely to go elsewhere for their gaming entertainment.

In addition to the above, there are applications and websites with cacheable content where certain types of business activities will trigger the need for an immediate purge. For example:

  • The removal of an account from a photo sharing application should result in the deletion of all photos related to that account. As soon the photos are deleted from the application database, you need to be sure that there are no traces of those photos on caching servers located around the world.
  • You own a blog and decide to delete an outdated blog post from your online platform. Similar to the example above, you don’t want copies of the deleted post to be accessed from your CDN’s caching servers.

If you need to respond quickly to business triggers or handle an emergency fix, and your changes need to be propagated across the CDN immediately, rapid purging is the answer.

How fast is a cache purge?

Once you’ve decided to purge the cache, the time it takes to execute depends on your CDN. The effectiveness of a purge request is measured in the time it takes for it to propagate worldwide through the entire network. The speed of the purge depends on several factors including hardware type, network size and the number of CDN customers requesting cache purges at the same time.

While some CDNs, like Incapsula, can achieve purge times of less than 500 milliseconds, CDNs with larger networks and outdated hardware may take longer to propagate the request and delete the relevant content from all the caching servers. Most CDNs let you purge a specific resource on demand, but some only give you the option of purging the entire site cache.

The goal is real-time syncing

Most web developers and website owners want the ability to control their cached content in a manner similar to how they manage their database. This means that the CDN caching mechanism should be easy to use and allows instant propagation of changes or fast purging.

Fast purging means near real-time syncing between the origin server (the application database) and the CDN. It’s designed to minimize the delay between updates to the application resources, such as database and files, and updates of the cached resource, allowing website owners to be in full control of what their visitors see. The first graphic below shows data on all PoPs  are instantly synced . The second graphic shows the PoP serving traffic to the user is not synced in time resulting in a data integrity issue where the user can only view old cached content.

purging-diagram 1 purging-diagram 2

How to implement fast cache purge on Incapsula

In our experience we’ve seen that purging is a last resort that should be reserved for exceptional situations. The best way to avoid cache purging is to implement efficient caching policies that reflect the behavior of your website or application. Incapsula provides intelligent caching functionality that identifies resources that change more frequently and automatically sets the caching rules accordingly. This gives customers an optimal default refresh policy, so content always stays fresh. These settings can be manually adjusted as needed.

That said, the Incapsula CDN is built for rapid purging, comprising a relatively small number of high-capacity PoPs that allow for instant propagation. Incapsula users can clear the cache as needed using the following methods:

  1. Manually via the GUI

The Incapsula GUI lets you purge the entire cache or a specific resource.

Caching modeAfter a major change to your website, such as a version update, you may want to clear all resources in the cache immediately, without waiting for the caching period to expire. Just click “Purge Cache” and “Continue” and you’re done.

Purge cache

Clicking “Purge specific resource” will display the following screen:

Purge specific resource

Select one of the URL rule options to specify a particular URL (URL is) or a string to be matched (URL is, URL contains, URL starts with or URL ends with). Then click the Purge button.

  1. Automated cache purge option using the Incapsula API

Using the GUI is simple but it can take a couple of minutes to login and execute the purge. It also needs to be repeated each time you update a specific piece of content.

The Incapsula API automates the purge cache action. Here the customer must define in advance the types of updates that will trigger an automatic purge like an update to an HTML page. A piece of code is added to the application that calls the API in the pre-defined conditions. The purge is propagated in less than half a second. The API call can be used to purge the entire cache for a site or a specific resource using the resource name as a parameter.

Purging the Cache

This rule purges all cached content on the Incapsula proxy servers for a specific site. For example, you may want to purge the cache after making adjustments in your site. To purge the entire cached content for the site, just use the API call with no parameters. If you want to purge a specific resource, add the resource name in the purge_pattern parameter.

This script adds this rule with these other parameters of the rule, as defined in the Site Management API.

  • site_id
  • purge_pattern — The pattern of the resource to be purged from the cache
#!/bin/sh

curl -X POST "https://my.incapsula.com/api/prov/v1/sites/cache/purge?api_id=12593&api_key=082ced47-cbf4-4488-a3d3-b852ab899437&site_id=78925715"

Sample response to the command, when successful, is

{"res":0,"res_message":"OK","debug_info":{"id-info":"9123"}}

The API offers a more advanced option because it requires some software development on the application side. The decision to use a manual approach or API will depend on your application’s sensitivity to data integrity, as well as the volume and frequency of changes. If it’s low, use the GUI; if it’s high, use the API.

Conclusion

CDN caching gives websites and web applications an effective way to reduce bandwidth costs, improve user experience and ensure reliable content delivery. At the same time, your visitors also expect continuous access to fresh content.

Fast purging allows real-time syncing between the state of the application and the content being served to users from the CDN. It’s particularly useful for reacting quickly to business triggers and crisis situations where you can’t afford to wait for the cache to refresh automatically. For this reason, whether you’re using a manual or automated purge mechanism, speed is of the essence and propagation times across your global CDN are critical for staying on the same page as your users.

Look for a future post on the underlying technology challenges of cache purge and how to handle it.

Have questions for David about CDN or caching? Please leave us a comment.

Keep your finger on the pulse

Sign up for updates from Imperva, our affiliated entities and industry news.