Database & Serverless : handle connection spikes ?
Hey,
I'm running a Nextjs side project on Vercel & Mongo Atlas. My ORM is Mongoose (yep, didn't learn Prisma yet, should come soon).
And in some occasions, i've been facing issues with spikes of traffic being hardly handled because i was reaching the maximum number of connections of my MongoDB database.
Here is what I've done so far to prevent this issue :
- Mongoose conf :
mongoose
.connect(MONGODB_URI, { maxIdleTimeMS: 1000 })
- Reduce number of requests at page load : each page does max 1 or 2 API requests at navigation so that if 100 people access a page, well, max 200 connections are created
- Caching : most of my requests /api
are cached using the built-in vercel cache which has been very helpful so far but can be off-topic on some pages that display live data
- ISR : turned my homepage to ISR so that content is loaded once and for all until revalidation, good win here
Despite all of this, I'm still uncomfortable not having a backup option and will be very curious to know how you have been handling this kind of issue ?
Thanks a lot for your inputs 🙏0 Replies