Skip to main content

Using a Content Delivery Network (CDN)

Once you start having more than one webserver (and thus need a load balancer), or even before that, you will need to have a CDN to deliver static content to your users. It's usually best to set it up before beginning your website, as otherwise you'll have a lot of files to move and database entries to change.

The purpose of a CDN

Your website's webservers are meant to serve webpages, like HTML and PHP files, not loads of other files and content. A CDN is basically a global network of servers, which you can use to deliverĀ static content (content that doesn't have to be evaluated or 'executed' by the server, like an image or video) to your website users, instead of your webservers doing it. This has a couple of major benefits:

  • It frees up the network bandwidth of your webservers, making them respond faster to requests
  • The CDN can deliver files from a server closer to the user, so they will load faster due to lower network latency
  • The bandwidth costs for using a CDN will be lower than using your webserver's bandwidth
  • The CDN caches files, making for much more efficient delivery

Getting a CDN up and running

Your CDN requires anĀ origin - a location it will obtain files from, i.e the files you want it to serve to users. This is generally a cloud storage bucket, such as an S3 bucket. When the CDN doesn't have a copy of the file, it requests it from the origin, and then caches the copy to serve to users later.

You therefore need to do a few things to get your CDN working:

  • Create a cloud storage bucket, e.g. an S3 bucket, to serve as your CDN's origin
  • Decide on a domain name (not mandatory, but it looks nicer). Usually it is a subdomain of your existing website's domain name, e.g. "cdn.yourwebsite.com".
  • Have access to your domain's DNS records and be able to edit them, if you want to use a custom domain name.