Troubleshooting and Optimization · Topic 4 of 5

Concurrency, Memory and Messaging Optimization

Tune Lambda reserved and provisioned concurrency, memory, connection pooling, Auto Scaling, and SQS and SNS messaging for DVA-C03 optimization questions.

  • 5 min
In a nutshell
  • Concurrency equals requests per second times duration in seconds.
  • Reserved concurrency guarantees and caps; provisioned concurrency pre-initializes to avoid cold starts and costs extra.
  • Provisioned concurrency applies to a version or alias.

What it is

Optimization means getting the same work done faster, cheaper or more reliably by changing settings and code habits, not by redesigning the system. For a developer that mostly means three things: how many requests run at once, how much memory and CPU each one gets, and how messages are moved between components.

Loadrequests per secondConcurrencycopies running at onceMemory and CPUper copyMessagingbatching and fan out

How it works

Lambda concurrency

Concurrency is the number of requests a function handles at the same time. Estimate it as average requests per second multiplied by average duration in seconds. At 100 requests per second and 500 ms, you need about 50.

By default an account has 1,000 concurrent executions per Region, shared by all functions, and you can request an increase. Each function can scale by 1,000 execution environments every 10 seconds. When demand exceeds the available concurrency, Lambda throttles requests.

SettingWhat it doesCostTypical use
Reserved concurrencyGuarantees a share of the account pool to the function and also caps it at that numberNo extra chargeProtect a critical function, or protect a downstream database from too many callers
Provisioned concurrencyKeeps a set number of execution environments initializedExtra chargeRemove cold start latency

Provisioned concurrency is configured on a published version or an alias, not on $LATEST. Application Auto Scaling can change it on a schedule or by utilization.

Concurrency outside Lambda

  • EC2 Auto Scaling: a target tracking policy keeps a metric near a target, for example average CPU at a chosen percentage or ALBRequestCountPerTarget. Step scaling reacts in steps to alarms, scheduled scaling handles known peaks, and predictive scaling forecasts load. A custom metric such as queue backlog per instance suits queue workers.
  • Thread pools: size a pool to the work. Too few threads leave capacity idle, and too many waste memory. Use a bounded pool so a slow dependency cannot create unlimited threads.
  • Connection pooling: opening a database connection per request is slow and exhausts the database. Reuse connections. In Lambda, create clients and connections outside the handler so warm invocations reuse them.

Many concurrent Lambda environments can overwhelm a relational database. Amazon RDS Proxy pools and shares database connections, and reserved concurrency can cap how many functions connect at once.

Memory and compute for Lambda

Memory ranges from 128 MB to 10,240 MB. CPU power is allocated in proportion to memory, with the equivalent of one vCPU at 1,769 MB. More memory can therefore make a CPU bound function finish faster, and because billing is memory multiplied by duration, the total cost can stay flat or even fall.

  • Find the right size by measuring. Read Max Memory Used in the REPORT line, and run the open source AWS Lambda Power Tuning tool, which tests a function at several memory sizes. AWS Compute Optimizer can also recommend a memory setting once you opt in.
  • Initialize once. Create SDK clients and load configuration outside the handler.
  • Check the architecture. Functions can run on arm64 (Graviton) or x86_64; test both for price and performance.

Optimizing messaging

Amazon SQS settings that matter:

  • Batching: SendMessageBatch, ReceiveMessage and DeleteMessageBatch handle up to 10 messages per call, which cuts the number of requests.
  • Long polling: set WaitTimeSeconds on the receive call, up to 20 seconds, to avoid empty responses and extra requests.
  • Visibility timeout: defaults to 30 seconds and goes up to 12 hours. For a Lambda event source, AWS recommends at least six times the function timeout.
  • Dead letter queue: moves messages that fail repeatedly (after the maxReceiveCount you set) so they stop blocking work.

For an SQS event source mapping, tune the batch size and batching window, return partial batch failures (ReportBatchItemFailures) so only failed messages return to the queue, and use maximum concurrency on the mapping to limit how many function copies poll the queue.

Amazon SNS fan out sends one published message to many subscribers, such as several SQS queues, so each consumer processes it independently. Subscription filter policies let each subscriber receive only matching messages, which avoids wasted invocations and code that discards messages.

How the exam asks about this

  • "A critical function is starved because other functions use the account concurrency" -> set reserved concurrency on the critical function.
  • "Cold starts hurt a latency sensitive API; reduce them" -> provisioned concurrency on an alias, scaled with Application Auto Scaling.
  • "Too many Lambda connections exhaust the RDS instance" -> use RDS Proxy, create the connection outside the handler, and cap concurrency.

What to remember for the exam

  • Concurrency equals requests per second times duration in seconds.
  • Reserved concurrency guarantees and caps; provisioned concurrency pre-initializes to avoid cold starts and costs extra.
  • Provisioned concurrency applies to a version or alias.
  • Lambda gives CPU in proportion to memory; the way to tune is to measure, for example with Power Tuning.
  • Create SDK clients and database connections outside the handler.
  • Batch SQS calls (up to 10), use long polling, and use SNS filter policies for fan out.
Watch out

Visibility timeout shorter than the function timeout causes messages to become visible again while still being processed. AWS recommends at least six times the function timeout for an SQS event source.

Common mistakes

Using provisioned concurrency to limit load. It only keeps environments warm. To cap a function, use reserved concurrency.

Raising memory by guesswork. Measure first; a higher setting may cut the bill, and an unused one wastes it.

Opening database connections inside the handler. Every invocation then pays connection setup, and a traffic spike can exhaust the database.

Check yourself

  • A function runs 200 times per second at 250 ms each. What is its concurrency?

    About 50, from 200 requests per second multiplied by 0.25 seconds.

  • Which setting protects a downstream database from too many Lambda copies?

    Reserved concurrency, which caps the function, or maximum concurrency on the SQS event source mapping.

  • How does long polling help?

    The receive call waits up to 20 seconds for messages, which reduces empty responses and the number of requests.

Topic 4 of 5 in Troubleshooting and Optimization

Test yourself on Troubleshooting and Optimization

The full AWS DVA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.