Setting up a Google Cloud CDN is challenging, because it requires you to fully immerse yourself in the vast vocabulary of Google Cloud. The goal of this article is to help get your Rails assets hosted on Google Cloud CDN. Along the way, I will try to contextualize why each step is necessary. At the end of this article, not only will you have your Rails assets hosted on Google Cloud CDN, but you’ll also understand a bunch of new Google Cloud terminology.
www.your-app.com
), because we will
eventually create a custom subdomain for your assets (assets.your-app.com
).A CDN (Content Delivery Network) is a geographically distributed network of servers whose sole purpose is to place content physically closer to end users. For a Rails app, that content consists of scripts, stylesheets and media.
The closer your assets are to end users, the faster they will be able to download them. And this is because computer networks are slower than you might think. To illustrate my point, I’ve included an excerpt from Latency Numbers Every Programmer Should Know below.
Operation | Total |
---|---|
Round trip within same data center | 0.5 ms |
Read 1 MB sequentially from SSD | 1 ms |
Disk seek | 10 ms |
Read 1 MB sequentially from disk | 20 ms |
Send packet CA ➔ Netherlands ➔ CA | 150 ms |
I think it’s key to understand the entire system from a macroscopic level and how your asset request will flow through the system. Once you understand how an asset request flows through the system, setting up each individual service will be much easier because you’ll understand how it fits into the system as a whole.
Take a moment to study the graph below, which describes how an asset request will flow through our CDN.
Now that you understand how an asset request flows through the CDN, we will begin by setting up each service from the bottom-up.
You might be wondering why we’re doing this in reverse. The reason is because I want you to spend as little time in the Google Cloud Console as possible. I brute-forced this process from the top-down many times, and through trial and error I discovered that it’s faster, and it takes fewer steps in the UI to set up each service from the bottom-up.
A Network Endpoint Group is a Google Cloud abstraction that is placed between a Load Balancer and the backend servers. A Network Endpoint Group is what allows us to bring our Rails app into the Google Cloud ecosystem and expose our Rails app to the Load Balancer that we’ll set up in the next section
This abstraction allows Google Cloud to route traffic from a Load Balancer to 3 different types of backend servers: traditional backend servers, Serverless servers and containerized services running on a VM.
But for Rails apps only 2 are relevant, and they are:
I will be focusing on how to setup a Network Endpoint Group for an externally hosted Rails app (e.g. Heroku). I have omitted instructions for Serverless Rails apps because support is currently in beta. If your Rails app is hosted on Google Cloud, you will need to follow these instructions.
www.your-app.com
A typical Load Balancer sits in front of your backend servers, and its main job is to evenly distribute requests to the backend servers without overloading any single server.
A Google Load Balancer provides similar functionality and much more, including the ability to turn your Load Balancer into a CDN with the click of a button.
www.your-app.com
assets.your-app.com
Write down the static frontend IP address so you can create an A record in your DNS settings. This record will map your asset subdomain to your Google Cloud Load Balancer.
Wait for the SSL certificate for your asset subdomain to be provisioned. A green check mark will appear once it has been successfully provisioned.
DNS is how domain names are resolved into IP addresses. This step will connect your asset subdomain to the IP address of your Google Cloud Load Balancer. We will create an A record which is specifically made for mapping domain names to IP addresses.
Visit your DNS host and find the advanced settings.
Create an A record to map your asset subdomain (assets.your-app.com
)
to your Google Cloud CDN frontend IP, which you previously wrote down.
Before you can test, you must wait for your SSL certificate to be provisioned, and your DNS A record to be propagated. Once these 2 tasks have been completed, you can begin testing.
You can test your Google Cloud CDN by using your asset subdomain to access a
public asset on your app. For example, if your public asset is located at
www.your-app.com/style.css
, then replace the subdomain (www
) with your asset
subdomain (assets
), and your public asset should also be available at
assets.your-app/style.css
.
Once your assets are available through your asset subdomain, you will need to
update your Rails app configuration. To set your asset host in Rails, open
config/environments/production.rb
and set
config.action_controller.asset_host
.
1# config/environments/production.rb
2Application.configure do
3 config.action_controller.asset_host = 'assets.your-app.com'
4end
For more details on how to setup a CDN with Ruby on Rails click here.
Congratulations, you’re all done! At this point you should have your Rails assets fully hosted on Google Cloud CDN.
If you found anything that was unclear or outdated please let me know, and I will update this post.