Caching a REST API (running on Cloud Run) response using Google Cloud CDN

Karthik Guttapudi
3 min readFeb 17, 2022

--

In this article we will see how to cache a REST API response using Cloud CDN.

Google Cloud services which we will using

  • Cloud CDN
  • Load Balancing
  • Cloud Run
  • Logging (Logs Explorer)
  • Artifact Registry

Our simple service has a cats endpoint which returns cat names. The code for this service is available here.
Response of cats endpoint:

Cloud Run

My previous article explains how to run a service on Cloud Run, please refer to it to run the cat-service on Cloud Run.

Make sure the cats endpoint is working,

curl https://cat-service-u7xvbpiada-uc.a.run.app/cats["Luna","Bella","Lily","Lucy","Nala","Kitty","Chloe","Stella","Zoe","Lola"]

Load Balancing, Cloud CDN

Let’s create a HTTP load balancer and configure it,

Load balancing > CREATE LOAD BALANCER > START CONFIGURATION (HTTP (S) load balancing)

In the next screen, Create a backend service, give it a name and select backend type as “Serverless network endpoint group”. In the Backends section, create a new backend — create serverless network endpoint group and select the cloud run function, cat-service.

After creating the serverless network endpoint group, enable the CDN and logging as shown below, make sure to select “use origin settings based on Cache-Control headers” as cache mode.

Expand the “Advanced configuration” section, and add a custom request header “host”, this is required to reach our Cloud Run service from LB as Cloud Run uses the host request header to route to the correct service (which is cat-service in our case), the value of host header should be the fully qualified name of our cat-service ( which is running on Cloud Run) without the https://

Click CREATE after making the above mentioned changes.

Leave the other defaults, give the load balancer a name and create it.

This is our /cats endpoint code, here we are setting the Cache-Control header, this instructs the Cloud CDN to cache the response,

Testing

Get the load balancer’s IP

Access the cats endpoint a couple of times from a browser, the response times can be viewed in the developer tools of the brower and you would notice a significant drop in the response time as the content gets cached by Cloud CDN and delivered from CDN instead of the backend(Cloud Run).

If we look at the Logs Explorer for the above requests, we would see that the first and second /cats endpoint calls were served by backend (Cloud Run) and the remaining are served from cache (Cloud CDN).

Conclusion

That’s it ! we have seen how easy it is to use Cloud CDN to cache the response(s) served from Cloud Run.

--

--