Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function state (zappa)

I think to optimize flask app lambda server by adding internal cache for relatively slow to change data (e.g. site dropdowns might changes few times per year). I use zappa to deploy to lambda. Does it make any sense? Or does it flash memory each time a request processed. I know that I can not rely on aws preserving state, my goal here optimize performance a bit without spending a fortune on some redis instance not to mention ElastiCache.

UPDATE: Yup, the serverless deployment frameworks like zappa recycle the state, so why I should not. Below a hackernoon blog discuss the state recycling in greater details

https://hackernoon.com/write-recursive-aws-lambda-functions-the-right-way-4a4b5ae633b6

Whilst Lambda functions are ephemeral by design, containers are still reused for optimization which means you can still leverage in-memory states that are persisted through invocations.

Not sure can one invalidate such cache, env variables are likely local to lambda instance, http, sns probably difficult/expensive.

like image 944
Serge Avatar asked Feb 14 '26 12:02

Serge


1 Answers

Yeah, that's not gonna work with Lambda.

You have to use some sort of 3rd party cache.


If caching only your GET requests is good enough for you, you could use a CDN for that.

I personally use CloudFlare CDN that caches all GET requests for n minutes. And you get a lot of requests for free. You just have to define a custom Page Rule to cache everything for a certain URL pattern.

CloudFlare Page Rule

Of course, same thing can be done with CloudFront (to stay within AWS ecosystem) or probably most other CDNs.

like image 64
mislavcimpersak Avatar answered Feb 17 '26 01:02

mislavcimpersak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!