Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer is writing a backend AWS Lambda function that must parse a 5 MB5\text{ MB} static lookup table stored in Amazon S3. The lookup table is updated only once a week, but the Lambda function is invoked thousands of times per hour. The developer wants to optimize the function's execution time and minimize Amazon S3 data retrieval costs. Which of the following is the most efficient design pattern for the developer to implement?

  1. A
    Write the downloaded lookup table directly to the function's deployment package directory (`./`) during the first run to persist it for all future invocations.
  2. Download the lookup table to the `/tmp` space and load it into a global variable outside of the Lambda handler function, reusing the cached data for subsequent warm starts.Cevap
  3. C
    Deploy the Lambda function inside a private subnet of a VPC to download the lookup table from Amazon S3 on every invocation, without configuring a NAT Gateway or a VPC endpoint.
  4. D
    Store the lookup table in AWS Secrets Manager and initialize the AWS SDK client inside the handler function to retrieve the data on each invocation.

Cevap

Download the lookup table to the `/tmp` space and load it into a global variable outside of the Lambda handler function, reusing the cached data for subsequent warm starts.
By downloading the lookup table to the `/tmp` space and parsing it into a global variable outside the handler function, the initialization code executes only during a cold start. Subsequent invocations that reuse the warm execution context skip this download and parse phase entirely, accessing the cached data directly from memory. This provides sub-millisecond access times and minimizes S3 API charges.

Adım Adım Çözüm

1
Identify the data access pattern and constraints.
The lookup table is 5 MB5\text{ MB}, static, updated once a week, and read frequently by thousands of invocations per hour.
To design an optimal caching strategy, we must understand the data size, mutability, and invocation frequency.
2
Evaluate the caching capabilities of the AWS Lambda execution environment.
Lambda execution context reuse allows files in the `/tmp` directory and memory-bound global/static variables to persist across warm invocations.
Reusing the execution context avoids downloading the lookup table on every single function execution.
3
Apply the best practice of initializing SDK clients and reading global state outside the handler function.
Downloading the lookup table during the cold start initialization phase (outside the handler) allows subsequent warm executions to immediately read the parsed data from memory.
This minimizes latency and eliminates redundant Amazon S3 GET requests and data retrieval costs.

Anahtar Kavram

Reusing the Lambda execution context (global variables and `/tmp` space) to cache static or rarely changing data across warm invocations.
Bu soruyu puanla