R
Railwayβ€’8mo ago
ivano.cs89

Memory usage even when not using nextjs app

Hello I'm still kinda new to railway but I'm finding some issues related to the memory usage so i wanted to see if someone could help me a little bit with this, i'd be very grateful, I have a nextjs app deployed and connected to a postgresql instance, it runs perfectly, however, even when the app is not being used the memory usage seems to be constantly around 400-600 mb, which of course translates in a higher cost, I can't find the reason why this is happening, I don't think it is related to the database, cause the memory usage on it stays relatively low(around 50mb) but ofc you can correct me if i'm wrong :). I tried even setting the sleep feature on both the nextjs app and the database and it only seems to work when I redeploy, cause after using one single time the app the memory usage then stays consistent even if it's not being used anymore as I said. Should't it go back to sleep? Attached you can find two ss's the one with the highest memory usage is the nextjs app the other is the postgresql instance. Thank you in advance πŸ™‚
No description
No description
20 Replies
Percy
Percyβ€’8mo ago
Project ID: d3baa03c-db75-49f3-9e1a-bf5ce2f999b6
ivano.cs89
ivano.cs89OPβ€’8mo ago
d3baa03c-db75-49f3-9e1a-bf5ce2f999b6
Brody
Brodyβ€’8mo ago
what start command are you using to start next?
ivano.cs89
ivano.cs89OPβ€’8mo ago
hello, I'm using the default next start command : "start": "next start"
Brody
Brodyβ€’8mo ago
are you doing anything cache related or using sharp? those things tend to use a lot of memory
ivano.cs89
ivano.cs89OPβ€’8mo ago
is it normal that the memory usage stays consistent even when not using or is it something that definitely shouldn't happen?
Brody
Brodyβ€’8mo ago
thats how memory works, your app is running 24/7 even when you arent using it
ivano.cs89
ivano.cs89OPβ€’8mo ago
not really no, I actually stopped using the optimized images and stuff, I'm only using getstaticpaths during build time but no, nothing cache related
Brody
Brodyβ€’8mo ago
node and next are not known to be particularly memory efficient, this could just simply be what your app uses as for app sleeping, there are many things that can stop your app from going to sleep, some of which would be bots accessing the site, next telemetry, connecting to the database over the public network, etc
ivano.cs89
ivano.cs89OPβ€’8mo ago
got it, thanks Brody, so just to make it clear, even if the app was more efficient is terms of memory usage, it would never go to sleep anyway right? Cause now that you mention it is runnin 24/7 then iI think i had a misconception about how it works, i thought when it was not being used the memory should go back to a lower level, but then the behaviour I'm getting is just the expected one right? πŸ™‚
Brody
Brodyβ€’8mo ago
how much memory an app uses has nothing to do with it going to sleep, its based on outbound network traffic, you could try turning off next telemetry and connecting to the database via the private network and see if it can then go into sleep, when sleeping its going to use zero cpu and mem (even though when sleeping the graph would appear to show full memory usage its just frozen)
ivano.cs89
ivano.cs89OPβ€’8mo ago
thanks for the tip, I'll definitely give it a try, don't wanna bother but could you provide some basic help on how to do it?
Brody
Brodyβ€’8mo ago
disabling next telemetry is just an environment variable, I don't know it off the top of my head but that's just a quick search away. as for connecting to the database via the private network, simply use the private url
ivano.cs89
ivano.cs89OPβ€’8mo ago
thanks already giving it a try! Just one more question, when using the private url for the database, does the name for the env variable on the nextjs app matters? I mean would I have to set it to "DATABASE_PRIVATE_URL" or can I leave it as it is "DATABASE_URL" an just change the value?
Brody
Brodyβ€’8mo ago
you can leave it as is in your code and in railway set a service variable -
DATABASE_URL=${{Postgres.DATABASE_PRIVATE_URL}}
DATABASE_URL=${{Postgres.DATABASE_PRIVATE_URL}}
ivano.cs89
ivano.cs89OPβ€’8mo ago
Perfect thanks for the help Brody! πŸ™‚
alexisdessard
alexisdessardβ€’8mo ago
Hello, exactly the same issue for me so I thought it'd be better to comment here instead of creating a new post. I understand that MySQL would be the problem and MARIADB the solution, gonna try to use it right now, but someone could explain me as a kid how the memory usage works? I also had to pay 10$ for the memory usage without having launched my product so I'm scared this memory usage increases for each new users and I have no idea if each of the users would cost me 10$ (i guess no) or if they will barely increase the memory usage. I have a doubt about your comment here. From the server, should I put the variable MARIADB_HOST or MARIADB_PRIVATE_HOST to reduce egress fees? And why are they two different variables (private or not) ?
Brody
Brodyβ€’8mo ago
I can safely say you won't pay an extra $10 in memory for every new user, something would have to be very wrong for that to happen. Processes anywhere from in the cloud to on your computer use memory to run, and thats part of what you get charged for on Railway. You should use the private host and port when you run your app on Railway to eliminate the service <---> database egress fees. There's both, because one is public and one is private, sometimes you need to be able to access a database publicly.
alexisdessard
alexisdessardβ€’8mo ago
Thanks a lot Brody. Sorry one more question: I just added these variables: - MARIADB_PRIVATE_HOST - MARIADB_PRIVATE_PORT My config from NestJS/TypeORM looks now like this: export default registerAs('orm.config', (): TypeOrmModuleOptions => ({ type: 'mariadb', host: process.env.MARIADB_PRIVATE_HOST, port: Number(process.env.MARIADB_PRIVATE_PORT), username: process.env.MARIADB_USER, password: process.env.MARIADB_PASSWORD, database: process.env.MARIADB_DATABASE, ...)} But I also see a variable named "MARIADB_PRIVATE_URL", should I use it or this current config using private host and port is enough to avoid extra egress fees?
Brody
Brodyβ€’8mo ago
yeah using the private host and port is enough, the url is there incase thats what works better for your code
Want results from more Discord servers?
Add your server