DEV Community

Cover image for Caching with Amazon CloudFront on AWS
Akshat Mishra
Akshat Mishra

Posted on

Caching with Amazon CloudFront on AWS

_*" I don't need a hard disk in my computer if I can get to the server faster... carrying around these non-connected computers is byzantine by comparison " _- Steve Jobs
*

Amazon CloudFront is a content delivery network (CDN) operated by Amazon Web Services. Content delivery networks provide a globally-distributed network of proxy servers that cache content, such as web videos or other bulky media, more locally to consumers, thus improving access speed for downloading the content.
For understanding CloudFront we first need to understand essentially the concept of caching. Let's say, there is some data that is getting frequently accessed and everytime the request takes like 2 seconds to complete. What if we already have this data in a temporary memory? Then response time to open it will reduce to like somewhere between 1.5-2 seconds. This is called caching, and the temporary memory is called cache.

Caching

Now a question may arise what should I cache? Here are a few examples -

  • Frequently accessed data : This can be stored in CloudFront Edge Locations

  • Static Data : CloudFront increases the speed of delivery of static content also like Images, HTML files, CSS files i.e., the data that is not frequently changed. By using CloudFront we can also take advantage of AWS Backbone Network and CloudFront edge servers to give your viewers a fast, safe & reliable experience.

  • Data that requires slow and expensive queries.

  • Customize at the edge using Lambda@Edge to serve private content.

  • Encrypt specific fields throughout system processing.

  • Video on demand or live streaming video(where cache would be for multiple requests).

Now, for storing data in Cache we have 2 components on AWS Global Infrastructure - Edge locations(also called point of presence) and Regional Edge Caches.
Edge Locations contain the data centers where servers are used specifically for caching.
Regional Edge Caches brings more of your content closer to viewers if the content is not popular enough to stay in Edge Locations.

architecture

Now, Cloud Front has collection of caching servers present all around the world through which it serves content to the end users with faster responses and it serves the content quickly through Edge locations to give low latency to users. It also speeds up distribution of static and dynamic web content to user & delivers content through world wide data centers present in edge locations. Thus CDN reduces the load on application origin and improves user experience. It also stores the content in caching servers globally. Examples of Origin include S3 Buckets, HTTP server, for example hosted on an EC2 instance.

How CloudFront delivers content?

Note - Origin is S3 for Image files and HTTP for HTML Files.

If regional edge caches are also required

Expiring Caches

Three methods-

  • Add TTL
  • Change object name
  • Invalidate the object

TIL (Time to live) - The objects will be stored in cache for a particular time. Default TTL is 24 hours.

Thus, CloudFront speeds up distribution of for example your websites by routing user request through AWS backbone network to Edge location that can best serve your content.

Hope You had fun reading.
Happy Coding!!

Top comments (0)