2

I use API Gateway + Lambda + DB.

If many requests make, Too many connections Error occurs.

For testing, I don't want to increase max connections option.

Is it possible to use SQS -> Lambda -> API Gateway -> Lambda -> DB?

Or other way?

4
  • What is your error? Commented Mar 29, 2019 at 3:25
  • Too many connections from DB Commented Mar 29, 2019 at 3:28
  • "Is it possible to use SQS -> Lambda -> API Gateway" What does this means? Do you want to invoke Lambda on SQS messages? Commented Mar 29, 2019 at 7:06
  • I misunderstood, I don't need to use SQS, But want to resolve 'Too many connections' Error without changing 'Max connections' option. Commented Mar 29, 2019 at 9:07

1 Answer 1

3

First, make sure not to create a new connection in each invocation of your lambda. You should create the connection outside of the lambda handler (so it will be made at initialization time), or use connection pooling.

By doing that, the number of connections to the DB will be as about the number of concurrent invocations.

You can set the concurrency limit of the lambda to have a limit on the active connections (but it means that triggering your lambda may fail due to concurrency limit when you reach it).

You can trigger your lambda with SQS, and by that, the requests will be queued in case of reaching the concurrency limit, until previous requests are ended.

Using SQS without setting a concurrency limit for your lambda won't solve the problem, because the number of concurrent invocations (=active connections) can still get high.

I don't see any benefit by doing something like 'SQS -> Lambda -> API Gateway -> Lambda -> DB'. A possible use case will be 'API Gateway -> SQS -> Lambda'.

You can also just use 'API Gateway -> Lambda' with setting the lambda concurrency limit. In that case some calls to the API Gateway may fail, and should be retried with exponential backoff.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.