Java 2.x - High execution time of lambda

0

My lambda takes a long time to do the first operation with an aws client. For example I am performing a query on the index through the dynamoDB client and the first execution takes 2 seconds, while in subsequent executions on the same lambda environment the query is executed in 100 milliseconds. The DynamoDB client is inizialized outside of the Lambda handler method. Why my first execution takes so long?

已提问 1 年前257 查看次数
1 回答
1

First execution will always take longer than subsequent requests. Mostly it has to do with the client initialization, regardless if its outside of the Lambda handler the first request must initialize the client. Moreover, DynamoDB makes use of internal metadata caching, which also needs to be populated and can add a couple of extra hops to your first request which increases your overall latency slightly.

You can perhaps use the recently released Common Runtime (CRT) HTTP client for the SDK which boasts the following improvements:

  • faster SDK startup time
  • smaller memory footprint
  • reduced latency time
  • connection health management
  • DNS load balancing
profile pictureAWS
专家
已回答 1 年前
  • You might also consider SnapStart but it will require using a different Java version.

  • Thank you Brettski-AWS, but I am using Java 8.

  • Thank you Leeroy Hannigan, but i need a synchronous client.

  • Using CRT is only a suggestion, the rest of my answer still stands. You cannot avoid client initialization unless you initialize it in advance. Likewise, you cannot avoid DynamoDB metadata cache misses if you have not been sending requests frequently.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则