CDN Cache Invalidation in AEMaaCS
AEMaaCS comes with Fastly as default CDN. There are two ways of invalidating CDN cache in AEMaaCS environments. 1. Through custom CDN cache invalidation workflow. 2. Through CDN set up.
- Custom CDN cache invalidation
I came accross a scenario where in we needed to do the CDN cache invalidation using custom code. However, this is not recommended way of invalidating cache but is doable using Fastly APIs here — https://www.fastly.com/documentation/reference/api/purging/
A simple servlet with appropriate path (again not recommended as servlets in AEM should be registered using resource type not path). And providing code like below. Purge key has to be obtained by making a request to the support.
private void clearCDNCache(String path, PrintWriter writer, SlingHttpServletRequest request ) throws Exception{
String CDN_PUBLISH_HOST = request.getParameter("publishHost");
String PURGE_KEY = request.getParameter("purgeKey");
String METHOD_PURGE = "PURGE";
HttpHost host = new HttpHost(CDN_PUBLISH_HOST,443,"https");
HttpClient httpclient = HttpClientBuilder.create().build();
BasicHttpRequest purgeRequest = new BasicHttpRequest(METHOD_PURGE, path);
purgeRequest.addHeader("x-aem-purge-key", PURGE_KEY);
HttpResponse cdnResponse = httpclient.execute(host, purgeRequest);
writer.println("Success : " + cdnResponse.getStatusLine());
}
However, in above approach, the event has to be identified that will trigger the invalidation on CDN. It can either be manual trigger by calling the servlet or a workflow or an event handler (Again AEMaaCS uses Sling content distributions so this will need to be evaluated for appropriate events).
2. Second way is to configure Fastly to use push invalidation. Push invalidation is useful when you want to invalidate the cache when content is published by the author. This is done using few steps. a. Configure fastly by providing production host, production service id, fastly auth token b. get access token from fastly to perform the content invalidation. c. Configure this header in contents — X-Push-Invalidation: enabled.
However, it is more suitable to use TTLs for dispatcher and CDN to invalidate the cache in AEMaaCS environments.
References :