Optimizing Websites Using Cloud CDN on Google Cloud

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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. Periodically refresh the website until the correct site displays.

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 a 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 your IP address 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, and select Network Services, followed by 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 0.001-0.003 seconds) response time, compared to the 0.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, followed by Logging.
  • From the dropdown menu, go to Cloud HTTP Load Balancer. Select http-frontend, followed by http-lb.
  • Expand one of the recent entries.
  • Under httpRequest, you should see a field labeled cacheHit: true, which confirms that this request was filled and served from the cache, not the original backend location.

Additional Resources

We will be using the Cloud CDN service to take an HTTP load-balanced application that is hosted in Australia and cache it closer to a testing instance in the us-central1 region.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?