Cache a static site from a bucket
Serve a page from an Object Storage bucket through an Application that caches it, then publish a new version and purge the old copy from the cache.
In this tutorial, you will serve a one-page site from an Object Storage bucket and keep a copy of it in the Azion cache. You will watch the cache answer the page, publish a second version of the page, and purge the old copy so that the new version reaches every reader.
Every resource the tutorial creates links to one created before it:
- The bucket holds the page. Its Workloads Access is read only, so the Application reads it and does not change it.
- The Connector, of the Object Storage type, points to the bucket.
- The Application has a cache setting and a request rule. On every path, the rule sends the request to the Connector and applies the cache setting.
- The Workload holds the domain that receives requests, and its deployment sets the Application that answers them.
The resources are the same in Azion Console, the Azion CLI, and the API. Select the interface you use, and every stage shows its steps.
Prerequisites
Console
- An Azion account. To create one, refer to Create an account.
- Access to Azion Console. To sign in, refer to Access Azion Console.
curl, to request the page and read its cache headers.
1. Create the page
Create a directory for the tutorial and go to it:
Write the first version of the page to an index.html file:
The directory holds index.html, whose heading reads Version 1.
2. Create the bucket and upload the page
Choose a name for the bucket. A bucket name is unique across all Azion accounts and takes 6 to 63 characters: lowercase letters, numbers, and hyphens, starting and ending with a letter or a number. Wherever a step shows <your-bucket-name>, use your name.
Console
To create the bucket:
- Access Azion Console > Object Storage.
- Select + Bucket.
- In Name, enter the name of your bucket.
- Keep Read Only in Workloads Access.
- Select Save.
The bucket appears in the list of buckets.
To upload the page:
- In the list of buckets, select the name of your bucket.
- Drag
index.htmlonto the area that reads Drag files here to add them to your bucket.
The bucket lists index.html with 131 Bytes.
The bucket holds the page.
3. Create the Connector
The Connector points to the bucket. Its prefix / reads the objects at the root of the bucket, so a request for /index.html reads the object index.html. The domain root / has no object to read, so every request on this page names /index.html.
Console
To create the Connector:
- Access Azion Console > Connectors.
- Select + Connectors.
- In Name, enter
cached-site. - In Connector Type, select Object Storage.
- In Bucket, select your bucket.
- Keep
/in Prefix. - Select Save.
The Console shows the message Connector successfully created.
The Connector reads the bucket. It answers requests only after an Application sends them to it.
4. Create the Application
The Application needs three parts: the Application itself, a cache setting, and a rule that applies both the Connector and the cache setting. The cache setting holds two times:
- Azion’s cache keeps a copy for 3600 seconds, one hour. During that hour, the copy answers requests, and the bucket is not read again.
- A browser keeps the page for 60 seconds. The Application sends this time in the
cache-controlheader of the response.
The cache setting also keeps Stale cache on: when a copy has expired and the bucket fails to answer, Azion can serve the expired copy for up to 300 seconds. A new Application has Cache turned on.
Console
To create the Application:
- Access Azion Console > Applications.
- Select + Application.
- In Name, enter
cached-site. - Select Create.
The Console opens the new Application on its Main Settings tab.
To create the cache setting:
- Select the Cache Settings tab.
- Select + Cache.
- In Name, enter
cached-site. - In Browser Cache, select Override cache settings, and enter
60in Max Age. - In Cache, keep Override cache behavior, and enter
3600in Max Age. - Keep Stale cache on.
- Select Save.
The Console shows the message Cache Settings successfully created.
To apply the Connector and the cache setting on every request:
- Select the Rules Engine tab.
- Select + Rule.
- In Name, enter
Serve and cache the bucket. - Keep Request Phase selected.
- In Criteria, keep
${uri}and starts with, and enter/as the argument. - In Behaviors, replace Deliver with Set Connector, and select
cached-sitein the field that it adds. - Select + Add Behavior. In the new behavior, select Set Cache Policy, and select
cached-sitein the field that it adds. - Select Save.
The rule appears in the request phase of the Rules Engine tab.
The Application reads every request from the bucket through the Connector, and keeps a copy of each response in the cache.
5. Create the Workload
The Workload holds the domain, and its deployment sets the Application that answers its requests.
Console
To create the Workload and its deployment:
- Access Azion Console > Workloads.
- Select + Workload.
- In Name, enter
cached-site. - In Deployment Settings, select
cached-sitein Application. - Select Save.
The list of Workloads shows the domain in the Workload Domain column, in the format xxxxxxxxxxx.map.azionedge.net.
Your domain has the same format as the examples, with different letters.
6. Request the page from the cache
A new Workload can take about ten minutes to propagate across the Azion network. Until then, the domain answers 404 with an HTML error page, and it can alternate between 404 and the page for a few minutes.
If you used the Console, set DOMAIN to your domain first, with DOMAIN=<your-domain>. Request the page:
Repeat the request every minute until the page returns three times in a row:
To see whether the cache answered, send the request header Pragma: azion-debug-cache. With it, the response carries the x-cache headers of Azion’s cache. Request the page and print its cache headers:
Each response carries three headers:
x-cachenames the server that answered and what it did.MISSmeans that the server had no copy: it read the page from the bucket and kept a copy.HITmeans that the server answered from its copy, without reading the bucket.x-cache-validis the time, in seconds, that the server keeps its copy: the 3600 seconds of the cache setting.cache-controlcarries the 60 seconds that a browser keeps the page.
Each server keeps its own copy, and your requests can reach more than one server. A server answers MISS the first time it receives the page, and HIT after that. Request the page 20 times and count the answers:
Your count can include a few MISS answers, one for each server that received the page for the first time. The requests also leave a copy of Version 1 on each server they reach. If the counts add up to less than 20, some requests got the 404 page because the Workload is still propagating: wait a minute and run the loop again.
The cache answers the page from its copies.
7. Publish a new version and purge the old copy
Write the second version of the page over index.html:
Upload it to the same key. The upload replaces the object in the bucket:
Console
To upload the new page in Azion Console:
- Access Azion Console > Object Storage.
- In the list of buckets, select the name of your bucket.
- Drag the new
index.htmlonto the area that reads Drag files here to add them to your bucket.
The Console replaces the object without asking for confirmation, and the Last Modified column of index.html shows the time of the upload.
Request the page 10 times and count the versions in the responses:
The bucket holds Version 2, but a server that has a copy of Version 1 keeps answering with it until the copy expires, up to one hour later. Only a server with no copy reads the bucket and answers Version 2, so your count can include a few. A count below 10 in total means that some requests got the 404 page; run the loop again. Until the copies expire, your readers can get either version.
Purge the old copy of the page from the cache. The purge takes the URL without its scheme, so it purges both the HTTP and the HTTPS copies:
Console
To purge the page in Azion Console:
- Access Azion Console > Real-Time Purge.
- Select + Purge.
- In Layer Settings, keep Cache selected.
- In Purge Type, select URL.
- In Arguments List, enter your domain followed by
/index.html, in the formatxxxxxxxxxxx.map.azionedge.net/index.html. - Select Save.
The purge appears in the list of Real-Time Purge, with Cache in Layer and Url in Type.
A few seconds after the purge, count the versions again:
Every response carries Version 2. The purge removed the copies of Version 1, so the first request to each server reads the new page from the bucket, and the next ones answer from the new copy. A purge takes time to propagate: if the count still includes Version 1, wait a few seconds and run the loop again.
8. (Optional) Delete the resources
The site keeps running on your domain after the tutorial. If you want to remove the resources, delete them in this order: the Workload, the Application, the Connector, and the page. Deleting the Workload also deletes its deployment, and deleting the Application also deletes its cache setting and its rule. The Connector is deleted only after the Application, because the rule of the Application references it.
A bucket is deleted only when it is empty and no object was removed from it in the last 24 hours. To remove the bucket too, delete the page first, and the bucket one day later.
Console
To delete the Workload:
- Access Azion Console > Workloads.
- In the
cached-siterow, open the actions menu and select Delete. - In the Delete Workload dialog, enter
cached-site. - Select Delete.
Repeat the steps in Applications for the cached-site Application, then in Connectors for the cached-site Connector. If the Application still exists, the Console refuses to delete the Connector with this message:
To delete the page:
- Access Azion Console > Object Storage.
- In the list of buckets, select the name of your bucket.
- In the
index.htmlrow, open the actions menu and select Delete. - In the Delete File dialog, enter
index.html. - Select Delete.
One day later, delete the bucket:
- Access Azion Console > Object Storage.
- In the list of buckets, open the actions menu of your bucket and select Delete.
- In the Delete bucket dialog, enter the name of your bucket.
- Confirm the deletion.
Before the 24 hours pass, the Console answers with this error:
The account no longer holds the Workload, the Application, the Connector, or the page. The removal of the Workload also propagates, so the domain can keep answering for a few minutes.