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?
- 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
- BGlobal 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.
- CThe 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.
- DThe 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
Anahtar Kavram
Lambda execution context reuse and database connection pool management
Tahmini Süre:2m 0s