In today’s digital age where most people are using gadgets, we’re likely familiar with the term “cache“. Most of us first hear about cache when looking to free up storage or improve performance on our mobile devices especially for android users. If we look at our device’s settings then navigate to the storage of an application, we will see two options: one is to delete the app entirely, and the other one is to clear its cache. Of course if you’re still using your application, you’ll click the second option. And after it, voila! Your application seems to run smoother. With this, we are certain that this cache is a type of data. But, what type of data exactly? Why does removing it improves the app’s performance when we are technically removing something? Let’s talk about it first.Â
Understanding Cache: Boosting Speed and Performance through Data Retrieval
Like what I have stated about, cache is a type of data or memory for applications. This type of data is temporary and only designed for quick access unlike the usual data that are in the storage itself. This cache saves the data that you access a lot from an application. Say for example, your favorite image with your friends, the online page of your favorite artist, episodes from sitcoms you binge watch, or a website where you can improve your Cloud computing expertise like our very own Tutorials Dojo! Cache stores the data from these applications or websites, so when you return to them, the loading and performance will be much faster. Instead of retrieving or downloading the same files again, your device will retrieve the saved data from cache. This results in less waiting time and a smoother experience.
Global Caching with Amazon CloudFront
Caching doesn’t just happen within your devices, but actually in a global scale. This is achieved by what we call Content Delivery Networks (CDNs). These CDNs utilize the storing of cached data in multiple locations all over the world to cater users that accesses your application or website from wherever they are. An example of that is the Amazon CloudFront that aims to distributes content—such as web pages, images, and videos—to users with low latency and high transfer speeds. It stores copies of these contents at various “edge locations” around the world, allowing users to fetch the content from a location closer to them instead of traveling all the way to the server.
For example, your server is in North California, USA and a user from the Philippines will be accessing your website. Even though fetching the data from Philippines to United States is certainly achievable through undersea cables like Bifrost, it would still take time. If the request takes 5 seconds to travel and another 5 seconds to fetch the data back to the user, the entire data retrieval process takes 10 seconds. However, with CDNs like Amazon CloudFront, data temporarily stores in edge locations or Points of Presence (PoP) near you. And this is achievable since AWS has numerous data centers around the world to cater users globally. This means your requested data fetches from these PoPs instead of the origin server. And the best thing here is that instead of 10 seconds, it will only take 1 second to retrieve and load your data!Â
Balancing the Benefits and Drawbacks of Caching
Even though caching offers benefits for enhancing performance, it also comes with several downsides. For one, cached data becomes outdated since it provides older data compared to regularly updated data from the origin server. Also, managing caching can be complicated and it poses security risks. And as mentioned in the introduction, it consumes storage space. To address these downsides and maximize its benefits, automated tools can be vital in optimizing cache management. An example of this that will be shared here is an automated tool that executes CloudFront invalidation through Slack which can simplify the process of refreshing cached data.Â
Automating CloudFront Invalidation via Slack
Below are the steps that you should follow to create your own automated tool that will execute CloudFront invalidation through a slack command:
STEP 1: Create a CloudFormation template and deploy it to your chosen region.
AWS CloudFormation is an Infrastructure as Code (IaC) service that automates the creation and management of AWS resources using a JSON or YAML formatted text file. In this template, you set up a serverless Lambda function to invalidate a CloudFront distribution’s cache with the following features:
- It will first accept CloudFront distribution ID, and Slack details such as signing key, channel, and webhook URL as parameters.
- The Lambda function, written in Python, will use these parameters as environment variables and is granted permissions to invalidate CloudFront cache.
- It includes a public URL, so anyone can trigger it directly.
- An IAM role with the necessary permissions is created.
- The function’s public URL is provided as output for easy access to trigger cache purge.
STEP 2: Fill in the parameters:
- Stack name – unique identifier for your CloudFormation template
-
DistributionId – The CloudFront Distribution ID of the website
-
SigningKey – The signing secret key of your Cache Manager Slack app which you can get here: https://api.slack.com/apps/
-
SlackChannel – Channel in slack where you can invoke the command
-
SlackUrl – Webhook URL of the slack channel
STEP 3: Click Next -> Next -> Tick all checkboxes -> Submit
STEP 4: Wait until stack is created
STEP 5: Once done, click the Stack -> Outputs tab, then copy the value of the Lambda FunctionURL.
STEP 6: Open your Cache Manager Slack app. Under Add features and functionality, click Slash Commands.
Step 7: On Slash Commands, click Create New Command and enter the following details:
-
Command –
/cdn-purge
-
Request URL – The Lambda FunctionURL that you copied in Step 5.
-
Short Description – Invalidates the Mainsite CloudFront
STEP 8: Click Save
Conclusion
Congratulations! After following the steps outlined above, you’ve successfully created your own automated tool for streamlining cache management. A complicated task can now be done effortlessly with a simple Slack command. Now, you can utilize caching to provide your global users with the latest information and data from your website while ensuring low latency wherever they are. From here, you can try to explore other complicated tasks and think of a way on how you can improve them through automation. Consistently improving your processes and finding new ways to be efficient will enhance work productivity and leads to more accomplishments.