Control Lambda Compute for large traffic

0

For my RestApi, I have used lambda as backend integration. My Lambda existing concurrency is 1000. For load testing , I set parameters as 1000 concurrent users and 500 users/ second on locust. Lambda timeout is 3 min. If I made 40/50 requests in 1 minute for single user , lambda duration per API hit is 500 ms or less. But while load testing, I made 100 K requests. Out of them about 50 K requests' responses were internal server error and other throttling.

While I check lambda logs in cloudwatch , many requests were of 51000 millisecond. Due to this large lambda duration, lambda total compute increased much. Since for very low traffic, lambda duration is of 500 ms, how can I control lambda duration to 500 ms or less for large number of requests during large traffic.

asked 2 years ago236 views
1 Answer
0
Accepted Answer

The duration of each function invocation should not be affected by the number of total invocations, unless the function itself is using some other service, e.g., a database, that can't handle the load and takes longer to respond, and therefore making the total function duration higher. Could it be that that is your situation?

To analyze where the higher duration is coming from I would recommend to use X-Ray.

profile pictureAWS
EXPERT
Uri
answered 2 years ago
profile picture
EXPERT
reviewed 16 days ago
  • I have used MongoDb where max pool limit is 500 . There was db connection max pool reached issue during execution. Does limiting lambda timeout to 2 sec solve the problem since all requests are of 500 ms in normal traffic?

    For every request , new db connection is initiated. Can we use already connected database to other requests? I have used mangum python package for handler

  • You should create your connection to the database outside the function handler so that a single connection will be used for all invocations of the function in the same execution environment (an execution environment handles a single request at a time as is reused by subsequent invocations). Do not close the connection. The connection will be closed automatically by the database after the execution environment will be destroyed.

    Limiting the function to 2 seconds might be problematic if it takes MongoDB to answer more than 2 seconds.

    You should not use a connection pool inside Lambda Lambda function has no shared memory between different execution environments, so a pool will not help.

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