Question

Difficulty: MediumApplication Caching and Session State Management

A developer is writing an AWS Lambda function that retrieves database configurations from an external database on every invocation. The database queries are slow, causing high latency and occasionally leading to function timeouts. Which approach should the developer use to optimize the function's performance by caching the configurations across invocations?

  1. Initialize the database connection and retrieve the configurations outside the Lambda handler function, storing them in global or static variables to reuse them across subsequent warm start invocations.Answer
  2. B
    Declare the database connection and configuration variables inside the Lambda handler function, and increase the Lambda function timeout to 15 minutes to allow enough time for queries.
  3. C
    Store the database configurations in AWS Systems Manager Parameter Store, and configure Parameter Store to automatically rotate the database credentials every 30 days.
  4. D
    Store the configurations in an Amazon SQS queue, and set the queue's visibility timeout to be shorter than the Lambda function's timeout so that the configuration message is immediately recycled.

Answer

Initialize the database connection and retrieve the configurations outside the Lambda handler function, storing them in global or static variables to reuse them across subsequent warm start invocations.
Declaring database connections and configuration variables in the global scope (outside the Lambda handler function) utilizes execution context reuse. During warm starts, Lambda reuses the existing container environment, allowing the application to bypass redundant initialization and query overhead by reading from the globally persisted variables.

Step-by-Step Solution

1
Analyze how AWS Lambda handles execution context reuse.
AWS Lambda preserves the execution context, including global variables and initialized SDK clients, for subsequent invocations on the same container (warm starts).
To identify where variables must be declared to persist across executions.
2
Determine where to place the configuration retrieval code.
Placing the connection initialization and retrieval code outside the handler method (in the global scope) ensures it runs only during cold starts.
This implements local caching of the configurations, preventing redundant database queries on warm invocations.

Key Concept

AWS Lambda Execution Context Reuse
Rate this question