Ryan
Ryan15mo ago

Can you return from an API endpoint before a sync operation is complete?

I'm curious, if I have an endpoint that saves something to a DB and I choose to return from the endpoint without waiting for the DB call to finish, is it possible the server will shut down before the DB call is complete? I ran into this problem in a previous project where I was using AWS Lamdas, not Next or TRPC. I didn't want to wait for sync operations to complete if I didn't need the data in my API response, but I found that the Lambda would be shut down once I returned and often my processes would not be completed. Thanks!
19 Replies
Nick
Nick15mo ago
setImmediate is probably all you need. Node shouldn’t exit until the event loop has cleared Unless this is a long running task and it’s exceeding the max lambda run time, then it just gets killed
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
setImmediate is probably all you need. Node shouldn’t exit until the event loop has cleared
no guarantee with lambdas as soon as res.end() is called the lambda will be killed so tl;dr no if you're running on serverless
Nick
Nick15mo ago
Got it, thanks for the correction 🙂 I was under the impression from reading cloudwatch logs at work that JS lambdas stay awake/warm the full 15 minutes servicing requests though. Maybe the answer is somewhere in the middle?
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
yeah they generally do stay alive longer but i don't think it's advised to rely on that
Ryan
Ryan15mo ago
Yeah I was running into this issue with tasks that were < 30s Is there any way that you are aware of to handle this without adding the task to like a queue or something? @alex / KATT
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
You can skip using serverless (that's what I do)
Ryan
Ryan15mo ago
fair point 🫡
Tom
Tom15mo ago
anybody have any other workarounds for this? im kinda already committed to serverless for a bunch of other reasons
Nick
Nick15mo ago
"Don't do it" Either do all your work in-line, or use an event queue and a dedicated worker lambda or fargate runner Frankly that's the advice we should have given from the start, it's a big red flag doing work outside of the stack or a job runner because you can't handle errors or tell the user it's failed
Ryan
Ryan15mo ago
Agreed, it's more so small cleanup things that I wanted to do, like after a user has claimed x delete it from the DB in a second call that doesn't effect what the user sees. But as most of these won't add significant time I will probably just await them for now until I have time to setup a job queue
Tom
Tom15mo ago
This seems like kind of a non answer… I’m aware that I can’t handle errors or tell the user it failed. The point of the question is that I don’t care for this case. I allow my users to set up custom web hooks that can allow me to inform them when updates are made. But the user who made the update doesn’t need to wait for that. It’s inherently best effort. The server they specify may be down or take too long to respond. That shouldn’t bring things to a halt. And I can set up a queue but that’s a lot of extra work for this little bit of extra processing
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
Either block the response until its been done or you need to pass it to a queue or a worker somehow, there's really no alternative for serverless AFAIK so if you're not satisfied with that, maybe you should reconsider serverless
Tom
Tom15mo ago
if its not possible then thats ok and i can go build a separate function / queue, my point was just that there are valid use cases that dont require cleanup / replying to the user
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
i agree, that's one of the reasons i don't use serverless for my current project, it's not trivial to setup queues etc
Ryan
Ryan15mo ago
I've never used this service before, so not a recommendation, but I came across this product that has a Vercel integration that maybe you could use @Tom3 https://vercel.com/integrations/serverlessq
ServerlessQ – Vercel
Serverless Message Queues & Cron Jobs
Tom
Tom15mo ago
that is interesting.... i went a different route for my stuff. my data is going to cloud firestore so i set up a google cloud function to listen for updates to my data there and fire webhooks it works but GCF are nowhere near as nice to setup / use as anything from vercel is might have to check that out thanks
Ryan
Ryan15mo ago
If you do - let me know how it is 🙂
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
Actually in edge fns you can prolong it apparently, not played with this https://nextjs.org/docs/api-reference/next/server#nextfetchevent
next/server | Next.js
Learn about the server-only helpers for Middleware and Edge API Routes.
Tom
Tom15mo ago
oh wow that would bve awesome will check it out @alex / KATT this actually seems to work great and its SOOO much simpler than adding a google cloud funciton to my code which was my plan