Table of contents
Open Table of contents
A request needs a place to run
Imagine a checkout service that calls an AWS Lambda function to calculate shipping. Your code is a function, but it does not run directly inside the Lambda service. Lambda first gives it an execution environment: an isolated environment with the runtime, your code, configured memory, environment variables, and any extensions.
The first request that needs a new environment can take longer because Lambda must prepare it. When the function finishes, Lambda may keep that environment around. If another request arrives while it is still available, Lambda can run the function there again.
The request flow looks like this:
Request 1 -> Lambda service -> Environment A: Init -> Invoke -> Freeze
Request 2 -> Lambda service -> Environment A: Thaw -> Invoke -> Freeze
Overlapping request -> Environment B: Init -> Invoke
An environment is a reusable workspace for a function version. It is not a permanent server that your code can keep running.
The lifecycle of one environment
AWS describes a standard execution environment in a few phases:
- Init: Lambda starts extensions and the language runtime, then runs initialization code outside the handler. For example, a Python module is imported or a JavaScript client is constructed.
- Invoke: The runtime calls your handler with one event. The function and its extensions finish the invocation.
- Freeze: When the runtime and extensions report that they are ready for another event, Lambda can pause the environment.
- Thaw or shutdown: Lambda can resume the paused environment for a later request, or eventually shut it down.
A cold start is the extra setup needed when Lambda has to initialize an environment before invoking your handler. Reusing an already initialized environment skips that setup. Reuse is common, but Lambda decides when to keep an environment and when to replace it.
What reuse makes faster
Initialization code is a good place to build objects that are expensive to create and safe to reuse. For example, a function can create a database client or SDK client outside the handler:
const client = new DatabaseClient();
export async function handler(event) {
return client.query("SELECT status FROM jobs WHERE id = ?", [event.jobId]);
}
If Lambda reuses that environment, the next invocation can use the existing client instead of paying the connection setup cost again. The same idea applies to parsed configuration, certificates, and small caches of data that can be refreshed.
The important detail is where the object lives. A value created at module scope belongs to one execution environment. It is not shared with other environments, even when they run the same function version.
Function version
├── Environment A: its own client and memory
├── Environment B: a different client and memory
└── Environment C: another client and memory
When requests overlap, Lambda can use several environments at once. Each one initializes its own copy of your code and its own module-level objects.
Why reuse is only an optimization
Your function should still work correctly if every invocation gets a new environment. Reuse is best-effort; it is not durable storage, a lock service, or a way to share state between concurrent requests.
There are two common mistakes:
- Keeping request-specific data in a global variable. If an environment is reused, the next invocation may see the old value. Keep user-specific data inside the handler call or clear it before returning.
- Treating local files as durable. Files in
/tmpcan remain when Lambda freezes an environment, which makes the directory useful as a temporary cache. The files are not shared across environments and can disappear when an environment is replaced.
Connections also need care. Reusing a connection saves setup time, but a database or network may close an idle connection while Lambda is paused. Check that the connection is still usable and reconnect when needed.
Handling concurrent requests
An execution environment for a standard Lambda function handles one invocation at a time. If two requests need to run simultaneously, Lambda can provide two environments. The number of active invocations is called concurrency.
That means a module-level counter is not a reliable count of requests. Each environment has its own counter, and Lambda can create or retire environments as concurrency changes. Store shared state in a service designed to coordinate it, such as a database, cache, or queue.
Ways to reduce startup delay
A few settings and design choices affect startup time:
- Provisioned Concurrency asks Lambda to initialize a chosen number of environments ahead of demand. It can reduce cold-start delay for predictable traffic, while adding cost for the provisioned capacity.
- Memory size controls the memory available to the function and also affects the CPU allocation. More CPU can shorten initialization, so compare end-to-end latency and cost for your workload.
- Initialization work should be limited to what every invocation needs. Load optional libraries and data only when a handler path needs them.
These options change how quickly an environment is ready. They do not make environment-local memory permanent. The application should remain correct when Lambda replaces an environment.
References
- AWS Lambda Developer Guide, Understanding the Lambda execution environment lifecycle
- AWS Lambda Developer Guide, Lambda concurrency
- AWS Lambda Developer Guide, Best practices for working with AWS Lambda functions