A great way to improve your time to first byte and page speed is to use a Content Delivery Network (CDN). CDN’s distribute your website’s assets around the globe. You can think about them as handling a key aspect of your site’s logistics.
By distributing content the push it physically closer to your visitor. When files do not need to travel as far they get to the requester faster.
When I first read I need to use a CDN in Steve Souders’ book I thought he was nuts. Since then the rise of cloud services has made CDNs a low cost commodity that anyone can afford.
There are multiple reasons you should use a CDN today. But first you need to understand how content delivery networks work.
How a Content Delivery Network Works?
Like I said, CDNs distribute your content around the world in different data centers. Today this is a common service offered by cloud providers and for very little cost.
Traditionally when you host a website it is on a server in a single data center. This means when someone across the country or on the other side of the world requests a page on your site it must travel a long way.
The time it takes packets to traverse back and forth between these two locations varies, but distance and the number of hops increases the time or latency between the two points.
A CDN would use the web server as an origin or source of content. It copies the content from the web server to each of the CDN distribution points. Now, your content is closer to potential customers. The time to traverse the Internet is reduced, shortening your time to first byte.
In addition to distributing your content content delivery network services also make it easier to use and manage modern web features like HTTP/2. This also gives your site performance advantages.
Performance
Light travels at 299,792,458 meters / second, which sounds really fast. Remember sunlight takes 8 minutes and 20 seconds to reach Earth. On the Internet you add friction forces, routers and other hops across the Internet and a packet’s speed is not near the speed of light.
When content is hosted physically near the end user it does not need to travel near as far, which of course if faster. CDNs help make this possible. You just need to upload the file to a central server and it is automatically distributed to the other servers around the world. The CDN provider manages routing the requests to the closest physical server.
Fiddler, the popular network sniffing tool, provides estimated information on how long a request to various parts of the world might take using an estimation formula. The formula produces estimated results and they are available in the statistics panel.
While this is not an real world measured value, it should serve as a guideline of end user discomfort caused by your distant assets crossing the globe.
Scalability
When static assets are served from a CDN they are not on your web server. When a file is served it must come from the disk. Anytime you hit the disk you are creating a server-side performance hit because disk I/O is very expensive. You have also used your web server’s (IIS, Apache, etc) threads that could otherwise be allocated to serving dynamic content.
Anytime you can avoid serving static content from your dynamic web application server, like ASP.NET or Node, you should. A web server should be tuned for the content it is serving. A rock band uses multiple guitars during a concert because each song is performed in different keys and the guitars are pre-tuned for the desired key. Similarly a web server should be tuned for either static or dynamic content. CSS, JavaScript, images and other media assets tend to be static, meaning they are not created at request time.
While some markup is static, often an application uses some sort of rendering. Even for a properly built SPA the markup has some server-side rendering involved. So these web servers are tuned for dynamic content, run through services like ASP.NET, node, ruby, etc.
Price
Today you can host your content in Azure or AWS for next to nothing. I moved my images to Amazon’s S3 many years ago and have layered CloudFront on top of the bucket. My monthly cost is usually less than 15 cents US. Granted this Blog and other other applications I host generate a small traffic volume, it is very cheap.
In the past there were not many CDN options and all were very expensive. Today there are many. Shop around to find the one that best suites your needs. CDNs tend to customize in a specific areas. Some are faster, others more distributed and so on. If you are just starting out both Azure and AWS have easy to setup, low cost solutions.
Summary
A CDN is something every web owner should utilize. They will help your application perform better, allow your web server to scale and do not cost an arm and a leg. Please invest some time and start using a CDN today.