Reputation: 3765
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.
Upvotes: 3
Views: 701
Reputation: 3020
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.
Of course, same thing can be done with CloudFront (to stay within AWS ecosystem) or probably most other CDNs.
Upvotes: 3