- Serving Static Files using Cloud Run - 1
- This blog post explains how to serve static files using Google Cloud Run. It focuses on redirecting and performance improvements.
This post has been translated by AI.
Post summarized by durumis AI
- We've built a CDN system that leverages Google Cloud Storage (GCS) and Cloud Run to efficiently manage images and web resources, delivering optimized files tailored to web browser environments.
- Uploaded images are converted and stored in various formats such as webp and avif, while text files are compressed using gzip, br, and other methods to provide users with optimized content.
- By deploying Cloud Storage and Cloud Run across 8 global regions, resources are delivered from the nearest region to the user's location, minimizing latency and enhancing user experience.
Previous Postexplained how durumis utilizes Cloud Run to serve static files.
However, unlike files like robots.txt or favicon.ico, image files uploaded to the blog or resources of the web page itself cannot be simply treated as static files.
Therefore, these files need to be handled using Cloud Run integrated with Google Cloud Storage (GCS).
The basic concept is simple. When image files are uploaded in real-time or new resources are uploaded during a site update, they are uploaded to GCS. When a request comes from outside, Cloud Run checks if the file exists in GCS and, if it does, transmits the file.
However, the actual working mechanism is much more complex than that.
When uploading images to GCS, durumis basically converts image files into three different formats.
When uploading jpeg or png files, the image files are automatically converted into webp and avif formats and stored simultaneously.
All image files are requested via URLs without extensions. Upon request, Cloud Run checks the requesting web browser's header to see if it can receive avif and webp formats in that order. If neither of these formats is supported, the file is delivered as jpeg or png. In addition to simple format conversion, resizing is also performed beforehand based on the size needed by the web page, and the files are stored and delivered accordingly.
Diagram
The same applies to text files.
Many resources needed on websites include not only images but also text files such as js and css.
Similar to images, js and css files are compressed with gzip and br during upload and stored. When a request is made, the web browser is served the supported file format.
The role of a CDN is not limited to simply caching content. In the case of Google Cloud Platform (GCP), Cloud CDN functionality can be directly connected to the Google Cloud Load Balancer (GCLB), providing even more powerful features.
Cloud CDN receives data from the Backend Service, stores it, and then delivers it to the client. During this process, Cloud CDN can deliver optimized files based on the client's request header. This allows for the delivery of different content formats for the same URL.
For example, for the same URL '/image/tempImage':
- In the case of image files,
- Different formats of images are delivered based on the browser's 'Accept' header.
- Chrome browsers generally indicate a preference order of 'image/avif,image/webp,image/apng'.
- The CDN delivers the image in the preferred format, and if it is not supported, it substitutes with jpeg or png.
- Similarly, for text files,
- Preference for compression methods is also considered.
- Chrome browsers support compression methods such as 'gzip, deflate, br, zstd'.
- For example, the durumis service currently supports gzip and br, so it prioritizes the delivery of br-compressed files.
These advanced features of CDN allow for the delivery of content optimized for the user's environment, significantly improving website performance and user experience.
Even with Cloud CDN, file requests generally need to be made from the CDN to Cloud Run. In the case of durumis, we deploy Cloud Storage and Cloud Run in 8 regions worldwide, pre-deploying resources and delivering them. This approach, while incurring slightly higher costs and workload, provides significantly improved latency for users.
Even when using Cloud CDN, files must typically be fetched from the origin server (in this case, Cloud Run). Durumis has optimized this structure to provide a better service to global users.
Durumis's global optimization strategy:
- Pre-deploy Cloud Storage and Cloud Run in 8 regions around the world
- Pre-distribute necessary resources to each region
- Deliver resources from the nearest region when a user requests them
This approach requires a slight increase in costs and additional work, but it offers users the following benefits:
- Significantly improved latency
- Faster content loading times
- Overall improved user experience
This structure can be visualized as follows:
Through this structure, durumis can consistently provide fast service to global users. Since content is delivered from the nearest region, regardless of the user's location, service can be accessed with low latency anywhere in the world.
This approach requires more effort in initial setup and maintenance, but it offers a significant improvement in the end-user experience. This structure can be particularly helpful for companies offering global services in providing a competitive service.
In today's CDN discussion, we only covered the basic concepts.
In fact, we could have discussed the detailed settings, but since they differ depending on the cloud vendor (GCP, Azure, AWS) and are less interesting, we focused on explaining the concepts.
If there's an opportunity in the future, I will cover the more detailed aspects.
So, in the next discussion, we'll explore how to synchronize Cloud Storage across multiple regions (we have 8!).
Thank you.