Live data from Hacker News

AWS Lambda Cold Start Times

filia-aleks.medium.com

121–130 of 233 posts

Re: AWS Lambda Cold Start Times

#121
post #114

Earlier quoted context omitted.

Same experience with Firebase. I just joined a team that has been using it. I've never worked with serverless before, and it boggles my mind how anyone thought it would be a good idea. The cold starts are horrendous. In one case, it's consistently taking about 7 seconds to return ~10K of data. I investigated the actual runtime of the function and it completes in about 20ms, so the only real bottleneck is the fucking…

Not a fit for every scenario, but you can set a minimum number of instances for Firebase to keep ready. Should help reduce cold starts: https://firebase.google.com/docs/functions/tips#min

Yeah I tried that, it doesn't work. Ultimately the provisioning algorithm is a black box.

Re: AWS Lambda Cold Start Times

#122

At my last job we built an entire API on top of serverless. One of the things we had to figure out was this cold start time. If a user were to hit an endpoint for the first time, it would take 2x as long as it normally would at first. To combat this we wrote a "runWarm" function that kept the API alive at all times. Sure kind of defeats the purpose of serverless but hey, enterprise software.

"To combat this" Did you actually need to? That's one of the things that always threw me with complaints about cold starts - how many apps/etc do I use daily where I interact, and there's a 10 second delay before something happens? The answer: quite a lot. Yeah, we can do better. And in fact, with Serverless, -most users will experience better-. It's only when load is increasing that you see those delays, and then it…

There is actually a really awesome middle-ground that AWS offers that no one seems to talk about.

That is using ECS + Fargate. This gives you (IMHO) the best of both worlds between Lambda and EC2.

