Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 70 ยท Serverless and messaging

Lambda Concurrency, Cold Starts and Limits

Understand how Lambda scales: concurrency, reserved and provisioned concurrency, cold starts, memory and CPU, timeouts and cost math, with runnable simulators.

Lambda scales by running more copies of your function at the same time. You never choose a number of servers: AWS creates as many execution environments as the traffic needs, up to a limit. That sounds effortless, and mostly it is, but three things still deserve your attention: how many copies can run at once (concurrency), how long a brand-new copy takes to become ready (the cold start), and how memory settings change both speed and price.

This lesson builds on Lambda Functions. You will simulate concurrency and cold starts, then work through the pricing formula.

Lambda cannot run on this site, and there is no AWS account here. The simulators below are simplified Python models with made-up timings: they show the logic, not real latency. The AWS CLI snippets are read-only. Prices change by region and over time, so check the official pricing page before relying on any number.

What concurrency means

An execution environment is a small sandbox that holds your code and the runtime. It handles one request at a time. If a second request arrives while the first is still running, Lambda needs a second environment. So:

concurrency = requests per second ร— average duration in seconds (this is Little's law)

A function that receives 100 requests per second, each taking 200 ms, needs about 20 environments busy at once. If the duration grows to 2 seconds (a slow database, say), the same traffic needs 200. That is why slow downstream calls can suddenly exhaust capacity.

Limits you should know:

  • Each AWS account has a regional concurrency quota, 1,000 by default, shared by all functions. You can ask for more.
  • A single function can add up to 1,000 new environments every 10 seconds, so a huge sudden spike is absorbed in steps, not instantly.
  • When the limit is reached, further synchronous requests are throttled (HTTP 429). Asynchronous events are retried for up to six hours before being dropped, and queue pollers back off.

Cold starts

When a request arrives and no idle environment exists, Lambda must create one. That is a cold start. It downloads your code, starts the runtime and runs your initialization code (everything outside the handler). Only then can the handler run. Later requests that find an idle environment are warm starts and skip all of this. An idle environment is kept for a while (minutes to hours, not something you control) and then recycled.

flowchart LR
    A["Request arrives"] --> B{"Idle warm<br/>environment?"}
    B -- Yes --> W["Run handler<br/>(warm start)"]
    B -- No --> C["Create environment<br/>download code, start runtime"]
    C --> D["Run init code"]
    D --> W2["Run handler<br/>(cold start)"]

A cold start adds environment creation and initialization time before the handler runs.

What makes cold starts longer: big packages, heavy frameworks, JVM or .NET class loading, many imports, and work at init time such as opening connections or reading large files. What shortens them: small deployment packages, lazy imports, arm64, languages with fast startup (Python, Node.js, Go, Rust), and SnapStart for supported runtimes, which resumes from a saved snapshot of an initialized environment. For many applications cold starts affect only a small share of requests, and a quiet function with one user a minute may see them often.

Concurrency can be tuned in two opposite ways.

  • Reserved concurrency sets aside part of your account's quota for one function. It both guarantees that capacity to the function and caps it at that number. It costs nothing extra. Use it to protect a critical function from noisy neighbors, or to stop a function from overwhelming a database.
  • Provisioned concurrency keeps a chosen number of environments initialized and ready, so those requests never see a cold start. You pay for the provisioned time whether or not it is used. Use it for latency-sensitive endpoints with steady traffic, often with a schedule.

Simulating concurrency and cold starts

This simulator receives arrival times (in milliseconds) and models environments that each serve one request at a time. A cold start costs init_ms extra. Reserved concurrency is a cap that throttles extra requests; provisioned concurrency pre-creates warm environments.

Python Concurrency, cold starts, reserved and provisioned capacity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By default the first five requests are cold, the second burst reuses those environments, and the third burst of eight needs three more cold starts. Provisioned concurrency of eight removes every cold start for this traffic, at the price of paying for eight idle-ready environments. The reserved cap of six avoids overload but throttles two requests of the last burst, because eight cannot run at once. Both settings are real levers. They solve different problems.

Memory, CPU and timeouts

You do not pick a CPU size. You pick memory (128 MB to 10,240 MB) and Lambda allocates CPU in proportion, reaching the equivalent of one full vCPU at about 1,769 MB. A function that is CPU-bound runs faster with more memory, often so much faster that the total cost stays the same or even drops. A function that mostly waits for the network gains nothing from extra CPU.

The timeout defaults to 3 seconds and can be raised to a maximum of 15 minutes. Set it to a value that fits your real workload, a little above the normal duration. A function that hangs on a dead dependency should fail early rather than burn money for 15 minutes. Other limits to remember: 6 MB request and response size for synchronous invocations, and /tmp scratch space from 512 MB up to 10 GB.

Terminal
# Set memory, timeout and capacity (read-only: needs an AWS account)
aws lambda update-function-configuration \
  --function-name resize-image --memory-size 1024 --timeout 30

aws lambda put-function-concurrency \
  --function-name resize-image --reserved-concurrent-executions 50

aws lambda put-provisioned-concurrency-config \
  --function-name resize-image --qualifier live \
  --provisioned-concurrent-executions 5

Pricing math

Lambda bills for requests and duration. Duration is measured in 1 ms steps and multiplied by the memory you configured, which gives GB-seconds. For x86 functions in the US East (N. Virginia) region, the list prices are about $0.20 per million requests and $0.0000166667 per GB-second; arm64 is around 20% cheaper per GB-second. There is a monthly free tier of 1 million requests and 400,000 GB-seconds. Cold start initialization time can also be billed, depending on the runtime and packaging, so check the current pricing page for the rules in your case.

The next example uses a simplified model: a CPU-bound job needs 200 ms on one full vCPU, and with less memory the CPU share shrinks in proportion.

Python Memory versus duration versus cost

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that between 128 MB and 1,769 MB the cost per million calls hardly changes: less memory means a longer run, more memory a shorter one, and the product stays almost the same. Past the point of one vCPU, extra memory only adds cost for a single-threaded job. That is why measuring your function at several sizes (or using a tuning tool) beats guessing. The monthly lines show the free tier at work: half a million requests cost nothing at all.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A function receives 50 requests per second and each request takes 400 ms. About how many execution environments are busy at once?

    Concurrency is requests per second times duration in seconds, so 50 x 0.4 = 20.
  2. 2. What does reserved concurrency do?

    Reserved concurrency is both a guarantee and a cap. Provisioned concurrency is the one that pre-warms environments and costs money.
  3. 3. Which is NOT a typical cause of a long cold start?

    The timeout is only a limit on how long a request may run. It does not change how long environment creation and initialization take.
  4. 4. How does Lambda decide how much CPU a function gets?

    You configure memory, and CPU scales with it, reaching about one vCPU near 1,769 MB.
  5. 5. Why might doubling a CPU-bound function's memory leave its monthly cost almost unchanged?

    Billing multiplies memory by duration. If more memory shortens a CPU-bound run in proportion, the product stays about the same.

Summary

  • Each execution environment handles one request at a time; concurrency is requests per second times duration in seconds, limited by a regional quota.
  • A cold start creates and initializes a new environment. Reduce it with small packages, lazy imports, arm64, SnapStart or provisioned concurrency.
  • Reserved concurrency guarantees and caps a function for free; provisioned concurrency keeps environments warm and costs money.
  • Memory (128 to 10,240 MB) controls CPU too, and the timeout (default 3 s, max 15 min) should fit your real workload.
  • Cost is requests plus GB-seconds; measure your function at several memory sizes instead of guessing.

Next: API Gateway and Function URLs