garageScript / c0d3-app

C0D3 - Become a Software Engineer the Hard Way
https://www.c0d3.com
MIT License
102 stars 69 forks source link

Vercel serverless functions causing database connection exhaustion #2639

Closed flacial closed 1 year ago

flacial commented 1 year ago

Description

Based on the analysis made by @JasirZaeem, we noticed we have been experiencing issues with our Vercel serverless function exhausting the database connections and causing the database to reach the maximum connections limit. This has resulted in errors and downtime for our application.

Upon further investigation, we have identified that the issue is caused by our Vercel serverless function making too many connections to the database in a short period of time. This is likely due to the high volume of requests being sent to the function, which is causing it to create a new connection for each request.

We believe that implementing a connection pooling solution, such as pgbouncer, could help resolve this issue. Connection pooling allows multiple connections to be grouped into a single "pool" and shared among multiple clients. This can help reduce the number of connections made to the database, as well as improve the performance and scalability of our application.

Steps to reproduce

  1. Send a high volume of requests to the Vercel serverless function
  2. Observe the database reaching the maximum connections limit and errors being thrown

Expected behavior

The Vercel serverless function should be able to handle the high volume of requests without exhausting the database connections and causing errors.

Actual behavior

The Vercel serverless function is causing the database to reach the maximum connections limit and throwing errors.

Solution

Implementing a connection pooling solution, such as pgbouncer, could help resolve this issue by reducing the number of connections made to the database and improving the performance and scalability of our application.

Resources

JasirZaeem commented 1 year ago

Pgbouncer has been setup on server and the PR has been merged. To tell the app to use the databse through pgbouncer add USE_POOLED_DB as an environment variable with the value 1 at vercel and redeploy it whenever needed.