Edge Functions or Lambda Functions to be used in Time Consuming API call

Hello,

First of all, thanks for building & supporting this amazing product. I am on the free tier as of now.
Even though I’m using custom domain, this is the Netlify version of my website: https://me-yu.netlify.app/

This is a SvelteKit website with an API endpoint in a server.js file. I call this file with a prompt, and it calls Replicate API, generates an image using a diffusion model with the prompt, and returns the URL of the image to the client.

The problem Im facing is that it might take more than 10 seconds for the Replicate API to create and return the image URL my server.js endpoint (Netlify function). And since there is 10 second time out limit, the server returns an error after I deploy on Netlify.

I understand that this 10 second limit can be increased to 26 seconds with Pro plan, but even then, there is still risk of Replicate API taking longer than 26 seconds if there is a lot of load on the network so I dont think this will be a permanent solution.

So I have experimented with:

  1. Skip waiting for Replicate API to create the full image and use a webhook instead which can be called by Replicate when image generation is finished. Then, I can use Server Side Events (SSE) to notify the client when this is done.
    PROBLEM: Even SSE function will have a 10 seconds timeout error, so after I start listening for webhook from Replicate, I still face Timeout error.

  2. I have tried Edge Functions instead. Using edge functions, I can wait for + 10 sec for Replicate API to return, as await calls do not count towards Edge Functions 50ms time limit. And this seems to work for now without implementing the webhook. However, with Edge functions, I sometimes get below error which Im yet to pinpoint the root cause:

Jan 26, 05:00:09 PM: 01JJGSZ0 error  [render] AbortError: The signal has been aborted
    at AbortSignal.[[[signalAbort]]] (ext:deno_web/03_abort_signal.js:147:14)
    at AbortController.abort (ext:deno_web/03_abort_signal.js:304:30)
    at AbortSignal.<anonymous> (file:///root/src/bootstrap/function_chain.ts:401:34)
    at innerInvokeEventListeners (ext:deno_web/02_event.js:754:7)
    at invokeEventListeners (ext:deno_web/02_event.js:801:5)
    at dispatch (ext:deno_web/02_event.js:658:9)
    at AbortSignal.dispatchEvent (ext:deno_web/02_event.js:1043:12)
    at AbortSignal.[[[runAbortSteps]]] (ext:deno_web/03_abort_signal.js:189:13)
    at AbortSignal.[[[signalAbort]]] (ext:deno_web/03_abort_signal.js:166:24)
    at ext:deno_web/03_abort_signal.js:129:28

Is this error related to the 50 ms timeout error?

Also, I understand the Edge functions are for lightweight operations such as http request header adjustment. Are edge functions the right tool to use to solve my problem (API call taking longer than 10seconds which is causing timeout error)?

Looking forward to your help on this!

@randomid With a Pro plan, you could use Background Functions: