The S3 storage service within Amazon Web Services provides a very simple way to store and host data. Once particular scenario in which it proves to be very convenient is in the hosting of JavaScript files that will be pulled down into browsers via call outs within web site pages. In many of these cases, the time it takes for the browser to pull down the JavaScript files will significantly impact the perceived overall time for the web site to load within the browser. Couple this will the potential network latency based on physical geographical distance between the server providing the files and the browser loading the files and the download time can increase dramatically.

Content Delivery Network

One way to reduce the amount of time it takes for any browser around the world to download the JavaSript files is to use a Content Delivery Network (CDN) to host the files. As defined by wikipedia, a CDN is “a large distributed system of servers deployed in multiple data centers across the Internet.” The main purpose of a CDN is to be able to serve files to those requesting them from the closest geographical location.

CloudFront is a CDN backed by S3 buckets.

Experiment

Conceptually it make sense that using CloudFront for hosting JavaScript files for our use case would be better than hosting from S3 directly.

However, to see what impact this would really have, I created an experiment to compare load times of the same JavaScript file from servers around the world using both CloudFront and S3. The details of the experiment are simple, a 8.7KB JavaScript file was put in a S3 bucket and then a CloudFront distribution was created with that same S3 bucket backing it. Then I used CA’s free website check site to check the load times using the public URLs for both S3 and CloudFront.

The following graph shows the results. Note, the times are in milliseconds and is for the total load time (resolve, connect, and download).

cloudfront_vs_s3_download_times

In all cases, the file loaded faster from the CloudFront compared to the S3 one. This is to be expected. What was surprising is how much faster the load times were in some regions.

Cost

Given this particular use case, where there is not a need to serve very large files like video, the cost for CloudFront is minimal. The current price is less than $0.25 for the first 10TB per month and is less than $0.03 per 10K request block (see actual pricing at http://aws.amazon.com/pricing/cloudfront).

Although using CloudFront would be an additional cost over using S3 directly, for this use case, the benefit makes it easy to justify.