Sub topic 10
Concurrency limits, reserved and provisioned concurrency
This chapter assumes you can write and deploy a Lambda function, and that you know the execution environment lifecycle and the difference between synchronous, asynchronous and poll-based invocation from the earlier chapters. It also assumes you know what a cold start is. Here you will learn how Lambda decides how many copies of your function run at once. You will also learn how to cap that number, protect a function's share of it, and keep environments warm.
What concurrency means
Concurrency is the number of requests a function is handling at the same instant. Each in-flight request occupies one execution environment. A second request that arrives while the first is still running cannot share that environment, so Lambda starts another one. When a request finishes, its environment becomes idle and can serve the next request.
You can estimate the concurrency a workload needs with a simple formula:
concurrency = requests per second × average duration in secondsFor example, 200 requests per second at 0.5 seconds each needs about 100 environments running at once. If the duration grows to 2 seconds, for instance because a downstream database slows down, the same traffic needs about 400. Concurrency therefore depends on latency as well as traffic.
import math
def required_concurrency(requests_per_second: float, avg_duration_seconds: float) -> int:
"""Rough number of simultaneous environments a workload needs."""
return math.ceil(requests_per_second * avg_duration_seconds)
print(required_concurrency(200, 0.5)) # 100
print(required_concurrency(200, 2.0)) # 400Treat the result as a floor. Real traffic is bursty, so peak concurrency is higher than the average.
The account limit and the scaling rate
Two separate limits apply, and people often confuse them.
- Regional concurrency quota. All functions in an account and Region share one pool of concurrent executions. The default is 1,000, though newly created accounts can start lower. You can request an increase through Service Quotas.
- Scaling rate. Each function can add at most 1,000 execution environments every 10 seconds. A spike larger than that is absorbed gradually, even if the account quota has room.
When demand exceeds either limit, Lambda throttles the extra requests. A throttle means Lambda rejects the invocation before running your code. It is not a function error, so it appears under the Throttles metric and not under Errors.
How a throttle reaches the caller depends on the invocation type:
| Invocation type | What happens on throttle |
|---|---|
| Synchronous (API Gateway, SDK `RequestResponse`) | The caller receives HTTP 429 `TooManyRequestsException` and must retry. |
| Asynchronous (S3, SNS, EventBridge, SDK `Event`) | Lambda keeps the event in its internal queue and retries with backoff for up to 6 hours. |
| Poll-based (SQS, Kinesis, DynamoDB Streams) | The poller backs off. SQS messages return to the queue after the visibility timeout. |
Reserved concurrency
Reserved concurrency sets aside part of the account pool for one function. It does two things at once:
- It guarantees that the function can always reach that many concurrent executions, because no other function can use that share.
- It caps the function at that number. Requests beyond it are throttled.
It costs nothing extra. Lambda requires the account to keep at least 100 unreserved executions, so with the default quota you can reserve at most 900 in total.
Reserved concurrency is useful in three situations:
- Protecting a downstream system. A function that opens database connections can be capped so it cannot exhaust the database. Set the cap from the database's connection limit.
- Protecting other functions. A runaway function with no cap can consume the entire pool and throttle everything else in the account.
- Emergency stop. Setting reserved concurrency to 0 throttles every invocation. This acts as a kill switch while you investigate a bad deploy or a retry storm.
Set it with the CLI:
aws lambda put-function-concurrency \
--function-name orders-function \
--reserved-concurrent-executions 50
aws lambda get-function-concurrency --function-name orders-function
aws lambda delete-function-concurrency --function-name orders-functionReserved concurrency with SQS
An SQS event source mapping starts with a few concurrent batches and scales up as the queue grows. If the function's reserved concurrency is lower than the number of pollers, some batches are throttled. The messages become visible again and their receive count rises. With a redrive policy, a message that is throttled repeatedly can reach the dead-letter queue without ever being processed.
Two rules follow from this:
- Reserve at least 5 concurrent executions for a function that reads from SQS.
- To limit how hard a queue drives a function, prefer the maximum concurrency setting on the event source mapping over a low reserved concurrency. It limits the poller itself, so messages are not received and then throttled.
Provisioned concurrency
Provisioned concurrency keeps a fixed number of execution environments initialized and ready before any request arrives. The init phase (runtime start, imports, code outside the handler) runs ahead of time, so requests served by these environments skip the cold start.
Key facts:
- It applies to a published version or an alias, not to
$LATEST. - It is billed for the amount configured and the time it is enabled, whether or not requests arrive. Invocations served by it are billed at a lower duration rate than on-demand ones.
- It counts against the function's reserved concurrency, if you set one, and against the account quota. You cannot provision more than the reserved concurrency.
- Requests beyond the provisioned amount spill over to ordinary on-demand environments, which can have cold starts. This is not a throttle, as long as the account and reserved limits are not reached.
Configure it for an alias named live:
aws lambda put-provisioned-concurrency-config \
--function-name orders-function \
--qualifier live \
--provisioned-concurrent-executions 10
aws lambda get-provisioned-concurrency-config \
--function-name orders-function \
--qualifier liveThe status moves from IN_PROGRESS to READY once Lambda has initialized the environments. Until then, traffic is served on demand.
Scheduled and target-tracking scaling
A constant provisioned amount wastes money outside peak hours. Application Auto Scaling can adjust it in two ways:
- Scheduled actions change the value at set times, for example more capacity on weekday mornings.
- Target tracking keeps
ProvisionedConcurrencyUtilizationnear a target such as 70%, adding and removing environments as load changes.
Scaling up takes time because new environments must initialize, so target tracking follows demand with a delay. For sharp, predictable peaks, a scheduled action that raises capacity before the peak works better.
How a request is routed
When a request arrives for an alias that has provisioned concurrency, Lambda follows this order.
Declaring it in SAM
In AWS SAM, ReservedConcurrentExecutions sets the cap. Provisioned concurrency needs an alias, which AutoPublishAlias creates and points to each new version.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
OrdersFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
MemorySize: 256
Timeout: 10
AutoPublishAlias: live
ReservedConcurrentExecutions: 50
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 10
Events:
CreateOrder:
Type: HttpApi
Properties:
Path: /orders
Method: postBecause ProvisionedConcurrentExecutions (10) is below ReservedConcurrentExecutions (50), the template is valid. API events created by SAM point at the live alias, so they use the provisioned environments.
Monitoring concurrency
These CloudWatch metrics show whether your settings fit the traffic:
| Metric | What it tells you |
|---|---|
| `ConcurrentExecutions` | Environments in use now. Compare its maximum to your caps. |
| `UnreservedConcurrentExecutions` | Account-wide concurrency not covered by any reservation. |
| `Throttles` | Invocations rejected for lack of capacity. Any sustained value needs attention. |
| `ProvisionedConcurrencyUtilization` | Share of provisioned environments in use. Near 100% means spillover is likely, and low values mean wasted spend. |
| `ProvisionedConcurrencySpilloverInvocations` | Requests that exceeded provisioned capacity and ran on demand. |
Choosing between them
- Use nothing if the default pool covers your peak and occasional cold starts are acceptable.
- Use reserved concurrency to isolate functions from each other, protect a downstream system, or stop a function quickly.
- Use provisioned concurrency only when cold-start latency affects users and optimizing init time (smaller packages, lazy imports) is not enough. Size it from measured peak concurrency, and scale it with a schedule or target tracking.
- Raise the account quota if the sum of your needs approaches the Regional limit.
Common mistakes
- Reserving everything. Reserving most of the pool leaves unreserved functions with very little capacity. Keep the 100-execution minimum in mind and leave headroom.
- Provisioning
$LATEST. This is not allowed. Use a version or alias. - Assuming provisioned concurrency raises the cap. It does not. Your reserved and account limits still apply.
- Calling a version directly. Provisioned concurrency applies to the alias or version you configured. Invoking
$LATESTor the unqualified function name bypasses it. - Reading throttles as bugs. A throttle returns before your code runs, so logs from the function show nothing. Look at the
Throttlesmetric.
Exercise
A function handles 150 requests per second with an average duration of 0.4 seconds. It has reserved concurrency of 50. Traffic doubles at peak, and users see HTTP 429 errors. What is the concurrency need before and at peak, why do the errors occur, and what would you change?
Show answer
Normal need is 150 × 0.4 = 60 concurrent executions. At peak it is 300 × 0.4 = 120. Even the normal load is above the reserved cap of 50, and at peak the cap is more than twice too small, so Lambda throttles the surplus synchronous requests and returns 429. Raise the reserved concurrency above the peak need (for example 150, if the account quota and the 100-execution unreserved minimum allow it), or remove the reservation if isolation is not needed. If a downstream system limits you to 50, keep the cap and have callers retry or queue the work instead.
Exercise
You configure provisioned concurrency of 20 on an alias, and ProvisionedConcurrencySpilloverInvocations is steadily above zero while ProvisionedConcurrencyUtilization sits near 100%. What does this mean, and what are your options?
Show answer
Demand regularly exceeds 20 concurrent requests, so the extra requests run on on-demand environments and can have cold starts. Options are to raise the provisioned amount (within reserved and account limits), to add target-tracking or scheduled scaling for the peak period, or to accept the spillover if the occasional cold starts are tolerable. Provisioned concurrency is billed even when idle, so size it from measured peak load and not from a guess.