3

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.

Serge
  • 3,387
  • 3
  • 16
  • 34

1 Answers1

3

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.

mislavcimpersak
  • 2,880
  • 1
  • 27
  • 30
  • any refs or experimental evidence that python level cache wont work? – Serge Jan 16 '18 at 14:23
  • I have a small app online (code: https://github.com/mislavcimpersak/xkcd-excuse-generator). All in all, this is just how CDNs work. The CDN doesn't care that this is a Lambda function; for it, it's just a normal request to your domain. First GET request goes to your app, the other (same) GET requests are served from CDN. You can set cache headers in your app and CDN will (should? I can't say that it's so for every CDN I guess) honor that. That's what I'm doing in my app. – mislavcimpersak Jan 16 '18 at 14:32
  • Keeping it real here, the traffic to my project isn't huge, but if someone shares an image that is served from Lambda on some high-traffic forum or what-not, I'm safe that my Lambda bill won't go through the roof. – mislavcimpersak Jan 16 '18 at 14:35
  • The comment about labmda caching is not substantiated (no arguments) yet voted because some CDN indeed have decent free tier – Serge Jan 17 '18 at 20:03
  • yeah, it's not a direct answer, I admit. But more like a viable alternative when you're on a shoestring budget. The only thing that I have to hold as an evidence is my project that is currently being online for $0 per month using above described Lambda/CDN setup. – mislavcimpersak Jan 17 '18 at 20:50