3 reasons to use a CDN

A CDN, or Content Delivery Network can improve your website’s performance while at the same time save you bandwidth. Here’s 3 reasons to use a CDN.


1: CDNs help to improve caching

The fastest way to get data to a client is by not sending it at all. If the client already has the data in his local cache, we can save ourselves the overhead of a data transfer and the latency that comes with the associated request / response cycle.

CDNs greatly help caching by creating a single source of truth. To understand why this is, consider a popular Javascript library such as jQuery, which is used on thousands, if not millions, of webpages. If each website were to host jQuery by itself, effectively we would have thousands or millions of jQuery versions around. Without comparing the file contents (which is only possible after downloading it) the browser cannot know for sure whether http://www.site-a.example.com/lib/jquery.js and http://www.site-b.example.com/lib/jquery.js are in fact the same file or not. Contrast this to the situation where both websites were including jQuery from the same CDN url, such as https://code.jquery.com/jquery-2.1.4.min.js. In that situation, the browser knows, for sure, that both websites are referring to the same file. So it can serve the file directly from cache if it has it.

To make sure we have the greatest chance of our files already being in the user’s cache, we should use the URL that is used by most other websites. This usually means finding the most authorative CDN url for the library we need and include the library directly from there. In the example above, the jQuery website actively lists and promotes that CDN url, so we can safely assume that most websites will be using that. And so we do the same.

2: CDNs reduce load on your server

Every time the user’s browser makes requests to your server, your server takes a small hit in CPU and I/O for processing that request. In addition, there is some bandwidth used for transferring the file back to the client. When your website becomes more widely used, this could start to eat into your server’s resources up to a point where you need more/more powerful servers to keep your website responsive.

The cheapest way to serve files to a client is by having someone else serve them for you, for free!

Most CDNs are completely free. Take the jQuery CDN we talked about. It’s free. Just copy-paste the URL and include from there instead of from your own server and from that moment on you are off-loading all jQuery requests away from your own server.

3: CDNs help improve parallelism

The fastest way to do many little things is to do them all at once.
To understand how CDNs help your website do more things at once, we need a little background.

Requests and the requests-per-domain limit.
Whenever a browser needs something from the server, such as a script or an image, it performs a request to that server. Because it often needs many such resources, to improve performance, browsers will send new requests before the old ones have completed. Thus, multiple requests are being executed simultaneously. To prevent the server from being overloaded with requests, any single client will stay within a connection limit. Once that limit is reached, extra requests are being queued until a previous request has completed. The limit used to be set to 2 simultaneous connections per server (per origin in HTTP-speak), but in recent years it has been increased somewhat to anywhere between 4 and 8 simultaneous connections (source). Still, once the limit has been reached, the browser will *wait* for requests to have been completed before making new ones. This can be seen in the Network tab of the debug console, where such requests get labeled as ‘pending’. This obviously is not good for our site’s performance.

Getting extra simultaneous connections
CDNs help big time here, because they are on a different domain and get their own limit. So a browser with a connection limit of 4, will be able to send 4 simultaneous requests to http://www.site-a.example.com, but also another 4 to http://www.site-b.example.com, all at the same time. For this reason, many websites that serve lots of images, will use many image servers on different domains.

Using a CDN gives you instant and free extra simultaneous connections from any client using your website. As a bonus, because your website’s resources are now spread out over multiple servers, you can now boast at parties that your website is distributed. 🙂

What do you use?

Are you using a CDN right now? See any other advantages? Or disadvantages maybe? Give us your take in the comments below.

Leave a Reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s