Skip to the page

AWS for developers · mid

Lambda and SQS under load

Each Lambda environment handles one request and has its own connections, and SQS delivers at least once with a visibility timeout. Both break in quiet or busy periods unless you cap concurrency and write idempotent handlers.

The notes

What you must know

  • A 504 from API Gateway after 29 seconds is its integration timeout for REST APIs, and the Lambda may still be running. Look at the CloudWatch REPORT and INIT_REPORT lines and an X-Ray trace: Init Duration, handler duration, max memory used.
  • Cold start: Lambda creates an environment, loads the code, starts the runtime (for example the JVM), runs init code outside the handler (Spring context, Hibernate, connection pool, fetching secrets), then runs the handler. A quiet night means no warm environments. Low memory also means low CPU, so Spring Boot on 512 MB is slow.
  • Fixes: more memory, lazy loading, a lighter framework, SnapStart for Java where supported, provisioned concurrency for the busy hours, or a container service for steady traffic.
  • Each Lambda environment handles one request at a time and has its own connections. Hundreds of environments times a pool of 5 is thousands of connections, past the database max_connections.
  • Right now: cap the function with reserved concurrency, or maximum concurrency on the SQS event source mapping. Messages wait in the queue, which is what the queue is for. In code: create the client or pool outside the handler, pool size 1, never a pool per invocation. RDS Proxy pools and shares connections.
  • SQS consumer: deleting a message before processing loses it if the worker crashes. Delete after success. The default visibility timeout is 30 seconds, so a 90-second job makes the message visible again and another worker repeats it. Set the timeout above the longest processing time, or extend it with ChangeMessageVisibility.
  • Standard queues deliver at least once, so make the handler idempotent: record the work for the order id with a unique constraint and skip repeats.
  • Use long polling (WaitTimeSeconds up to 20) to avoid empty responses that cost money, and a dead-letter queue with a maxReceiveCount for poison messages.

The red pen

Common traps: mistake → fix

  • Mistake: Lambda scales without any limit on my side

    Fix: It scales until the database or an API breaks. Set concurrency limits.

  • Mistake: SQS gives each message exactly once

    Fix: Standard queues are at least once. Handlers must be idempotent.

  • Mistake: Delete the message first so it is never processed twice

    Fix: If the worker fails, the message is lost.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why does a Lambda time out only after a quiet night?
  2. Why can Lambda scaling take down the database, and how do you stop it?
  3. How do you configure the visibility timeout and why?
  4. Review this SQS consumer and list the bugs.
  5. How would you make an email-sending worker safe to run twice for the same message?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview