Start free trial

Central data platform for your serverless environment.

Get full access to all premium features for 14 days. No code changes and no credit card required.

Password: 8+ characters, at least one upper case letter, one lower case letter, and one numeric digit

By signing up, you agree to our Privacy policy and
Terms and Conditions.

Leveraging Lambda Cache for Serverless Cost-Efficiency

Share

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 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.

The 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 a 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 then 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 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 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. In case you are interested in mastering serverless architectural best practices, we compiled a list with several battle-tested design patterns in this short video.

Made by Developers for Developers

Our history and present are deeply rooted in building large-scale cloud applications on state-of-the-art technology. Dashbird was born out of our own need for an enhanced serverless debugging and monitoring tool, and we take pride in being developers.

10,000+ developers trust Dashbird

Dashbird helped us reduce the time to discovery from 2-4 hours to a matter of seconds. This means we’re saving dozens of hours in developer time, which not only can be funneled into improving the product but also means hundreds of dollars are saved every month.

Great onboarding: it takes just a couple of minutes to connect an AWS account to an organization in Dashbird. The UI is clean and gives a good overview of what is happening with the Lambdas and API Gateways in the account.

Dashbird provides an easier interface to monitor and debug problems with our Lambdas. Relevant logs are simple to find and view. Dashbird’s support has been good, and they take product suggestions with grace.

Great UI. Easy to navigate through CloudWatch logs. Simple setup.

Dashbird helped us refine the size of our Lambdas, resulting in significantly reduced costs. We have Dashbird alert us in seconds via email when any of our functions behaves abnormally. Their app immediately makes the cause and severity of errors obvious.

Read our blog

Why Are Some Engineers Missing The Point of Serverless?

Why are some engineers missing the point of serverless? Let’s have a look at the common critique points, benefits, drawbacks of serverless, and if it makes sense for your use case.

How Dashbird innovates serverless monitoring

What makes an effective serverless monitoring strategy? In this article, we’re discussing the three core ideas that Dashbird’s serverless monitoring tool was built on top and that should be the fundamentals of any effective serverless monitoring approach.

Debugging with Dashbird: Malformed Lambda Proxy Response

A problem that pops up quite frequently when people try to build serverless applications with AWS API Gateway and AWS Lambda is: Execution failed due to configuration error: Malformed Lambda proxy response.Learn what causes it and how to fix it.

AWS Lambda Error Handling

In this article, we’ll be discussing everything you need to know about the basics of AWS Lambda error handling and some popular methods using StepFunctions and X-Ray.

Deploying AWS Lambda with Docker Containers: I Gave it a Try and Here’s My Review

AWS Lambda now supports Docker Containers. Finally! We gave it a try and here’s our review (with ETL examples)

Go to blog