Google Cloud: Caching Cloud Storage content with Cloud CDN

Share At:

In this Lab, we will configure Cloud Content Delivery Network (Cloud CDN) for a Cloud Storage bucket and verify caching of an image. Cloud CDN uses Google’s globally distributed edge points of presence to cache HTTP(S) load-balanced content close to our users. Caching content at the edges of Google’s network provides faster delivery of content to our users while reducing serving costs.

For an up-to-date list of Google’s Cloud CDN cache sites, see

Task 1. Create and populate a Cloud Storage bucket

Cloud CDN content can originate from different types of backends:

  • Compute Engine virtual machine (VM) instance groups
  • Zonal network endpoint groups (NEGs)
  • Internet network endpoint groups (NEGs), for endpoints that are outside of Google Cloud (also known as custom origins)
  • Google Cloud Storage buckets

In this lab, we will configure a Cloud Storage bucket as the backend.

Create a unique Cloud Storage bucket

  1. In the Cloud Console, on the Navigation menu > Storage > Browser.

2. Click Create bucket.

3. Specify the following, and leave the remaining settings as their defaults:

Try to choose a location that is either halfway around the world from you or at least in a different continent. This provides a greater time difference between accessing the image with and without Cloud CDN enabled. See the current list of regions here.

4. Click Create.

5. Note the name of your storage bucket for the next subtask. It will be referred to as [your-storage-bucket].

Copy image files into your bucket

Copy images from a public Cloud Storage bucket to our own bucket.

  1. In the Cloud Console, click Activate Cloud Shell
  2. If prompted, click Continue.

3. Store [your-storage-bucket] in an environment variable in Cloud Shell:

export YOUR_BUCKET=<Enter the name of [your-storage-bucket]>

4. To copy two images from a public bucket to our bucket, run the following commands:

gsutil cp gs://cloud-training/gcpnet/cdn/cdn.png gs://$YOUR_BUCKET

gsutil cp gs://cloud-training/gcpnet/cdn/regions.png gs://$YOUR_BUCKET

5. In the Cloud Console, click Refresh Bucket to verify that the images were copied.

6. In Cloud Shell, to publish the image files to the web, run the following command:

gsutil iam ch allUsers:objectViewer gs://$YOUR_BUCKET

7. In the Cloud Console, click Refresh Bucket to verify that both images have Public access set to Public to internet.

Task 2. Create the HTTP load balancer with Cloud CDN

HTTP(S) load balancing provides global load balancing for HTTP(S) requests of static content to a Cloud Storage bucket (backend). When you enable Cloud CDN on our backend, our content is cached at a location at the edge of Google’s network, which is usually far closer to the user than your backend is.

Start the HTTP load balancer configuration

  1. In the Cloud Console, on the Navigation menu > Network Services > Load balancing.

2. Click Create load balancer.

3. Under HTTP(S) Load Balancing, click Start configuration.

4. Select From Internet to my VMs, and click Continue.

5. For Name, type cdn-lb

Configure the backend

  1. Click Backend configuration.
  2. For Backend services & backend buckets, click Create or select backend services & backend buckets > Backend buckets > Create a backend bucket.

3. For Name, type cdn-bucket

4. Click Browse under Cloud Storage bucket.

5. Select our bucket, and click Select.

6. Select Enable Cloud CDN.

7. Click Create.

Configure the frontend

The host and path rules determine how our traffic will be directed. For example, you could direct video traffic to one backend and image traffic to another backend. However, you are not configuring the host and path rules in this lab.

  1. Click Frontend configuration.

2. Specify the following, leaving all other values with their defaults:

3. Click Done.

Review and create the HTTP load balancer

  1. Click Review and finalize.
  2. Ensure that Cloud CDN is enabled for the Backend buckets. If it is not, return to the backend configuration to enable Cloud CDN.

3. Click Create. Wait for the load balancer to be created.

4. Click on the name of the load balancer (cdn-lb).

5. Note the IP address of the load balancer. It will be referred to as [LB_IP_ADDRESS]. In our case it is —

6. Open a new browser tab and navigate to http://[LB_IP_ADDRESS]/regions.png, replacing [LB_IP_ADDRESS] with the IP address of the load balancer.

You should see an error because the load balancer needs time to be available from all edge locations. Wait a couple of minutes and keep refreshing the browser tab until you can see the image. In the meantime, you can read about caching details, including the cache mode.

7. When you can see the regions.png image (shown below), continue to the next task.

Task 3. Verify the caching of your bucket’s content

Now that you have created the HTTP load balancer for your bucket and enabled Cloud CDN, it is time to verify that the image is cached on the edge of Google’s network.

Time the HTTP request for an image

One way to verify that an image is cached is to time the HTTP request for the image. The first request should take significantly longer, because content is only cached at an edge location after being accessed through that location.

You already accessed and cached the regions.png image. Therefore, you will focus on the cdn.png image in this task.

  1. In Cloud Shell, store the IP address of the load balancer in an environment variable:

export LB_IP_ADDRESS=<Enter the IP address of the load balancer>

2. Run the following command for consecutive HTTP requests:

for ((i=0;i<10;i++)); do curl -w \ “%{time_total}\n” -o /dev/null -s http://$LB_IP_ADDRESS/cdn.png; done

In this example output, the second and third request take less than 1% of the time of the first request. This demonstrates that the image was cached during the first request and accessed from an edge location on further requests. Depending on how far away you placed your storage bucket and where your closest edge location is, you will see different results.

Invalidate the cached content

When an object is cached, it normally remains in the cache until it expires or is evicted to make room for new content. Sometimes, you might want to remove an object from the cache before its normal expiration time. You can force an object, or set of objects, to be ignored by the cache by requesting a cache invalidation.

  1. Open a new browser tab and navigate to http://[LB_IP_ADDRESS]/cdn.png, replacing [LB_IP_ADDRESS] with the IP address of the load balancer.

This image is very dated and needs to replaced with a newer image.

2. In Cloud Shell, replace the old image with a newer image:

gsutil cp gs://cloud-training/gcpnet/cdn/updatedcdn.png gs://$YOUR_BUCKET/cdn.png

3. Return to the browser tab for http://[LB_IP_ADDRESS]/cdn.png, and reload the page.

You should see the old image because it is still cached!

4. In the Cloud Console, on the Navigation menu > Network Services > Load balancing.

5. Click on the name of the load balancer (cdn-lb).

6. Click Caching.

7. For the Path pattern to invalidate, type /cdn.png

8. Click Invalidate and wait for the invalidation to complete under the Recent cache invalidations section.

This sends off the request to invalidate cache entries, so the command takes some time to run.

9. Return to the browser tab for http://[LB_IP_ADDRESS]/cdn.png, and reload the page.

You should now see a new image.

This concludes our tutorial for “Caching Cloud Storage content with Cloud CDN”.

Happy Learning !!!

Share At:
0 0 votes
Article Rating
Notify of
Oldest Most Voted
Inline Feedbacks
View all comments
binance register
11 days ago

Can you be more specific about the content of your enticle? After reading it, I still have some doubts. Hope you can help me.

Κωδικ Binance
24 days ago

Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
3 months ago

Nice article.Quick question about consideration.I want to have a soda where the person drinks the soda and can choose to see if they have won but going to the website and entering the number that is on the back of the bottle.If they win they get a gift card if not nothing as I am not trying to capture their information.I am trying to give back to the individuals who purchase the product but I am not sure that is possible.Does your company have something that can work for me?

Back To Top

Contact Us