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 Answer
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
EXPERT
answered a year ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions