How do I make my API faster?
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
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
@Transvaal lion 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?
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? ...
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
@Transvaal lion its only the first request
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
Transvaal lionOP
it also could be JOSE setting up, because that seems to take a hot second and my implementation isnt very secure
@B33fb0n3 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
Transvaal lionOP
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
@Transvaal lion is there a way to make it faster in dev?
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
Transvaal lionOP
is it possible in instrumentation to maybe preload the connection too?
@Transvaal lion 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
@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
Transvaal lionOP
does this also work with ts?
@B33fb0n3 in production this will happen automatically. In dev you can change your code during runtime. So the route will also be build during runtime
Transvaal lionOP
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?
@B33fb0n3 yes
Transvaal lionOP
will look at that as well
@Transvaal lion 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?
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)Transvaal lionOP
im pretty familiar with mongodb, so ill stick with that for the minute, but i might consider that too
@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
Transvaal lionOP
im a bit confused on how to get this working
@Transvaal lion 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
Transvaal lionOP
i get all these mongo errors when running my app
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
@Transvaal lion does turbo happen to bypass instrumentation on start?
I never used turbo, so I can't help you with errors. You can imagine turbo as own compiler. So like webpack
Transvaal lionOP
ruh roh, its working now, idk how that happened
holy, that is actually super fast
yea 🙂
Transvaal lionOP
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
@B33fb0n3 yea, next-auth is pretty cool. Don't use the beta of next-auth and don't use auth.js. They are pretty buggy
Transvaal lionOP
will keep in mind
happy to help