Lambda to RDS database connection takes 6 seconds.

0

I have a Lambda function and a Postgres Serverless V2 RDS instance in us-west-2. The Lambda function frequently takes 6 seconds to establish a database connection to the RDS instance with IAM Authentication, and I have profiled the code to confirm that the time is spent on the actual connection process, not on cold starts, or creating the IAM Authentication token. Below you can see a sample from X-Ray:

Enter image description here

You can see that the connection function is taking 6 seconds. There is also a subsegment which is getting the IAM Auth token in 150ms. The rest of the time is spent connecting. This is a relatively new trend (last couple of days), because it just started tripping my Cloudwatch Alarms.

My backend code is Python 3.11, using psycopg2. After the IAM Auth token is negotiated, I immediately call psycopg2.connect and the function completes. So there is nothing else taking time, and I am fairly confident that there is something wrong with the network or the database.

How can I debug this? How can I improve this connection time?

1 Respuesta
1
Respuesta aceptada

I had 2 pending database maintenance actions. One for a postgres minor version upgrade, and another for an OS patch. I applied both of those upgrades and the connectivity latency has dropped back down to sub-second. This suggests that the db or the OS had accumulated some connection cruft that was slowing down its ability to provision new connections. I hope this helps someone else.

respondido hace un mes
profile picture
EXPERTO
revisado hace un mes
profile pictureAWS
EXPERTO
revisado hace un mes
AWS
INGENIERO DE SOPORTE
revisado hace un mes

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas