How we built a CDN to drastically improve site performance

By Aamir Khan

Last summer at Haven Life, where we use AWS for much of our infrastructure, we started an initiative to take advantage of Cloudfront Distribution (AWS Content Delivery Network). At the time, the havenlife.com domain pointed to an nginx load balancer. We predicted that moving large static files like images to deploy on servers in various regions, rather than in one singular repository (a load balancer), would improve site load-time. While the rest of the code base performs all logic, moving these files out would keep it smaller, more compact, and more easily manageable.

Below is a diagram of the cloudfront architecture we built for this project. The yellow boxes are relative paths of the havenlife.com domain, and on the bottom left of the diagram “AWS Route 53” represents our DNS web service where we configure all of our domains and aliases for all environments.

Cloudfront architecture

In our Cloudfront Distribution setup we built two types of origins:

  1. AWS S3 bucket (top right of diagram) is our object storage service where we upload files such as images or videos that would otherwise take a long time to load. To begin this project our team identified several images used on the site which we removed and stored in an S3, where you can store anything, secure it, and be provided a hyperlink to quickly direct to load the file.
  2. Classic Load Balancer where every container has a load balancer in front of it. Load balancers then forward the requests by virtue of round robin algorithm to the respective service. Here we technically have 3 different origins:
  • The load balancer pointing to nginx-haven
  • The load balancer pointing to quotes-v3
  • The AWS S3 bucket.

The results were better than we expected. Previously when an image was requested it would immediately hit the load balancer, but now it first hits cloudfront, then checks the cache or subsequently the S3 bucket.

Testing site performance using Google’s Lighthouse, we saw a 71–94% increase in site after Cloudfront implementation

It improved site load time and, along with other improvements we made, load time was improved by almost a factor of two.

--

--