T
tRPC

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

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

RRyan4/28/2023
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!
Nnlucas4/28/2023
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
AKAlex / KATT 🐱4/28/2023
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
Nnlucas4/29/2023
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?
AKAlex / KATT 🐱4/29/2023
yeah they generally do stay alive longer but i don't think it's advised to rely on that
RRyan4/30/2023
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
AKAlex / KATT 🐱4/30/2023
You can skip using serverless (that's what I do)
RRyan5/1/2023
fair point 🫡
TTom5/1/2023
anybody have any other workarounds for this? im kinda already committed to serverless for a bunch of other reasons
Nnlucas5/1/2023
"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
RRyan5/1/2023
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
TTom5/1/2023
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
AKAlex / KATT 🐱5/1/2023
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
TTom5/1/2023
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
AKAlex / KATT 🐱5/1/2023
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
RRyan5/1/2023
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
TTom5/1/2023
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
RRyan5/1/2023
If you do - let me know how it is 🙂
AKAlex / KATT 🐱5/1/2023
Actually in edge fns you can prolong it apparently, not played with this https://nextjs.org/docs/api-reference/next/server#nextfetchevent
TTom5/2/2023
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

Looking for more? Join the community!

T
tRPC

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

Join Server
Recommended Posts
useInfiniteQueryHey i saw on trpc docs that it is used with prisma, but can i so it with drizzle ? How to past cursoTRPC Next/Server Types Broken >10.5.0Hi all, recently I upgraded from 10.5.0 to latest 10.21.2, discovering that I now have type-check erJWT Token is type "never" in frontend.??!!TRPC Backend is sending JWT Token as string but frontend is reading it as type "never". I am using tHow to properly check the contents of prefetched data?I have dynamic route with SSG and if coming product slug is not in db I want to return notFound: trutrpc/next very slowI have set up my project using trpc/next and i have extremely slow queries, simple hello world takinGeneric handler for data.isLoading and data.isErrorHi, I'm looking for a way to create generic interface for useQuery result (budgetData from example bBug where 2 requests are fired at once. TRPC batches them. Can I cancel the 2nd via ProcedureOption?Hey all. I have a bug where my app fires two identical requests at the same time. This happens do toHow to do an async API call in useEffect (T3 stack)Hey, I have the router below and want to call the `tutor`async in an `useCallback` function, but thepagination - Offset MethodHi trpc has pagination example but only using cursor https://trpc.io/docs/reactjs/useinfinitequery next js appDirNext js tRpc What are the advantages of using trpc instead of the native Next.js APIs when buildingHow can you fetch data on a dynamic router with trpc?I’m creating a table component within my NextJs app. Instead of making an api call in the parent comMocking tRPC call w/ Playwright (Transform Error)I have a tRPC call that I would like to mock out for a Playwright E2E test. I've followed their doczod input validation from ts typeI imported a type using `import type { WebhookEvent } from "@clerk/nextjs/api";`. Is there a way to Looking to fix my tRPC implementationHi guys I am looking for some help implementing tRPC in my current project, I have 3 files that needExpression produces a union type that is too complex to representHi! I have started to encounter the above on error on pretty simple react components using trpc v10 Skipping useQuery with typescriptI'm wondering if there is a way to skip a query in a typescript friendly way? `rtk-query` has a handClerk Webhook Input UndefinedHi! I wrote a public procedure that takes in an input and updates user info based on Clerk Webhook. Getting this error: Cannot read properties of undefined (reading 'upsert')I'm using the T3 Stack. And I'm fairly new to tRPC so I am not sure what this error is caused. I'm Stuck trying to utilize useQuery to fetch some data on form submissionI'm trying to call a procedure in trpc through the use of useQuery in react upon form submission. SocreateTRPCNext type errorHi everyone. So I started creating nextjs app with trpc + prisma set up. and when i use my AppRouter