ECS is Elastic Container Service. Think docker/podman containers. You can even pull from Dockerhub or ECR (Elastic Container Registry - amazon's version of dockerhub). ECS can then deploy to either a traditional EC2 compute instance (giving you a standard containerization deployment) or to "Fargate".

Fargate is a serverless container compute instance. It is like serverless EC2. You get the "serverless" benefits of Lambda, but it is always-on. It has automatic scaling, so it can scale up and down with traffic (all of which is configured in ECS). You don't need to manage security updates of the underlying compute instance or manage the system. You get high-availability and fault tolerance "for free". But at the end of the day, its basically a non-managed EC2 instance. You can choose the ram/cpu options that you need for your Fargate just like any other compute instance. My recommendation is go as small as possible and rely on horizontal scaling instead of vertical. This keeps costs as low as possible.

When I hear people trying to keep Lambdas running indefinitely, it really defeats the purpose of Lambda. Lambda has plenty of benefits, but it is best used for functions that run intermittently and are isolated. If you want the serverless benefits of Lambda, but want to have the benefits of a traditional server too, then you need to look at Fargate.

And of course there is a world where you combine the two. Maybe you have an authentication service that needs to run 24/7. Run it via ECS+Fargate. Maybe your primary API should also run on Fargate. But then when you need to boot up a bunch of batch processing at midnight each night to send out invoices, those can use Lambdas. They do their job and then go to sleep until the next day.

I should also add that the developer experience is far superior going the ECS+Fargate route over Lambda. I have built extensive APIs in Lambda and they are so difficult to debug and you always feel like you are coding with one hand tied behind your back. But with ECS+Fargate you just build projects as you normally would, with your traditional environment. You can do live testing locally just like any other container project. Run docker or podman on your system using an Amazon Linux, Alpine Linux, CentOS base. And that same environment will match your Fargate deployment. It makes the developer experience much better.

Re: AWS Lambda Cold Start Times

#123

I'm surprised Node has cold-start issues. I had it in my mind that JS was Lambda's "native" language and wouldn't have cold start issues at all. Did it used to be like that? Didn't Lambda launch with only support for JS, and maybe a couple other languages that could compile to it?

I wonder ho w much time was spent requiring all of aws-sdk. The v3 sdk is modular and should be quicker to load. Bundlers like rebuild save space and reduce parsing time.

Re: AWS Lambda Cold Start Times

#124

If anyone is running into cold start problems on Firebase, I recently discovered you can add .runWith({minInstances: 1}) to your cloud functions. It keeps 1 instance running at all times, and for the most part completely gets rid of cold starts. You have to pay a small cost each month (a few dollars), but its worth it on valuable functions that result in conversions, e.g. loading a Stripe checkout.

This is basically how Google Cloud Run works if I'm not mistaken.

Google Cloud Run also scales to zero by default, but you can configure the minimum number of instances: https://cloud.google.com/run/docs/configuring/min-instances

Re: AWS Lambda Cold Start Times

#125
The main downside of Lambda, in particular for user facing applications is that the incentives of the cloud provider and you are completely opposed. You (the developer) want a bunch of warm lambdas ready to serve user requests and the cloud provider is looking to minimize costs by keeping the number of running lambdas as low as possible. It's the incentive model that fundamentally makes Lambda a poor choice for these types of applications.

Other downsides include the fact that Lambdas have fixed memory sizes. If you have units of work that vary in amount of memory required you're basically stuck paying the costs of the largest units of work unless you can implement some sort of routing logic somewhere else. My company ran into this issue using lambdas to process some data where the 99% of requests were fine running in 256mb but a few required more. There was so way to know ahead of time how much memory the computation would require ahead of time. We ended up finding a way to deal with it but in the short term we had to bump the lambda memory limits.

That doesn't even get into the problems with testing.

In my experience, Lambdas are best used as glue between AWS components, message processors and cron style tasks.

Re: AWS Lambda Cold Start Times

#126
I wish the author had done a comparison for Java apps using JLink that generates a custom Java runtime image that contains only the platform modules that are required for a given application, and if that makes a difference.

Re: AWS Lambda Cold Start Times

#127
post #88

that is why the language for cloud native is GO, and languages like Java/C# are dead tech stack, they failed to reinvent themselves to stay relevant

>"they failed to reinvent themselves to stay relevant" This sounds like it came straight out of "The Corporate BS generator" - https://www.atrixnet.com/bs-generator.html .

no, it's the direct translation of the benchmark of this post

cold start should be a thing of the past, hence the "failed to reinvent themselves to stay relevant"

you need to speak to the hardware directly, not to the VM or the embedded compiler

with ARM and ultimately RISC-V coming, that's gonna be even more true

Re: AWS Lambda Cold Start Times

#128

I wish the author had done a comparison for Java apps using JLink that generates a custom Java runtime image that contains only the platform modules that are required for a given application, and if that makes a difference.

jlink usage won't make any significant difference typically. What does make a difference is (App)CDS though, as available in newer Java versions. Memory-mapping a file with the class metadata from previous runs can easily shave off a second or more from time-to-first-response [1], depending on number and size of classes required for that.

[1] https://www.morling.dev/blog/smaller-faster-starting-contain...

Re: AWS Lambda Cold Start Times

#129

Conclusion here is to write 1 huge lambda instead of several small lambdas. right?

I think if you get to this point with lambda you're probably overthinking it. I think language runtime choice is important because some choices do have a cost, but likewise, choosing lambda is a tradeoff -- you don't have to manage servers, but some of the startup and runtime operations will be hidden to you. If you're okay with the possible additional latency and don't want to manage servers, it's fine. If you do and want to eke performance, it might not be.

Larger lambdas mean a higher likelihood of concurrent access, which will result in cold starts when there is contention. Your cold starts will be slower with more code (It's not clear how much the size of your image affects start time, but it does have SOME impact).

It's best to just not worry about these kinds of optimizations -- that's what lambda is for. If you *want* to worry about optimizing, the best optimization is running a server that is actively listening.

Scope your lambda codebase in a way that makes sense. It's fine if your lambda takes multiple event types or does routing, but you're making the test surface more complex. Just like subnets, VPCs and everything else in AWS, you can scope them pretty much however you want and there's no hard fast rule saying "put more code in one" or "put less code in one", but by there are patterns that make sense and generally lots of individual transactions are easier to track and manage unless you have an explicit use case that requires scoping it to one lambda, in which case do that.

There are a few cases where I've advocated for bigger lambdas vs smaller ones:

* grapqhl (there still isn't a very good graphql router and data aggregator, so just handling the whole /graphql route makes the most sense)

* Limited concurrency lambdas. If you have a downstream that can only handle 10 concurrent transactions but you have multiple lambda interactions that hit that service, it might be better to at least bundle all of the downstream interactions into one lambda to limit the concurrency on it.

Post reply on HN