Next.js Discord

Discord Forum

How do I make my API faster?

Answered
blahaj.bitch posted this in #help-forum
Open in Discord
Avatar
Currently my API uses GET and POST requests, but I find those to sometimes be slower than what's really needed for my app.
Is there a faster way to do APIs, like WebSockets, but easier and less resource-heavy?
Answered by B33fb0n3
yea you can [use turbo](https://nextjs.org/docs/app/api-reference/next-config-js/turbo) to make your dev server faster. It's stable now
View full answer

36 Replies

Avatar
how long does a request take now on avg? Do you know why it's slow? Do you know which part of the api is slow? ...
Avatar
10 seconds
its only the first request
other requests are faster
Image
sometimes other requests suffer the same issue tho
im thinking it could be mongoose connecting to mongodb, but im not sure what to do about that
Avatar
oh.. that's normal. When beeing in dev mode the first time youy access it, the whole route will be compiled as well. All this is in production already done. So they will be waaay faster in prod
Avatar
it also could be JOSE setting up, because that seems to take a hot second and my implementation isnt very secure
is there a way to make it faster in dev?
it should be pretty quick on first run, but after mongo and jose being used it has drastically slowed down
Avatar
yea you can [use turbo](https://nextjs.org/docs/app/api-reference/next-config-js/turbo) to make your dev server faster. It's stable now
Answer
Avatar
is it possible in instrumentation to maybe preload the connection too?
Avatar
in production this will happen automatically. In dev you can change your code during runtime. So the route will also be build during runtime
Avatar
does this also work with ts?
Avatar
yes
Avatar
i can see that, but is it at least possible to preload the mongo connection in instrumentation so it doesnt have to connect every time?
will look at that as well
Avatar
that's how databases work. You get a connection, execute your queries though this connection and the connection will be closed. That's a basic database connection and cant be disabled
However: you can make your DB itself faster. For example by using postgresql. With that you can get results from your DB in 20ms (with a slow server)
Avatar
im pretty familiar with mongodb, so ill stick with that for the minute, but i might consider that too
im a bit confused on how to get this working
Avatar
when you starting your dev server you just add --turbo at the end. So like this next dev --turbo
Avatar
i get all these mongo errors when running my app
Image
does turbo happen to bypass instrumentation on start?
or like run alongside it
because it looks like turbo doesnt start instrumentation before doing everything else
Avatar
I never used turbo, so I can't help you with errors. You can imagine turbo as own compiler. So like webpack
Avatar
ruh roh, its working now, idk how that happened
holy, that is actually super fast
Image
Avatar
yea 🙂
Avatar
now i might look into nextauth, since that would make my flimsy auth system a lot better and faster
thank you @B33fb0n3! :D
Avatar
yea, next-auth is pretty cool. Don't use the beta of next-auth and don't use auth.js. They are pretty buggy
Avatar
will keep in mind
Avatar
happy to help