Lesson 4 of 12 AWS Lambda

Managing Concurrency, Throttling and Cold Starts

This lesson explains how Lambda scales and how to control it. Concurrency is the number of requests a function is handling at the same time, and Lambda adds execution environments to meet it, within your account's limits and any limits you set on the function.

Learning outcomes

After completing this lesson, you will be able to:

  • Estimate the concurrency a workload needs from its request rate and average duration.
  • Explain how account concurrency limits and scaling rates cause throttling.
  • Choose between reserved concurrency, provisioned concurrency and SnapStart for a given scaling or latency requirement.

Estimating concurrency

Concurrency = requests per second × average duration in seconds.

Example. 200 requests per second at 400 ms needs about 200 × 0.4 = 80 concurrent executions. If traffic doubles and a slow dependency doubles the duration, the function needs 320. Making a function faster directly reduces the concurrency it needs.

Fill in the value

Estimate the concurrency

Exam skills 4.3.1

Practise estimating the concurrency a function needs from its traffic and duration.

Log in to try this activity Interactive activities are free with an account.

Limits and throttling

Lambda limits both the total number of concurrent executions and the rate at which a function can scale. The following list describes these limits.

  • Account limit: 1,000 concurrent executions per Region by default, shared by all functions. It is a soft limit that can be raised.
  • Scaling rate: each function can add up to 1,000 concurrent executions every 10 seconds, so a sudden spike can be throttled briefly. A queue in front of the function absorbs bursts.

When a request would exceed either limit, Lambda throttles it rather than running it. What happens next depends on the invocation model. A synchronous caller, such as API Gateway or an SDK client, receives an HTTP 429 TooManyRequestsException and must retry itself; API Gateway passes the error on to its client. For asynchronous invocations, Lambda returns the throttled event to its internal queue and retries it for up to the maximum event age, which is six hours by default, so short bursts cause delay rather than lost events. For an event source mapping, Lambda slows its polling; SQS messages that were already received become visible again after the visibility timeout, and stream records wait in the shard.

The Throttles metric counts throttled requests, and ConcurrentExecutions shows how close a function or account is to its limit. The following table summarises the behaviour for each invocation model.

Invocation typeWhat happens when throttled
SynchronousThe caller receives 429 TooManyRequestsException
AsynchronousLambda retries the event automatically for up to 6 hours
Event source mappingLambda slows polling and retries; SQS messages become visible again

1,000

Concurrent executions per Region by default

1,000every 10 s

Concurrent executions each function can add

6hours

Default maximum event age for throttled asynchronous events

Sort into groups

What happens to a throttled request?

Exam skills 1.2.4 · 4.3.1

Learn how each invocation model reacts when Lambda throttles a request.

Log in to try this activity Interactive activities are free with an account.

Reserved concurrency, provisioned concurrency and SnapStart

Lambda provides three features that influence how functions scale and start. They are frequently confused because all three concern concurrency or start-up, but each addresses a different requirement.

Reserved concurrency

Reserved concurrency sets aside part of the account's concurrency for one function and, at the same time, caps the function at that number. A function with a reserved concurrency of 100 can always run 100 concurrent executions, even when other functions are busy, and can never run more. The cap is often the main reason to use it: it protects a downstream resource, such as a relational database with a limited number of connections, from being overwhelmed. Setting reserved concurrency to 0 stops all invocations, which is a quick way to disable a misbehaving function. It costs nothing, but the reserved amount is no longer available to other functions.

Provisioned concurrency

Provisioned concurrency keeps a chosen number of execution environments initialised and ready, so requests served by them have no cold start. It is configured on a published version or an alias, never on $LATEST, and is billed for as long as it is configured, whether or not it is used. Requests above the provisioned amount still run, using standard on-demand environments with normal cold starts. Application Auto Scaling can adjust provisioned concurrency on a schedule, for example higher during business hours, or by target tracking on its utilisation.

SnapStart

SnapStart reduces cold-start time rather than eliminating cold starts. When a version is published, Lambda runs the function's initialisation code once, takes a snapshot of the initialised memory and disk, and caches it. New execution environments are then restored from the snapshot instead of repeating the initialisation. It suits functions with slow initialisation, such as Java applications that load large frameworks, and is supported for Java, Python and .NET. Because every environment starts from the same snapshot, values that must be unique, such as random seeds or network connections, must be created after the restore.

The following table summarises the three features.

Reserved concurrency
PurposeGuarantees capacity for a function and caps it at that number
Extra costNo
Applies toThe whole function
Typical useProtect a downstream database; set to 0 to stop all invocations
Provisioned concurrency
PurposeKeeps environments initialised so there are no cold starts
Extra costYes
Applies toA published version or alias, never $LATEST
Typical useLatency-sensitive APIs; scale it with Application Auto Scaling
SnapStart
PurposeStarts new environments from a cached snapshot, making cold starts shorter
Extra costNone for Java; Python and .NET pay for caching and restores
Applies toPublished versions (Java, Python, .NET)
Typical useCheaper way to reduce cold-start latency

Example. Guarantee and cap a function at 100 concurrent executions, then keep 50 environments warm on the live alias:

aws lambda put-function-concurrency \
  --function-name orders-api --reserved-concurrent-executions 100

aws lambda put-provisioned-concurrency-config \
  --function-name orders-api --qualifier live --provisioned-concurrent-executions 50

The following diagram uses the settings from the example above to show how reserved and provisioned concurrency divide the account's pool, and compares the three features.

The account's 1,000 concurrent executions in a Region, with 100 reserved for orders-api, of which 50 are provisioned, and 900 unreserved for all other functions. Reserved concurrency guarantees and caps a function at no charge. Provisioned concurrency keeps environments initialised, is billed while configured and applies to a version or alias. SnapStart shortens cold starts by restoring a snapshot taken when a version is published.
A tick marks a benefit and a cross a cost or limitation. Reserved concurrency controls how many executions a function may run; provisioned concurrency and SnapStart address how quickly they start.
Scenario questions

Reserved, provisioned or SnapStart?

Exam skills 4.3.1

Tell apart the three features that control how functions scale and start.

Log in to try this activity Interactive activities are free with an account.
Worked example

One function starving the others

During a marketing campaign, a public signup function scales to 900 concurrent executions. The account limit is 1,000, so the critical payments function is throttled and customers see errors at checkout.

FixSetting
Guarantee capacity for paymentsReserved concurrency of 200 on payments
Stop signup taking everythingReserved concurrency of 300 on signup (also a cap)
Remove cold starts on checkoutProvisioned concurrency of 50 on the payments live alias
Longer termRequest a higher account concurrency quota

Reserved concurrency both reserves and caps: payments can always run 200 copies, and signup can never exceed 300. Throttled synchronous calls to signup now receive 429 errors, which the front end retries with backoff.

Check yourself

A function receives 200 requests per second, and each invocation takes 0.5 seconds. How many concurrent executions does it need?

Show answer

About 100. Concurrency is the request rate multiplied by the average duration in seconds: 200 × 0.5 = 100.

Exam Tips

Remember for the exam

  • "Eliminate cold starts" points to provisioned concurrency.
  • "Stop one function using all the account's concurrency" or "limit the load on a database" points to reserved concurrency.
  • Estimate concurrency as requests per second × average duration in seconds.
  • Scale provisioned concurrency with demand using Application Auto Scaling on the alias.
  • Synchronous callers that are throttled receive a 429 error.