Does lambda persist any data in memory?

社会主义新天地 提交于 2021-02-07 19:42:12

问题


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?


回答1:


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.



回答2:


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.


来源:https://stackoverflow.com/questions/63227135/does-lambda-persist-any-data-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!