How to Memoize/Cache Lambda Function

Hi,

Lambda functions are intended to be stateless, you can’t guarantee that the data will be there or even that it will run on the same server it ran on before. That said, any variables you establish outside of your handler function will cache for a time on that single server, and future requests will use it until the server wipes the cache. There are no promises that it will be there or how often or how long. One more thing you can do is pass a custom cache control header and we’ll cache the response, again there is no guarantee on how long. Ultimately the better option would be to make a proper backend, or if you need caching for products try standing up a redis instance or any other database as a service and querying that instead.

4 Likes