Sub topic 10
Concurrency limits, reserved and provisioned concurrency
This chapter assumes you know how a handler runs, how the execution environment is reused, and the difference between synchronous and asynchronous invocation. It also assumes you can read a SAM template.
What concurrency means in Lambda
Concurrency is the number of execution environments serving requests at the same moment. Lambda does not queue requests onto a running environment. Each environment handles one request at a time, so ten overlapping requests need ten environments.
A useful estimate is:
concurrency ≈ requests per second × average duration in secondsA function that receives 50 requests per second and runs for 200 ms needs about 10 concurrent environments. If duration doubles, so does the concurrency you need.
The account limit and burst scaling
Each AWS Region in an account has a total concurrency quota. The default is commonly 1,000, and you can ask for an increase. Every function in the Region shares this pool.
Lambda also limits how fast a function can scale up. It adds environments in bursts rather than all at once. If demand grows faster than the scaling rate, or you hit the account quota, Lambda throttles the extra requests.
What the caller sees depends on the invocation type:
| Invocation type | Behavior when throttled |
|---|---|
| Synchronous | The caller gets a 429 `TooManyRequestsException` and must retry. |
| Asynchronous | Lambda retries internally for up to six hours, then drops the event or sends it to a failure destination. |
| Poll-based (SQS) | The event source mapping slows polling, and messages return to the queue. |
Check the current quotas in the Lambda documentation before you plan capacity, because the numbers change.
Reserved concurrency
Reserved concurrency sets aside part of the account pool for one function. It works as both a guarantee and a cap.
- Guarantee: other functions cannot use the reserved amount, so this function is not starved by a noisy neighbor.
- Cap: the function can never exceed the reserved number.
Setting it to 0 stops all invocations, which is a quick way to switch a function off. Reserved concurrency has no extra charge. Because it removes capacity from the shared pool, AWS requires you to leave at least 100 unreserved for the other functions.
A common use is protecting a downstream database. If a function writes to a database that tolerates 20 connections, reserve 20 and the database never sees more.
Provisioned concurrency
Provisioned concurrency keeps a set number of environments initialized and ready before requests arrive. It removes cold starts for traffic up to that number. It is billed for the time it is configured, whether or not it is used. You apply it to a published version or an alias, not to $LATEST.
If traffic exceeds the provisioned number, Lambda serves the overflow with normal on-demand environments, and those can cold start.
This SAM snippet configures both settings on a function with an alias:
Resources:
OrdersFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
AutoPublishAlias: live
ReservedConcurrentExecutions: 50
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: 10Here the function can run at most 50 environments, and 10 of them stay warm on the live alias. Provisioned concurrency counts against the reserved amount, so keep it at or below the reservation.
Choosing between them
| Goal | Use |
|---|---|
| Protect a downstream system from overload | Reserved concurrency as a cap |
| Guarantee a critical function gets capacity | Reserved concurrency |
| Avoid cold starts for latency-sensitive traffic | Provisioned concurrency |
| Handle unpredictable spikes cheaply | Neither, accept some cold starts |
Watching concurrency
CloudWatch publishes ConcurrentExecutions, Throttles, and ProvisionedConcurrencyUtilization. Alarm on Throttles greater than zero. If provisioned utilization stays near 100%, raise the setting. If it stays low, you are paying for idle environments.
Exercise
A function averages 80 requests per second and each invocation takes 250 ms. It writes to a database that allows 25 connections. What is the approximate concurrency need, and what setting would you use to protect the database?
Show answer
The need is about 80 × 0.25 = 20 concurrent environments. Set reserved concurrency to 25 (or slightly lower) so the function can never open more connections than the database allows. Note that at peaks above 25 concurrent requests, synchronous callers will be throttled, so they must retry.