Cost-efficiency is one of the main pillars of the Serverless Well-Architected framework. Read-intensive applications can save money and improve efficiency by using Lambda cache systems. AWS Lambda’s internal memory could be used as a caching mechanism.
A Lambda container remains alive after an invocation is served, even if it stays idle for some time. Whatever was loaded in the container’s memory will remain there for the next invocations. And that can be used as a caching mechanism as we demonstrate below. In some cases, this could be an alternative to external caches such as Redis or Memcached.
Basic structure of a Lambda function
The basic structure of a Lambda function is the main file containing a handler function:
What runs inside the lambda_handler function is lost after the execution ends, but what is loaded outside the handler function will remain in memory as long as the container is alive.
Consider a read-intensive backend service that provides data about application users. It receives a username as an argument, looks for the user in a database, and returns the information to the requester.
Caching data outside Lambda handler
The basic idea of using Lambda for caching is to start keeping this information in memory, outside the handler function. We could delegate the user data collection to a caching mechanism that would be loaded outside the handler function.
Whenever user information is needed, we ask the cache. If the username is not available, the cache automatically pulls it from the database and updates its own internal buffer. In future invocations, when the same username is requested, the cache won’t have to query the database again.
cache_by_username variable is a key-value map. Everything loaded there will remain in memory throughout the Lambda container lifecycle. In subsequent requests, retrieving cached user data will not only be faster but also cheaper, since we won’t have to hit the database.
Considerations for Lambda caching
This is obviously an oversimplification of the problem. We would likely need to consider some other factors to make sure our caching works properly.
In the example above, the function isn’t really doing anything with the cached data other than returning it. In these cases, we could cache in a higher-level part of the infrastructure by using CloudFront with API Gateway, for example. This would avoid requests from even reaching the Lambda function.
Another aspect is cache size and resource usage. How much memory does the Lambda function need and how much is allocated? The difference is roughly what we have available for caching. If the Lambda cache starts to grow further, it may cause memory exhaustion errors and prevent the function from working altogether. Thus, it’s a good idea to run a benchmark and avoid this potential failure point.
Another thing to consider is the lack of synchronization between Lambda containers. When multiple invocations are running concurrently, Lambda has to spin up one container for each. They will not have the exact same usernames in their caches, as invocation arguments hitting each container will hardly be the same.
For that reason, we can’t ensure consistent caching performance across all Lambda invocations. Depending on the number of concurrent requests and the variability of usernames requested, the Lambda cache might end up having to hit the database most of the time in practice.
The Well-Architected framework is composed of four other pillars apart from Cost-Efficiency. You can learn more about serverless architectural best practices in this free e-book, covering the basics and the best practices for all five Well-Architected pillars.
Did you know that by using Dasbird, you can save up to 80% on your time spent on debugging? Built by serverless developers with specifically serverless technologies and AWS Lambda in mind, Dashbird gives you customized and actionable insights based on the AWS Well-Architected Framework to further improve your infrastructure, and provide a quick and easy to understand real-time overview of the health and performance of your serverless infrastructure.
You can give Dashbird a try for free:
- No code changes
- No credit card required
- Simple 2-minute set up
- Start debugging and securely working with your data immediately
- Free access to all premium features (including pre-configured real-time alerting, detailed observability into your serverless stack, Well-Architected insights checks and reporting, and more!)
- Simple, clean, and easy to understand interface
- One of the most budget-friendly monitoring and troubleshooting solutions in the market
- Small-team-friendly all around 🙂