Soru

Zorluk: ZorDebugging Lambda Execution and Configuration Issues

A telemetry ingestion system uses an AWS Lambda function to write incoming data to an Amazon RDS PostgreSQL database instance. To optimize performance and reduce database connection overhead, the developer initializes a single database connection pool outside the Lambda handler function (in the global scope). During testing, the function runs successfully under continuous load. However, during periods of low traffic, subsequent invocations of the Lambda function fail, resulting in a `Task timed out after 15.02 seconds` error. CloudWatch logs indicate that the function hangs at the database query execution line. Which of the following is the most likely cause of this issue, and how should it be resolved?

  1. The database closed the idle connections during the period of inactivity, leaving stale connections in the pool. The developer should configure the connection pool to validate connection liveness before executing queries, or implement error handling to re-establish the connections.Cevap
  2. B
    Global variables are automatically cleared by the Lambda runtime between invocations when the execution environment goes idle. The developer should move the connection pool initialization inside the handler function and increase the function's timeout to allow for connection recreation on every invocation.
  3. C
    The Lambda function is deployed in a private VPC subnet and requires a NAT Gateway to route traffic to the RDS PostgreSQL database. The developer should deploy a NAT Gateway in a public subnet and update the private subnet's route table to point database traffic to the NAT Gateway.
  4. D
    The execution context is recycled after every invocation, meaning the connection pool is garbage collected. The developer should set the Lambda function's reserved concurrency to 1 to ensure that the same execution context is reused indefinitely without timing out.

Cevap

The database closed the idle connections during the period of inactivity, leaving stale connections in the pool. The developer should configure the connection pool to validate connection liveness before executing queries, or implement error handling to re-establish the connections.
AWS Lambda reuses execution environments for subsequent warm starts, which preserves any connection pools defined in the global scope. During low-traffic periods, the database server closes these idle connections due to its configured timeout settings. The Lambda function is unaware of this and attempts to reuse the stale connection, causing the database driver to hang indefinitely until the Lambda function's timeout is reached. The appropriate fix is to validate connection liveness before borrowing a connection from the pool, or to catch the connection error and re-establish the connection.

Adım Adım Çözüm

1
Analyze the symptom and error behavior
The Lambda function times out only during low traffic, and logs show it hangs at the database query execution step.
This indicates that network connectivity to the database is functional, but the connection being used is unresponsive.
2
Evaluate the initialization context
The connection pool is initialized in the global scope (outside the handler function).
The Lambda runtime maintains the global scope state across warm starts to optimize subsequent executions through context reuse.
3
Identify the root cause of connection failure
During periods of low traffic, connections remain idle longer than the database's idle timeout threshold, causing the database to silently close them.
Because the Lambda execution environment remains warm, the local pool still holds references to these terminated sockets, causing the application to hang when attempting to write data.
4
Select the correct mitigation strategy
Configure connection liveness testing (e.g., test-on-borrow) or handle connection drops by clearing and rebuilding the pool dynamically.
This guarantees that any stale connection is discarded and replaced before a query is executed, preventing the application from hanging.

Anahtar Kavram

Lambda execution context reuse and database connection pool management
Tahmini Süre:2m 0s
Bu soruyu puanla