Nextjs and ServerLess Functions

hello I’m using Nextjs with serverless functions, at first my site had problems loading the functions so I had to do a mongoose.disconnect(); at the end of each API route this worked but made the site way slower as it have to connect, retrieve and disconnect each time. I deployed my site on other hosts, there mongoose will stay connected and won’t cause the function to time out is there any way to get around this problem. thanks in advance

Hey there, @alainarja :wave:

Welcome to the Netlify Forums! Thanks for reaching out.

I am just going to request a bit more information so that we can dig into this further. Can you please share:

  1. Your Netlify site name / URL
  2. Your most recent deploy log
  3. Any additional debugging steps you have taken

Thank you

-my site url is :
-my latest deploy log: 11:53:37 PM: Build ready to start11:53:39 PM: build-image version: d03efc9bc7b -
-at the start of each of my functions I do an await connectToDb(); at my first deploy sending any sort of requests to the api resulted in a request timed out so after adding mongoose.disconnect it started to work but made the site overall way slower in some cases I couldn’t find any solutions beside disconnecting the database at the end of each api call

hi there @alainarja,

thanks for sharing that detail. While it is possible to run a serverless function that could theoretically connect to something like a database, serverless functions are very short lived and you wouldn’t be able to maintain a connection to a database the way you are expecting.

This information might help:

there are databases such as fauna or airtable that might work for your purposes, which is maybe worth investigating. good luck!

okay, thanks for the suggestions