How do I make my API faster?
Answered
blahaj.bitch posted this in #help-forum
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?
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
36 Replies
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? ...
10 seconds
its only the first request
other requests are faster
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
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
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
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
is it possible in instrumentation to maybe preload the connection too?
in production this will happen automatically. In dev you can change your code during runtime. So the route will also be build during runtime
does this also work with ts?
yes
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
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)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
when you starting your dev server you just add
--turbo
at the end. So like this next dev --turbo
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
I never used turbo, so I can't help you with errors. You can imagine turbo as own compiler. So like webpack
ruh roh, its working now, idk how that happened
holy, that is actually super fast
yea 🙂
now i might look into nextauth, since that would make my flimsy auth system a lot better and faster
thank you @B33fb0n3! :D
yea, next-auth is pretty cool. Don't use the beta of next-auth and don't use auth.js. They are pretty buggy
will keep in mind
happy to help