Amazon CloudFront Tutorial

AWS logo (Copyright Amazon AWS)

Amazon CloudFront is a content delivery network. You change the URLs of static (not frequently updated) files in your website HTML, and then people who visit your website get the files from CloudFront. Because a company like Amazon has a lot of resources, they can set up servers in many parts of the world, including Europe, Asia, and North America. This improves response time for requests from those locations.

This tutorial shows how you can use Amazon CloudFront for static resources. CloudFront improves website performance.

Performance

Note

A CDN improves performance for all pages on your specific server, because the load of the server is reduced due to fewer requests. The downside is that another DNS lookup will occur for your content delivery network, which may take several milliseconds. Overall, though, even those files are faster to download because of the server locality.

Get started

To get started with CloudFront, you must go to Amazon Web Services and make an account. If you already have an Amazon account, then you can use that. You must additionally create a AWS account. Amazon provides a lot of cloud computing resources, including computational ones, but CloudFront is only for content delivery. Once you create your CloudFront account, you may also need to receive an automated phone call from Amazon for security purposes. This requires about 30 seconds of your time.

Amazon web page

Security credentials. Before you can access or upload data to your CloudFront bucket, you must receive your security credentials. These are displayed on the Amazon Web Services - Account screens. You will need your Access Key ID and your Secret Access Key.

Client program

You will need to upload your data files to Amazon CloudFront, and the simplest way to do this is to use a client program. If you are using Windows, you can use a program such as CloudBerry Explorer. There are many other options for Windows and other platforms. You can even use the CloudFront API to develop your own software to do this.

CloudBerry ExplorerCloudBerry Explorer

Using CloudBerry Explorer. You can think of this program as sort of an FTP client for Amazon CloudFront. You will need to enter your security credentials into CloudBerry when you first start it up so that it can access your Amazon CloudFront account. Then, you can, inside CloudBerry, create a bucket, and then drag and drop files from your local computer into the cloud.

Next, you will need to get the web URL for the CloudFront data you upload. If you upload a file into the cloud, it will retain its file name. You can address the file using your automatically-generated and unique CloudFront webserver URL. For example, with Dot Net Perls my CloudFront web server is http://d142qn1ou6ht8n.cloudfront.net/; to access specific files, I can use http://d142qn1ou6ht8n.cloudfront.net/2, where '2' is the complete filename.

Cost

How much does the Amazon CloudFront service cost? I don't know the complete results for this yet, but for five hours of peak traffic for Dot Net Perls, I incurred about six cents ($0.06) of costs. The costs for the web server running the website is many times higher than that per day, and if Amazon CloudFront can reduce the need to upgrade that server, it will likely save me money.

Note: This ignores the fact that CloudFront improves the usability and performance of the site.

Performance tips

Performance optimization

The CloudFront cache is where the big performance gains come into play with CloudFront. CloudFront uses edges (servers in local regions) and these servers have caches. If a local server has a cache of the data, the user will receive it quickly.

HTTP timing results

static    6.70 KB   73 ms
	  X-Cache: Hit from cloudfront
~timespan 5.60 KB 1170 ms
	  X-Cache: Miss from cloudfront

Performance results. The above results show the difference in a hit and a miss response from CloudFront. The file "static" was loaded in only 73 milliseconds and was a hit. The file "~timespan" was loaded in 1.17 seconds and was a miss. Most of the time spent on the miss was latency, not transferring data. In this time, CloudFront's edge location had to pull the file from the central location (Amazon S3).

Tip: Increasing cache hits are the most effective way to improve CloudFront performance.

Programming tip

Reduce object size. One performance tip I have found to be useful with CloudFront is to minimize object size. The CloudFront edges (servers at various locations) use caches. The objects on the servers are expired based on a variety of characteristics, including frequency and object size.

This means that one way to ensure your object is in the cache more of the time is to make it smaller. Large objects that are used a medium number of times may be expired, but smaller objects would be kept. So, if you can't make objects more popular, you can make them smaller.

Isolate popular data. On many websites, there are some pieces of data such as CSS or JavaScript that are popular. This means they will be stored in the CloudFront cache on edge locations. The key here is to isolate them: don't combine them with less popular data that might change based on the user or the page.

This tip means you may end up with more HTTP requests and slightly more files for your website. Another benefit here is that when you isolate popular data, the less popular files become smaller and are therefore more likely to be in the cache themselves.

Question and answer

Edge locations. Does CloudFront's design help with people accessing data from other continents? To answer this, I looked at the number of requests coming in from each edge location. I found the following percentages of edge locations were used:

United States: 39%
Europe: 41%
Japan: 3%
Hong Kong and Singapore: 16%

So, even though 100% of the pages on this website come from the United States, the most popular edge location to access them is in Europe. This means that, according to the CloudFront algorithms, the largest group of users can download files fastest from Europe.

Set Content-Type. The Content-Type header is important to use on files from CloudFront as it can help indicate how long files need to be kept on the edges. I don't have data on how much this affects performance, but this Content-Type header is effective:

Cache-Control header [1 day]

Cache-Control: public, max-age=86400

Tip: Hosting static files in a distributed way is effective for improving website performance and decreasing costs. It becomes more important to approach site optimization from the perspective of the cache algorithm on the edge locations.

Summary

I looked at the Amazon CloudFront content delivery network, which is a web service that provides an easy and fast cloud computing approach to performance optimization on websites. By hoisting resources from my webserver to CloudFront, I was able to improve performance and also potentially save money on hardware.

Amazon.com [External]
.NET