Everyone wants their website to be popular. You want to get your idea or product out there and the internet is a great way to do that. After a while of things going smoothly you start to get complaints – the website is slow, people in Europe are having a hard time downloading files, the server is getting overloaded. You need to start optimizing your website to provide a fast, consistent experience for everyone that visits.

There are a multitude of different things that one can do, and one of the easiest is to start using a Content Delivery Network, or CDN. For the most part setting up and using a CDN is much easier than attempting to rewrite a site to be horizontally scalable and can provide many different benefits.

What is a CDN?

A CDN is a collection of servers that share a set of static files or content and are usually geographically separated. The CDN takes care of incoming requests for a file, figures out which is the best server to use, and then routes the request to that server. A CDN might have a set of servers in the eastern US, the western US, Asia, and Europe. When a visitor from the UK visits your site, the CDN detects they are in Europe and will route requests for files to their European servers instead of the servers in the US.

In most cases sites will put large video files, images, documents, javascript, cascading style sheets (CSS), and other rarely-changing files up onto a CDN service. Dynamic content will still be hosted like normal, such as from a server in Dallas, TX. It is rare that a website will be hosted entirely on a CDN.

There are two types of CDNs that can be used. One is a CDN service that sits in front of your website that routes all the requests to either other CDN servers or your own servers. Akamai is probably the best known CDN provider that does this. The second type is a CDN that hosts files and you point your application to that other set of servers for requests of static files.

When should I use a CDN?

There are three times where using a CDN may be appropriate.

  1. When large files need to be served quickly to users
  2. Speeding up website load times
  3. Offloading work from the main web servers

A CDN is an option that you should look at whenever you run into any of the above three problems. Setting up a CDN can be much quicker and cheaper than setting up massive storage areas for content, reworking your website to expand horizontally across multiple servers, or purchasing bigger equipment for your servers.

Serving Larger Files to Users

It is not uncommon these days for files to be massive. Digital downloads of games can range anywhere from 500MB to upwards of 10GB. High definition video has become the norm. Users with fast connections to the internet do not want to wait for their content. Users want their downloads as quickly as possible.

Each individual web server only has a certain amount of constant bandwidth though. If you are serving large files you may start to saturate the bandwidth that is available to your server as well as run into overage charges from your hosting provider. Putting your larger files onto a CDN shifts the bandwidth issue to a service that is specifically designed to handle large files and provide quick downloads to users.

CDNs, by their nature, allow a large collection of computers to handle the download requests. This, coupled with CDNs picking a server that is close to the user, speed up download times for your users. A user in Japan no longer has to hop all the way from Japan to Dallas, TX to download a file, they can do that from a local CDN server.

Speeding up Website Load Times

By default, web browsers will only make a certain number of connections to a website. If you have 100 elements on your page that need to be downloaded, the browser will only make so many connections at once. If the browser is limited to 6 connections at a time the page elements will be queued for downloading. While the first thing that one should do is reduce the number of elements that need to be downloaded, you still have to contend with the connection limit at the browser.

One way around this is multiple domains. The connection limit is per domain so you can use multiple domains to serve content. If you host your dynamic content on ‘www.domain.com’ and your static content on ‘cdn.domain.com’, that doubles your connection limit are you can now load more content in parrallel. Couple this with the geographic nature of a CDN and their large bandwidth allocations, and you can push more content to your users faster.

Setting up a CDN for this purpose may require some changes to your website. You will have to know what content is on the CDN and what isn’t, and have your website link appropriately. Most content management systems have plugins that will take care of this.

Offloading Work from the Main Web Servers

Since CDNs are separate from your main website, you get the benefit of serving less requests on your own servers. This translates to more resources and potentially faster response times from the dynamic portions of your site. Your servers no longer have to deal with large downloads, or dealing with multiple connections for images and CSS that rarely change. This opens up more connections to be made.

Depending on how the CDN is set up, you could also benefit from parallel loading of elements when using multiple domains, or use a transparent CDN like Akamai which, while you loose parallelism, will reduce the stress on the server yet still benefit from the CDN’s speed and locations.

CDN Providers

There are many CDN providers out there, both that function strictly as CDN services and some that can double as CDN services. Akamai is one of the largest and well known providers, while Edgecast is one that is used by many media companies. Cloudflare is a newer transparent CDN that also offers other services such as spam filtering.

You can build your own CDNs using your own hardware hosted in clouds like Amazon’s EC2 or Rackspace Cloud Servers, but that requires setting up mirroring between servers and running your own servers. Amazon offers their S3 service for content storage which can be used for content delivery, and a companion service called Amazon Cloudfront which will distribute the resources in the S3 buckets out to servers in different regions.

Which one you goes with depends on your budget and your team. A highly technical team can implement a CDN themselves or use on of the cloud file servers like Amazon S3 to bootstrap a CDN. For teams that don’t have the luxury of a team that is well versed in servers or the cloud, paying extra for one of the full-blown CDN services could be a wise investment.

One final thing to keep in mind is that a CDN is not a magical bullet that can be used to speed up a site. If your site is slow a full analysis should be done to determine where the slowdown is. Using a CDN might be a great addition to your website’s application stack if your site is hosting large files or has enough traffic to start to slow down under heavy load.