AWS CloudFront Basics
When a user request an image at http://mywebsite.com/myimage.png, this request is routed through a complex network to reach the original server which causes latency. To bypass this latency, you can use AWS CloudFront.
CloudFront is AWS’ Content Delivery Network (CDN) service. It caches data and acts as an intermediary between the original server and client. Amazon has numerous data centers, called “edge locations,” throughout the world. Let’s assume a client in California makes a request to a website hosted in Italy. The client’s request will be routed to the closest edge location in California. CloudFront will check if the data is already cached at this edge location. If it is, then it will immediately return the data to the client which saves time. If the data is not cached at this location, then the request will be routed to the original server in Italy which will return the data to the California edge location, the data will be cached at this location for the next time, then the data will be returned to the client.
Region Edge Caches: In reality, there is one additional step between the edge location and original server. Regional edge caches are located between the original server and multiple edge locations. As objects become less popular, individual edge locations might remove those objects to make room for more popular content. Regional edge caches have a larger cache than an individual edge location, so objects remain in the cache longer at the nearest regional edge cache location. When a viewer makes a request on your website or through your application, DNS routes the request to the edge location that can best serve the user’s request. This location is typically the nearest CloudFront edge location in terms of latency. In the edge location, CloudFront checks its cache for the requested object. If the object is in the cache, CloudFront returns it to the user. If the object is not in the cache, the edge location typically goes to the nearest regional edge cache to fetch it. In the regional edge cache location, CloudFront again checks its cache for the requested object. If the object is in the cache, CloudFront forwards it to the edge location that requested it. For objects not cached at either the edge location or the regional edge cache location, CloudFront forwards the request to the origin server.
- Accelerates static website content delivery
- Better performance for live-stream and on-demand. CloudFront will batch streaming requests to reduce load on the original server. In addition, CloudFront can stream in multiple formats: MPEG DASH, Apple HLS, Microsoft Smooth Streaming, and CMAF, to any device.
- Encrypt specific fields
- Web: Static content such as .html, .css, and images
- RTMP: Real-time messaging protocol is a TCP-based protocol that enables high quality and lightning fast transmission of audio and video data over the Internet between a player and a server.
- Origin Domain Name: S3 bucket, load balancer, or EC2 instance.
- Custom Distribution Domain Name: By default, AWS will create a domain name for the distribution. However, you can set the distribution domain name to your existing domain name (i.e. mywebsite.com)
- Cached Duration: By default, each file is stored in the edge location for 24 hours.
CloudFront Managed Prefix List: A list of CloudFront IP addresses: com.amazonaws.global.cloudfront.origin-facing. You can set a security group for your original server so that it only accepts requests from CloudFront.