What is CloudFront in AWS?
CloudFront is a service that speeds up the delivery of static content to users. CloudFront accomplishes this through a network of edge servers that cache static files. An edge server is an entry point in the network. By providing a larger network of edge servers, user’s requests have less distance to travel and can receive data faster. Latency is measured in the amount of time it takes to receive the first byte of a file.
The goal is to store the content as close to the end user as possible thereby reducing latency.
How does it work?
Each request is routed to the closest edge server. If the content being requested is already stored in that edge server, then the content is returned to the client. Else, the request is directed to the intended source (usually S3 for our usage), and returned to the client. Once returned to the client, the content will be stored in that edge server for future retrieval.
In AWS, this means two things: faster content delivery and lower costs. It’s cheaper to load content from Cloudfront than loading directly from S3 every time.
When objects are updated, the edge server cache is invalidated and the request will forward to the source, for instance an S3 bucket, and the cache will be updated for future requests.
Serving static content from CloudFront is generally cheaper and faster.
Essentially, you tell CloudFront where you want it to serve content from and CloudFront will optimize the distribution.
Here’s the step by step breakdown of how it works:
1. Choose Origin Server (Typically, we’ll use S3 as the origin. Although, your own http server can be the origin as well.)
2. Upload files to origin server
3. Create Cloudfront Distribution
4. Specify logging
5. Domain name assigned
6. Distribution is sent to edge locations
Use cases for CloudFront
According to Amazon, here are the most appropriate use cases for CloudFront:
Static website delivery
This is the main use case for CloudFront. CloudFront makes serving static files, like images, videos, or website content more performant.
On-demand or live streaming video
You can use CloudFront to live stream video files in standard formats like HLS, MPEG, etc.
CloudFront also enables you to cache media fragments, essentially just chunks of the video, at the edge for faster delivery.
Encrypt fields throughout system processing
CloudFront can enable an encryption system where only certain Amazon resources can see the data. This can allow you to get even more fine grained about what systems have visibility into data.
Using Lambda@Edge, you can perform highly available, fast functions.
How CloudFront Delivers Content
1. User requests access to a file.
2. DNS Routes them to the closest CloudFront edge location.
3. The edge server checks for the file. If the file is there, the file is served to the user. When the file is not there, CloudFront will forward the request to the origin server (your own web server or S3) and will access the appropriate files. If the files exist on the origin server, they will be cached at the edge location and returned back to the user.
CloudFront is a very powerful layer of the AWS ecosystem. It’s used highly throughout our recommended stack, and something nearly all applications should take advantage of.