o7planning

Introduction to Amazon Cloudfront and its architecture

  1. What is Amazon Cloudfront?
  2. New architecture of CloudFront
  3. Edge Locations Map
  4. Invalidation
  5. CloudFront vs CloudFlare

1. What is Amazon Cloudfront?

If you have a website with users from all over the world, there will be a problem with your users who are too far away from where your server is located. They will have to wait a long time to receive a response from the website.
CDN stands for Content Delivery Network. CDN is a network of many servers located in different parts of the world. These servers store copies of the content of your Website in order to distribute to nearby users. Amazon CloudFront is one of the world's largest CDN systems.
We have two terms here:
Origin Server
Origin Server: This is where your original files are placed. It can be an HTTP Server, Amazon S3 Bucket, etc.
Edge Location (POP - Point of Presence)
Edge Location is also known as POP (Point of Presence) where the user's requests will be sent to. Note that requests of the user will not be sent to the Origin Server directly.
Let's take a look at the illustration below. We are going to see how Cloudfront works when the user requests a file from your website:
1- The user accesses your Website and requests a file such as an image file or HTML, etc.
2 - DNS routes the request to the fastest responsive POP (Edge Location) (minimum latency). Normally, a POP with the closest geographical location to the user, and it will serve the user's request.
3 - At POP (Edge Location), CloudFront will check its cache to see whether or not the file requested by the user is available. If yes, the file will be returned to the user. However, if the file is not in the cache, CloudFront will perform the following steps:
  • 3a - CloudFront will send a request to Origin Server to get the file.
  • 3b - Origin Server will send the file to Edge Location.
  • 3c - As soon as the first bytes of data from the Origin Server are sent to the Edge Location, they will be sent to the user. Moreover, CloudFront adds this file to the Edge Location cache for the next service in case someone requests the file next time.

2. New architecture of CloudFront

Amazon upgraded CloudFront architecture in November 2016. Here is an illustration of the difference between old and new architecture.
We can see the presence of the Regional Edge Cache in the new architecture.
As for the new CloudFront architecture, what will happen if the user requests a file from your website?
1 - A user accesses your website and requests a file such as an image file or HTML, etc.
2 - The user's request will be sent to the fastest responsive POP (Edge Location) (minimum latency).
3 - At POP (Edge Location): if the requested file exists in the POP cache, it will be returned to the user. Otherwise, CloudFront will perform the following steps:
  • 3a - CloudFront will forward the request to Regional Edge Cache.
  • 3b - If the file exists on the cache of Regional Edge Cache, it will be sent to Edge Location. Otherwise, it forwards the request to the Origin Server (...)
  • 3c - As soon as the first bytes of data from the Regional Edge Cache are sent to the Edge Location, they will be sent to the user. Moreover, CloudFront adds this file to the Edge Location cache for the next service in case someone requests the file next time.
Regional Edge Cache has a much larger cache than a single Edge Location, so it stores files including less common ones longer. While Edge Location has a smaller cache, so common files will be stored for a long time. Over time, if a file becomes less common, it will be removed from the cache of Edge Location.
The presence of Regional Edge Cache cuts back on requests sent to Origin Server.

3. Edge Locations Map

The map below shows you the geographical locations of Edge Location(s) and Regional Edge Cache(s) in the world:

4. Invalidation

When a file is stored on the Edge Location (or Regional Edge Cache), it will have a validity period (How long it is in the cache). If before the validity period ends, the user gets access to it, this validity period of the file will be reset from the beginning, otherwise, the file will be removed from the cache.
Note: The validity period of files on the Regional Edge Cache is much longer than that of files on the Edge Location.
If you change the content of a certain file on the Origin Server, the user may not be able to access the new content as CloudFront will send the user the old version of the file if it is in the cache.
You can send an Invalidate request to CloudFront to tell CloudFront to delete a list of certain files from the cache of all Edge Location and Regional Edge Cache.

5. CloudFront vs CloudFlare

  • Cloudfront vs Cloudflare