I'm using a Java 8 AWS Lambda to process records through API Gateway, save data to S3, send Firebase notifications, and invoke another Lambda for background tasks. Originally, cold starts took around 20 seconds, but my warmed execution time is about 500ms. I tried pinging the Lambda every 2 minutes to keep it warm, which helped, but I still experience occasional cold starts roughly once an hour. I switched to provisioned concurrency with two instances, which cut down cold starts to about 10 seconds, but it doesn't match the 500ms warm performance. I'm curious why provisioned concurrency doesn't completely eliminate cold start delays and whether it's worth the extra cost if it doesn't maintain consistently low response times. Here are my Lambda stats: Java 8 on Amazon Linux 2, x86_64 architecture, 1024MB memory (using ~200MB per invocation), and 512MB ephemeral storage.
5 Answers
Have you tried using SnapStart? It might help with cold starts.
It’s possible you’re exceeding your provisioned concurrency. If you have 2 warm Lambdas but your requests spike to 8, those additional Lambdas will cold start. In our case, we found it more cost-effective to keep our Lambdas warm than switch to an always-on solution like EC2. It’s worth considering your architecture if this is a frequent issue.
20 seconds is quite extreme for a cold start!
If you're relying on a scheduled action to keep your Lambda warm, it might not be the best fit for your workload. By the way, are you packaging your code as a container or a zip file?
Java is often not ideal for Lambda. It tends to have longer cold start times.
I’m using a JAR file stored in S3 and loading it via its S3 URI. I’ve noticed that the slowest part is making the initial connection to S3, which can take about 10 seconds.
You might want to experiment with changing the Java compilation tier. This can significantly improve cold start times, though it might come at the cost of runtime optimization. Be sure to measure your Lambda performance to find the right balance.
If I were in your shoes, I'd consider switching to a different language. Java's cold start is generally slower since it requires starting a JVM, which takes a lot of time. JVM-based Lambdas need at least 128-512MB RAM just to function, and your code could require even more. If fast cold starts are critical, Go is a great option. But if you prefer ease of use, Python is a solid choice.
I tried SnapStart before switching to provisioned concurrency, but I didn’t see much improvement. I might give it another shot to see if it makes a difference.