Topic lessonTopic: Designing event-driven processing
Lambda execution environments: initialization, reuse, and concurrency
Lambda Event Functions run in execution environments that can serve later invocations once they are free. Follow an image-processing example to see what can be reused and how slower requests change the number of invocations running at once.
Distinguish initialization from handler execution, identify data that needs durable storage, and estimate average concurrency from arrival rate and mean execution time.
A Lambda handler receives an event. This lesson covers standard on-demand Event Functions and uses fictional requests and load.
An initialized environment can serve another invocation
During initialization, Lambda starts the runtime and runs code outside the handler, among other tasks. The handler then processes each invocation. Reused environments may retain initialized objects. An environment can be shut down or reset.
- Image requests → Environment 1Handles A
- Image requests → Environment 2Handles overlapping request B separately
- Environment 1 → C arrives after AThis example reuses the free environment for C
- Image requests
- B arrives while A is running
- Environment 1
- Initializes and handles A
- Environment 2
- Handles B separately
- C arrives after A
- May reuse environment 1
Initialization can add latency when a new environment is needed. Compare first-use and reused environments with comparable inputs and memory settings. Also look for differences in time spent waiting on dependencies.
Keep durable results outside the execution environment
| Item | Example placement | On invocation |
|---|---|---|
| Image library or client | Initialize outside the handler | Check whether you can reuse it and reconnect if needed. |
| Shared settings that can be rebuilt | Cache in /tmp | Reload from the original source on a cache miss. |
| Committed result for a request ID | Durable storage | Keep it accessible from another execution environment. |
Memory and /tmp belong to a single environment. Data from a previous invocation can persist when the environment is reused. Scope variables and temporary files so request-specific state cannot leak into another invocation.
Longer processing raises concurrency even at a steady arrival rate
When arrival and completion rates are stable, average concurrency is approximately the number of invocations per second multiplied by mean execution time in seconds. In this example there is one invocation per request. At 40 per second and a mean of 0.25 seconds, average concurrency is about 10. At 0.75 seconds it rises to about 30.
| Situation | Invocations per second | Mean execution time (seconds) | Estimated average concurrency |
|---|---|---|---|
| Typical | 40 | 0.25 | 10 |
| Slow storage response | 40 | 0.75 | 30 |
| Typical duration, higher arrival rate | 80 | 0.25 | 20 |
Decide what the execution environment can tell you
Choose an option to see the result and explanation.
Check, then explain in your own words
- Trace initialization, overlapping requests A and B, and possible reuse for request C.
- Separate state that can be rebuilt from evidence that work committed.
- Estimate concurrency when execution time increases while arrivals stay steady.
Check your understanding: Request volume is steady but concurrency rose. What do you compare first?
Compare the invocation rate and execution time over the same observation window.
Each longer invocation keeps its environment busy for more time. Check initialization, handler work, and dependency latency separately.
Sources and conditions checked
The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.
Scope and conditionsInitialization, invocation, reuse and temporary data for Lambda Event Functions.
Used in this lessonAn initialized environment can serve another invocation Keep durable results outside the execution environment
Scope and conditionsIn-flight requests, separate execution environments and the concurrency estimate.
Used in this lessonLonger processing raises concurrency even at a steady arrival rate