Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does lambda persist any data in memory?

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?

like image 960
Joey Yi Zhao Avatar asked Jan 25 '23 20:01

Joey Yi Zhao


2 Answers

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:

  • Systems Manager Parameter Store - Use this option if you need to store a single key/value combination.
  • DynamoDB - Use this option if you need a number of key/value combinations.
  • Elasticache - Use this option if you need an in memory key/value store with support for many complex data types. This would require the Lambda being added to the VPC.
like image 107
Chris Williams Avatar answered Feb 01 '23 22:02

Chris Williams


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:

  1. The lack of synchronization between different lambda containers created by multiple invocations of the lambda would cause to store the same data and many misses of already store data in the cache.
  2. If the data in the cache has increased to a level of memory exhaustion needed for lambda for processing could cause to stop lambda working altogether.
like image 28
Farheen Nilofer Avatar answered Feb 02 '23 00:02

Farheen Nilofer