Blog

Purge Content with the Akamai Docker Image

June 10, 2020 · by Javier Garza ·
Categories:

If you manage a site using a content management system (CMS), you probably have a workflow that involves several teams. There's a team to publish content, a team to purge cached content from your content delivery network (CDN), along with many other teams. In this blog post, I'll go over how you can optimize operations by automating purging via the Fast Purge API. You can watch the full demo at the end of this post. 

There are several ways to use the Fast Purge API. In this example, I'll show you how to use the purge CLI package, included with the Akamai Docker image. The package allows you to make changes without writing any code.

To begin, make sure that you have your API credentials and the proper permissions to use the Fast Purge API. If you check the help screen of the Purge CLI, you will see that, by default, the CLI is going to look for a local file called .edgerc located in the home directory.

In the code below you can see that the CLI is going to look for a local file called .edgerc by default. The file is located in the home directory and a section called ccu (you can of course use flags to use something else, but for simplicity I am going to use the defaults). 

Akamai DevOps [~] >> akamai purge help

Usage:

  akamai purge [global flags] command [command flags] [arguments...]



 

Description:

   Purge Content from the Edge. URLs/CPCodes/Tags may be specified as a list of arguments, or piped in via STDIN

 

Built-In Commands:

  invalidate

  delete

  list

  help

 

Global Flags:

   --edgerc value  Location of the credentials file (default: "/root/.edgerc") [$AKAMAI_EDGERC]

   --section value     Section of the credentials file (default: "ccu") [$AKAMAI_EDGERC_SECTION]

Below is the setup I’ll be using.

I’ll use an origin server called jgarza.origin.sandbox.akamaideveloper.com that I set up in Heroku that returns the current time of the web server. The name resolution below shows how the origin hostname resolves to a set of 8 Heroku IP addresses on the Akamai staging network.

Akamai DevOps [~] >> dig jgarza.origin.sandbox.akamaideveloper.com

; <<>> DiG 9.14.8 <<>> jgarza.origin.sandbox.akamaideveloper.com

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50203

;; flags: qr rd ra; QUERY: 1, ANSWER: 9, AUTHORITY: 0, ADDITIONAL: 0

 

;; QUESTION SECTION:

;jgarza.origin.sandbox.akamaideveloper.com. IN A

 

;; ANSWER SECTION:

jgarza.origin.sandbox.akamaideveloper.com. 377 IN CNAME wildcard.origin.sandbox.akamaideveloper.com.herokudns.com.

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 52.71.209.190

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 52.207.47.153

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 18.213.76.145

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 3.224.101.150

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 54.209.102.93

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 18.214.66.67

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 54.84.169.173

wildcard.origin.sandbox.akamaideveloper.com.herokudns.com. 77 IN A 52.86.203.217

 

;; Query time: 736 msec

;; SERVER: 192.168.65.1#53(192.168.65.1)

The hostname delivered by Akamai is jgarza.sandbox.akamaideveloper.com and, as you can see, it resolves to the Akamai staging network (note the -staging in the CNAME resolution). 

Akamai DevOps [~] >> dig jgarza.sandbox.akamaideveloper.com

; <<>> DiG 9.14.8 <<>> jgarza.sandbox.akamaideveloper.com

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24062

;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

 

;; QUESTION SECTION:

;jgarza.sandbox.akamaideveloper.com. IN A

 

;; ANSWER SECTION:
jgarza.sandbox.akamaideveloper.com. 377 IN CNAME sandbox.akamaideveloper.com.edgesuite-staging.net.
sandbox.akamaideveloper.com.edgesuite-staging.net. 4502 IN CNAME a1219.dscq.akamai-staging.net.
a1219.dscq.akamai-staging.net. 27 IN A 23.50.49.11
a1219.dscq.akamai-staging.net. 27 IN A 23.50.49.10

 

;; Query time: 1186 msec

;; SERVER: 192.168.65.1#53(192.168.65.1)

;; WHEN: Tue May 19 08:40:02 UTC 2020

;; MSG SIZE  rcvd: 187

This is unusual as you don’t want to point a production server to the Akamai staging network, which is a small set of Akamai Edge servers meant for Akamai customers to validate configuration changes before activating them on their production network. 

The main reason to point the sample website to the Akamai staging network is because activations in staging are much faster than in production. Since our test website doesn’t have any real traffic, it doesn’t matter if you use a much smaller subset of Akamai Edge servers.

If you open the origin URL http://jgarza.origin.sandbox.akamaideveloper.com/ and reload the page a couple of times, you will see the content of the page updates as it shows the current timestamp of the server. However if you open the “Akamaized” URL http://jgarza.sandbox.akamaideveloper.com/, you we will see that the timestamp doesn’t change because the Akamai server is instructed to cache to content for 1 day.

Let’s see how easy it is to invalidate the Akamai cache using the purge CLI: 

Akamai DevOps [~] >> akamai purge invalidate http://jgarza.sandbox.akamaideveloper.com/ --staging

Purging...... [OK]

Purged 1 objects (ETA: 5 seconds)

Purge ID: c5cf5fca-94cd-11ea-b7d5-5118a434dc95

In the example above, I use the akamai CLI which has the name akamai with the CLI Purge package. Next, I indicated the parameter with “invalidate,” which marks the object as expired on the cache but does not delete it. For the URL of the object we want to invalidate, I used --staging to indicate the staging network as the default is production.

Now, when I reload the http://jgarza.sandbox.akamaideveloper.com/ in my browser window, the timestamp should update.

Akamai also offers two additional ways to purge content:

  1. By Content Provider code (or CP code) which allows you to purge the whole site at once by using the parameter --cpcode and should be used with caution.

  2. By cache-tag which allows you to purge a group of objects that are tagged by your origin with a single value

Let’s see how we would purge by cache-tags indicating a tag of fiction-books:

Akamai DevOps [~] >> akamai purge invalidate --tag fiction-books --staging

Purging...... [OK]

Purged 2 objects (ETA: 5 seconds)

Purge ID: b60b3da4-94ce-11ea-bb0d-fb1c0dd275d3

Watch a Demo

You can see a demo of the whole process of how to purge content with the Akamai Docker Image here:

You May Also Like:

About the author

Javier

Javier Garza is a developer evangelist at Akamai Technologies where he helps the largest companies on the internet run fast and secure apps by leveraging web performance, security and DevOps best practices. Javier has written many articles on HTTP/2 and web performance, and is the co-author of the O’Reilly Book “Learning HTTP/2”. In 2018, Javier spoke at more than 20 developer-related events around the world, including well-known conferences like Velocity, AWS Re:Invent, and PerfMatters. His life’s motto is: share what you learn, and learn what you don’t. In his free time he enjoys challenging workouts and volunteering for non-profits in areas like children and education