×
Fork me on GitHub

How to configure the Google Cloud Content Delivery Network

The Google Content Delivery Network uses Google’s globally distributed edge points of presence to cache HTTP(S) load balanced content close to our users, to reduce latency and cost. In this blog we configure our example application with terraform in order to understand the components involved and see the content delivery network in action.

To put the content delivery network in front of our application, turns out to be easy: We just enable the CDN cache on the load balancer backends:

deployed gcp application

The content delivery network can front both service and storage backends. This of course, means that we first have to configure our application behind the google HTTP/S load balancer, as described in our previous blog.

CDN on storage backends

To enable CDN caching on storage backends, set the property enable_cdn to true:

resource "google_compute_backend_bucket" "paas-monitor" {
  bucket_name = "${google_storage_bucket.paas-monitor.name}"
  enable_cdn = true

By default, any object served from the bucket will get a cache control of public, max-age=3600. You can override these settings on a per object basis, as shown below:

resource "google_storage_bucket_object" "index" {
  name          = "index.html"
  source        = "./public/index.html"

  cache_control = "public, max-age=300"
  bucket        = "paas-monitor-${var.project}-static-content"
}

CDN on service backends

To enable CDN caching on service backends, set the property enable_cdn to true:

resource "google_compute_backend_service" "paas-monitor" {
  backend {
    group = "${module.instance-group-region-a.instance_group_manager}"
  }
  enable_cdn = true

Backend services do have to return cache control and etag headers on resources we want to have cached. In our go application, we added the following response headers on selected resources:

w.Header().Set("Cache-Control", "public, max-age=60")
w.Header().Set("ETag", etag)

This will cause the content to be cached for 60s. When the content expired in cache, Cloud CDN will issue a request with the etag value in the If-None-Match header. If the etag is still the same, you can return a HTTP status 304 (Not modified), saving network bandwidth, as show below:

if match := r.Header.Get("If-None-Match"); match != "" {
    if strings.Contains(match, etag) {
	w.WriteHeader(http.StatusNotModified)
	return
    }
}

installation

If you want to install this application in your project, install terraform, create a Google project and configure your gcloud SDK to point it.

Then, run the following commands:

git clone https://github.com/binxio/blog-configuring-google-cloud-cdn.git
cd blog-configuring-google-cloud-cdn

GOOGLE_PROJECT=$(gcloud config get-value project)
terraform init
terraform apply -auto-approve
open http://$(terraform output ip-address)

It may take a few minutes before traffic reaches the application. Until that time you will encounter http 404 and 502 errors.

conclusion

Enabling the Google content delivery network on any application, is very easy indeed. Just expose your application through the HTTP/S load balancer and toggle the enable_cdn flag on the backend and away you go!

Picture of Mark van Holsteijn
Mark van Holsteijn
CTO