Search code examples
amazon-web-servicesaws-lambdatimeoutaws-api-gatewayjava-11

Aws api gateway and lambda functions timeout problem


I think I'm having a common problem but I don't know how to solve it. I've got a frontend developed in angular10 and a backend divided in several lambda functions written in java11. In the middle there's the api gateway. As database I'm using mysql in rds. Now, from the client i'm sending a get request, the api gateway calls the right lambda function but a timeout is returned. From CloudWatch I see all lambda process is successfully closed and from the report I see: REPORT Duration: 32295.26 ms Billed Duration: 32296 ms Memory Size: 256 MB Max Memory Used: 178 MB Init Duration: 378.89 ms

I understand the java cold start is heavy and the connection to the db slow, but the database is empty so I don't understand where this time is spent. Plus: if i make a second call immediately after the first the response is super fast and the client is happy.

Anyway...I don't know how to solve since we understood the api gateway timeout can not be increased. Then, I don't want to transform all my client in asynchronous way.

Any suggestion?


Solution

  • You are dealing with a Lambda cold start time issue. There are a few potential solutions to your problems.

    1. Change your Backend language.

    Java is known for having one of the slower start times with lambda since it takes a bit to start up the environment. If you use an interpreted language such as python or nodejs with your lambda, the start up times will be greatly reduced.

    1. Increasing the memory of your lambda

    The boot time can be improved by increasing the allotted memory. This will increase the price.

    1. Keeping the lambda warm

    You can be use another lambda to make calls to your first lambda every minute. This will keep your lambda in a warm state, but lambda will need to cold boot if you ever need to scale.

    1. Provisioned Concurrency

    Provisioned concurrency is a feature of lambda to keep an always running lambda instance. This can reduce your boot times since you would only need to cold boot if you need to scale up for traffic (you can always increase the total number provisioned to prevent the need for scaling). This can be expensive since you are essentially having a server running and paying for it to be on.

    Resources: Cold Starts