The Cloud CDN service is able to bring your websites much closer to the end user by caching content at their nearest Edge Point of Presence. This lab will show you how to use Cloud CDN to bring your website “closer” to your end users.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Deploy your environment
Open Cloud Shell
Copy and paste the below command to build out your HTTP load balanced environment:
gsutil cp gs://acg-gcloud-course-resources/network-engineer/cdn/cdn-setup-script.sh . && source cdn-setup-script.sh
After the script completes, enter the listed frontend IP address in a new tab. It will take roughly 5 minutes after script completion for the load balanced application to fully initialize. Perioudly refresh the website until the correct site displays.
- View and test performance of non-cached website
View and test performance of non-cached website
Since our website server is very far away from us, let’s see how long it takes to request data from it.
In your new web browser tab, enter the frontend IP address if you have not already done so. Once the website loads, click on the link for Page 2.
Depending on your own location, it may load quickly or slowly. If you are in North or South America, the images should load very slowly because you are very far away from the backend servers.
Let’s measure the exact performance. Go to Compute Engine, and SSH into your
testing-server
instance.Once in your instance, run the below commands to send a series of curl requests to both pages of our website and view how long it takes to come back. We will want to reduce these response times via caching.
- Keep in mind your frontend IP address will be different. Substitute in the below commands as necessary.
for i in {1..15};do curl -s -w "%{time_total}n" -o /dev/null http://35.190.125.64/index.html; done
for i in {1..15};do curl -s -w "%{time_total}n" -o /dev/null http://35.190.125.64/page-2.html; done
- Enable Cloud CDN
- Leave your SSH window up, and go back to the web console.
- Go to the top left menu – Network Services – Cloud CDN
- Select Add origin
- From the Load balancer dropdown menu, select
http-lb
- Your
http-backend
backend service should be automatically selected. Click Add
- Measure Cached Performance
It will take a few minutes for your Cloud CDN settings to be fully initialized. Go back to your
testing-instance
and run the above curl commands again. If you do not get substantially quicker results, wait a few minutes and try again. A few minutes later, repeating the command will show substantially quicker results (about .001-.003 seconds) response time compared to the .19 second responses in the cached version. Repeat this for both your index.html and page-2.html pages.- View logs
Let’s check our Stackdriver logs to confirm that the cache was filled as well:
- From the top left menu, go to Stackdriver – Logging
- From the dropdown menu, go to Cloud HTTP Load Balancer – http-frontend – http-lb
- Expand one of the recent entries
- Under
httpRequest
, you should see a field labeledcacheHit: true
, which confirms that this request was filled and served from the cache, not the original backend location.