# Nextjs and ServerLess Functions

**URL:** <https://answers.netlify.com/t/nextjs-and-serverless-functions/37388>\
**Category:** Support\
**Tags:** nextjs\
**Created:** [May 9, 2021, 1:40pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388 "2021-05-09T13:40:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alainarja](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alainarja/32/25197_2.png) [@alainarja](https://answers.netlify.com/u/alainarja)\
**Post date:** [May 9, 2021, 1:40pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388/1 "2021-05-09T13:40:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hillary](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hillary/32/21132_2.png) [@hillary](https://answers.netlify.com/u/hillary)\
**Post date:** [May 10, 2021, 8:10pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388/2 "2021-05-10T20:10:39Z")

</div>

Hey there, @alainarja 👋

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

---

<div class="post-metadata">

**Author:** ![alainarja](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alainarja/32/25197_2.png) [@alainarja](https://answers.netlify.com/u/alainarja)\
**Post date:** [May 11, 2021, 4:30pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388/3 "2021-05-11T16:30:47Z")

</div>

Hello,  
-my site url is : alainarja.netlify.app  
-my latest deploy log: [11:53:37 PM: Build ready to start11:53:39 PM: build-image version: d03efc9bc7b - Pastebin.com](https://pastebin.com/GjJc1xxk)  
-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

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [May 11, 2021, 4:52pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388/4 "2021-05-11T16:52:49Z")

</div>

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:

> [@\[Support Guide\] Can I run a web server, HTTP listener, and/or database at Netlify?](https://answers.netlify.com/t/support-guide-can-i-run-a-web-server-http-listener-and-or-database-at-netlify/3078):
>
> Last reviewed: July 2025 How do I run a server/database at Netlify? We often get questions about how to run a specific backend server or database at Netlify. Examples of these types of questions include: How to I get my node HTTP listener to respond to requests? How do I start Mongo/MySQL/\<insert-some-database-here\> running on the server? How can I SSH into my website? We are here today to answer these questions - and the answer is: you can’t! hushed_faceWhat do you mean, I cannot run a …

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

---

<div class="post-metadata">

**Author:** ![alainarja](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/alainarja/32/25197_2.png) [@alainarja](https://answers.netlify.com/u/alainarja)\
**Post date:** [May 11, 2021, 5:07pm UTC](https://answers.netlify.com/t/nextjs-and-serverless-functions/37388/5 "2021-05-11T17:07:38Z")

</div>

okay, thanks for the suggestions
