Improve your website performance Using Google Cloud CDN

Meiyappan Kannappa
5 min readDec 4, 2022

In continuation of the story Software Platform and its Business Value, my fictional friend Visalakshi started her home chef business, expanding it across cities as well as onboarding new home chefs. Now she started facing issues in her online service since she started scaling her business, the slowness of the website. Images take avg of 500 ms to download. Perhaps there were unwanted hits to the site may be DDOS attacks, bot traffic etc.

Her entire solution was hosted in GCP Cloud Run and Postgres as the Database. It is a microservice architecture with a frontend hosted in Cloud Run as well. The solution had Content Management System Strapi as well hosted in the cloud run to manage the contents for the website and mobile apps.

A major problem in the system now is

  1. Image load times are getting higher day by day. Chef Images, Images of the Food Menu.
  2. Static Contents like tags, and labels are loading slowly.
  3. Irrelevant API requests (without a token, from same IP address)

Reduce latency using CDN

For the first two problem statements, CDN will be a good solution. What is CDN?

From Akamai: A content delivery network (CDN) is a group of geographically distributed servers that speed up the delivery of web content by bringing it closer to where users are. Data centres across the globe use caching, a process that temporarily stores copies of files so that you can access internet content from a web-enabled device or browser more quickly through a server near you. CDNs cache content like web pages, images, and videos in proxy servers near to your physical location. This allows you to do things like watch a movie, download software, check your bank balance, post on social media, or make purchases, without having to wait for content to load.

Basically, CDN is edge cache and there are popular CDNs in the market like AKAMAI, Azure CDN and Google Cloud CDN etc.

Since Visalakshi hosted the solution in GCP, we will leverage Cloud CDN. To enable Cloud CDN, the easiest way is to enable it via Google Cloud Load balancer.

Requests Served from CDN for images & Static content

First, the Cloud Run Services should be enabled via Google Cloud Load Balancer. You need to configure the frontend and backend, then need to define paths and rules. It would be good practice to reserve and use static external IP.

Front End Configuration
Backend Configuration

In this scenario, the Home Chef solution has multiple microservices which are routed by the Google LB using the API Path and would have a common domain name. The Content Management System which serves the static images, data etc will be served from a different domain name like cms.homechef.com and the path will be routed to Strapi CMS deployed in Cloud Run.

Host and Path Configuration

Now we have configured the load balancer, we next need to enable CDN so that images and static contents are served from the cache. While you do your Backend configuration you can enable CDN by selecting the checkbox. You have 3 types of CDN Configuration

Cache Static Content — Static content includes web assets (CSS, JavaScript & fonts), audio and video formats, and PDF content, based on the Content-Type in the response. Contents from public URLs (without authentication will be cached) are automatically cached. Origin responses that set valid caching directives are also cached.

This is the default behaviour for Cloud CDN-enabled backends created by using the Google Cloud CLI or the REST API.

Use origin settings based on Cache-Control headers — Cloud CDN will only cache responses with valid cache directives in the response headers, such as ‘Cache-Control: public, max_age=3600’. Successful responses without these directives are forwarded from the origin.

Force cache all content — Cache all content served by the origin, ignoring any “private”, “no-store” or “no-cache” directives. Unconditionally caches successful responses, overriding any cache directives set by the origin. This mode is not appropriate if the backend serves private, per-user content, such as dynamic HTML or API responses.

In this use case we are serving content from Strapi, most of it is publicly readable content. Considering it, we will use the third option, which is to cache all contents served from Strapi in Cloud Run.

Cache Configuration in CDN

Next, you should define TTL, there are two TTLs.

Client Time to Live — It allows you to set a shorter time to live for browsers or clients, and to have those clients revalidate content against Cloud CDN on a more regular basis. The value of the client's time to live cannot be greater than the maximum time to live but can be equal.

Default Time to Live — Specifies the default time to live for cached content served by this origin for responses that do not have an existing valid time to live. The value of default time to live cannot be set to a value greater than that of maximum time to live but can be equal.

Invalidate Caches

Now Visalakshi updates contents, to refresh the content in the CDN cache or to invalidate it, Navigate to Cloud CDN in Google Cloud Console and select Caching Tab. In the Load Balancer input, select the Load Balancer and the path (you can use /* as wild card path) to click Invalidate cache. The subsequent requests will fill the cache retrieving from the origin.

References:

  1. https://cloud.google.com/cdn/docs/caching?_ga=2.44135012.-1519102165.1657459153

--

--

Meiyappan Kannappa

Technical enthusiast, daydreamer. Design of involute software architecture for digital transformation, sustainability and cost optimization.