Hi, I’m new to serverless
I’m using Netlify Serverless Functions(which uses AWS Lambda) and my database is MongoDB. I was using Heroku express server until I was getting a lot of traffic and things are very slow so I tried to switch to serverless.
I have over 8500 users and my app gives users alerts for restocks. So regularly there isn’t as many traffic but during restock time, it spikes a lot. Some users still use the app version that uses Heroku, some use the new version that use serverless
My issue is that I have cached my connection (code attached, it seems to reuse sometimes based on the logs) but I’m still reaching MongoDB limit of 500 connections (it’s a free database tier). Without high-traffic hours, the connections vary between 250-450 connections but it doesn’t go back to lower than 100. I understand I have people sign up and use the app as well but I wonder if this is a normal behavior. Netlify Serverless function told me yesterday I had over 70,000 requests. I was surprised because of that.
So I guess my question is
- Is serverless still the right choice if I have a small budget. I’m just a dev and I paid everything out of my pocket. I’ve attempted to upgrade MongoDB to the next tier which allows 1500 max connections but I still don’t know if my code is right/if I’m doing anything wrong that makes it end up being 500 connections. and will 1500 is enough for thousands of users.
Connection code
const mongoose = require("mongoose");
let cachedMongooseDb = null;
async function connectToDB() {
const uri =
process.env.NODE_ENV === "production"
? `mongodb+srv://${process.env.name}:${process.env.pw}@<dbname>/<collection>?retryWrites=true&w=majority&maxIdleTimeMS=300000`
: "mongodb://127.0.0.1:27017/squishmallows?maxIdleTimeMS=300000";
if (cachedMongooseDb && cachedMongooseDb._readyState === 1) {
console.log("db cached");
return cachedMongooseDb;
} else {
console.log("----db NOT cached");
const connect = await mongoose.connect(uri, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
cachedMongooseDb = connect.connection;
console.log("--Connected to the db--");
return cachedMongooseDb;
}
}
(whether I have maxIdleTimeMS=300000 or not, it still behaved the same way where it generates over 500 connections)
Hopefully, I could have some guidance. I’ve not been able to find an answer for my question, lots of articles said serverless is costly if you have a larger application. However I don’t think my application that larger and I know other bigger companies use serverless (I know they have money to scale). Which is back to my question of whether this is the right choice. Thank you very much.