I have below code in AWS lambda:
const cache = {};
exports.handler = async (event) => {
// TODO implement
if (cache[event.key]) {
console.log('read from cache');
return cache[event.key];
}
console.log('generate value');
cache[event.key] = Math.random()
return cache[event.key];
};
when I run the the lambda and I can see read from cache
on the log which means the lambda cache some values in cache memory. Does the lambda persist its memory when it becomes warm? Does it mean I can make use of this memory for some caching in order to improve performance?
It is possible to persist things between invocations of Lambda (assuming that the invocations hits the same MicroVM), but your application should not rely on it for performance.
There is no guarantee for how long it will persist on the MicroVM, also be aware by the volume you store in memory as this may impact the performance of your Lambda if no enough memory is available for it. If you're looking to improve performance then take a look at some of these options:
Lambda container remains alive even after the invocation is complete. So, whatever data loaded in the container's memory will be available throughout the Lambda container lifecycle.
So, this memory can be used as leverage for caching.
Data in the cache would be useful if your application is read-extensive. For example in a case where the same username has to be fetched from Database, we can cache the username for future invocations. Thus, mitigating the cost of DB query.
Above approach seems to help but there are few downsides as well:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With