A developer designs an AWS Lambda function to process event logs. To track processed message IDs within a test execution, the developer declares a global list variable `processed_ids = []` outside the Lambda handler function. During testing, the developer observes that subsequent invocations of the function run slower, eventually timing out, and contain data from previous invocations. Which of the following explains why this issue is occurring?
- AWS Lambda reuses the execution context for subsequent invocations, causing the global list variable to persist and continuously grow in size, consuming memory and processing time.Cevap
- BAWS Lambda destroys and recreates the execution environment for every invocation, but the global variable is cached by the AWS Lambda service itself, requiring the developer to clear the cache via SDK.
- CThe Lambda function is associated with a private VPC subnet without an active NAT Gateway, preventing the global scope from being re-initialized by the AWS Lambda service API.
- DThe execution context is isolated per invocation, but the global variable undergoes memory leaks directly to the host operating system virtual machine, causing the execution to hang.
Cevap
AWS Lambda reuses the execution context for subsequent invocations, causing the global list variable to persist and continuously grow in size, consuming memory and processing time.
AWS Lambda optimizes performance by reusing the execution environment for subsequent invocations. Because the list is declared outside the handler, it is only initialized once (during the cold start). Warm invocations append items to the same list in memory, causing it to grow indefinitely, which leads to increased latency and timeouts.
Adım Adım Çözüm
Anahtar Kavram
AWS Lambda execution context reuse and its impact on global state management.
Tahmini Süre:45s