AWS Lambda执行时间过长(冷启动)与Spring Cloud。

huangapple go评论54阅读模式
英文:

AWS Lambda taking so long to execute (cold start) with Spring Cloud

问题

以下是翻译好的内容:

有一个需求,我们正在尝试将现有的Spring Boot REST API转换为AWS无服务器架构。

以下是我们正在遵循的架构。

API网关 --> AWS Lambda --> Postgres DB(本地EC2实例)

在API网关中,我们已经创建了REST API设置,并将我们的Lambda集成到该API中。

在Lambda中,我们使用了Spring Cloud和Java-8,它进一步使用Spring Data JPA与PostgresDB连接以进行CRUD操作。

问题:
如果我们谈论Lambda,那么它的运行情况如预期一样良好。像Spring项目一样启动,按预期执行工作,但问题在于Spring Cloud具有一些内部附加依赖项,因此Lambda的大小变得约为40-50 MB,并且响应时间超过30秒。因为API网关的最大超时设置为30秒,所以导致API网关给出了网关超时错误。

根据分析,我们观察到AWS Lambda始终启动完整的Spring项目,还会创建与数据库的连接,这是一个耗时的部分。

作为最终用户,这不是一个良好的体验,因为API需要很长时间才能响应,而实际上不应该如此。

有人可以帮助我了解我们在基础架构方面或在Java(Spring Cloud)方面做错了什么吗?

我们如何减少Lambda执行时间?我们如何缓存数据库连接或任何其他Java缓存,以使Lambda更快,从而减少API的响应时间。

TIA

英文:

There is a requirement where we are trying to convert the existing Spring boot rest API to AWS serverless architecture.

Below is architecture we are following.

> API Gateway --> AWS Lambda --> Postgres DB (local EC2 instance)

In the API Gateway, we have created a REST API setup and we have integrated our lambda to that API.

In the Lambda, we have used Spring Cloud with Java-8 and it is furthur connecting with PostgresDB for the Crud operations using Spring Data JPA.

Probelm :

if we talk about the Lambda then it is working fine as per expected. Like Spring projects get started and it will do the work as expected but the problem is because Spring Cloud has some internal extra dependencies so because of that Lambda Size become approx. 40-50 MB and it is taking more than 30 seconds to respond. Because of this API Gateway gives Gateway Timeout as the max timeout setup on API Gateway is 30 seconds.

As per the analysis, we observed that AWS lambda always starting the complete Spring project and also creating connection with DB which is a time consuming part.

As an end-user it is not a good experience because API is taking so long to respond which it should not take.

Can anyone help me understand what we are doing wrong on the Infra side or on Java (Spring Cloud) side ?

How we can reduce the Lambda execution time ? How we can cache the Database connection or any other java stuff caching so that lambda will be faster and response time of the API gets reduced.

TIA

答案1

得分: 1

这是一个范围较大的问题。因此,我无法给你提供详细的答案。

但以下是一些建议(底部附有链接):

  1. 调查AWS SnapStart。这是在2022年的re:Invent上宣布的新功能,目前只适用于Java应用程序,旨在减少冷启动时间。他们甚至提供了Spring的示例。
  2. 考虑切换到RDS以便使用AWS Lambda RDS代理。这可以减少数据库延迟。
  3. Spring原生镜像。显然,您可以使用GraalVM创建“原生”Java镜像以在AWS Lambda上运行。我从未尝试过,但这可能是一个值得研究的好选择。

除此之外,我不确定还有很多事情可以做。总是有将Lambda保持“热”以避免冷启动的可能性,但那样你将失去Lambda的大部分好处,所以我不认为这是一个合适的解决方案。

如果您真的认真考虑要大量使用AWS Lambda,我还建议重新考虑是否使用Java。它是一种很好的语言,有很多优点,但不太适合AWS Lambda。我多年来找到的最佳两个选择是Go或纯JavaScript(不包括TypeScript)。显然,不同人的意见会有所不同,而像SnapStart这样的不同发展将有所帮助,但我不确定它们是否值得麻烦。

链接

  1. AWS Lambda SnapStart
  2. 在AWS Lambda中使用Amazon RDS代理
  3. 如何保持Lambda函数保持“热”
  4. Spring Boot原生GraalVM Lambda
英文:

This is a question with a large scope. Therefore, I won't be able to give you a detailed answer.

But here are a few suggestions (links at the bottom):

  1. Investigate AWS SnapStart. It is a new feature announced at re:Invent 2022 that works only with Java applications at the moment and is supposed to reduce cold starts. They even have Spring examples.
  2. Consider switching to RDS to be able to use AWS Lambda RDS Proxy. This could reduce database latency.
  3. Spring Native images. Apparently, you can use GraalVM to create "native" Java images to run on AWS Lambda. I have never tried it, but it might be a good option to investigate.

Apart from this I am not sure that there are a lot of things that you could do. There is always the possibility to keep your Lambdas "hot", so there is no cold start but then you are losing most of the benefits of Lambda, so I do not consider this to be an adequate solutions.

If you are really serious of working a lot with AWS Lambda, I would also recommend re-thinking using Java. It is a great language that has a lot of merit, but it is not as well suited to AWS Lambda. The best two options I found over the years is either Go or pure Javascript (no Typescript). Obviously, opinions will be different and different developments like SnapStart will help, but I am not sure that they are worth all the trouble.

Links

  1. https://aws.amazon.com/blogs/aws/new-accelerate-your-lambda-functions-with-lambda-snapstart/
  2. https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/
  3. https://acloudguru.com/blog/engineering/how-to-keep-your-lambda-functions-warm
  4. https://effectiveserverless.com/spring-boot-native-graalvm-lambda

huangapple
  • 本文由 发表于 2023年2月27日 15:09:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定