You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
An internal 504 from an Edge Function means the function failed to initiate a response within 150s, the max execution time.
As of now, this limit cannot be increased. If your function always needs more time than allowed, skip to When optimization isn't enough section. Otherwise, follow the steps below to speed up response times.
Step 1: Identifying slow Functions
You can filter for 504 events in the Log Explorer with the below query:
select
cast(timestampas datetime) astimestamp,
req.pathname,
res.status_code,
metadata.execution_time_msfrom
function_edge_logs
cross join UNNEST(metadata) as metadata
cross join UNNEST(metadata.request) as req
cross join UNNEST(metadata.response) as res
whereres.status_code=504limit20;
You can further explore how much time a specific function takes on average by running the below query:
selectreq.pathname,
res.status_code,
AVG(metadata.execution_time_ms) as avg_runtime_ms,
MIN(metadata.execution_time_ms) as min_runtime_ms,
MAX(metadata.execution_time_ms) as max_runtime_ms
from
function_edge_logs
cross join UNNEST(metadata) as metadata
cross join UNNEST(metadata.request) as req
cross join UNNEST(sb) as sb
cross join UNNEST(req.headers) as headers
cross join UNNEST(metadata.response) as res
wherereq.pathname='/functions/v1/YOUR_FUNCTION_NAME'-- <---add your function name or remove filtergroup byreq.pathname, res.status_codelimit20;
Once candidate functions are identified, you can investigate more thoroughly.
Step 2: Investigating slow Functions
Add console.time labels around suspicious sections to pinpoint where time is being spent:
The time response will show up in the function's dashboard under the Log tab with the label you assigned it. You can use the label as a keyword to search for the timestamp.
For local development, you can also step through execution with Chrome Dev Tools.
Common culprits:
Sequential API calls that could run in parallel
Unindexed or unoptimized database queries
External APIs that are throttling or rate-limiting you
Loops without a clear escape condition
Step 3: Optimize
The below suggestions are some strategies you can pursue to reduce execution times.
Parallelizing requests
If requests are independent of one another, rather than calling them sequentially, you can speed up operations by calling them in parallel with Promise.all:
// Before: sequential: total time = A + BconstresultA=awaitfetch('https://api-one.com/data')constresultB=awaitfetch('https://api-two.com/data')// After: parallel: total time = max(A, B)const[resultA,resultB]=awaitPromise.all([fetch('https://api-one.com/data'),fetch('https://api-two.com/data'),])
Splitting up logic
The Edge Function may be doing more than it needs to. It may be better to split up it's logic into multiple parts that can be called individually and run for shorter periods.
Utilize background tasks
You can initiate functions in a background task to be handled independently of the request/response handler. The process is outlined in the Background Task docs
Offload work to the client or an external service
Instead of managing all operations within the function itself, there may be an external API that can execute jobs faster and then send back results. Alternatively, you may be able to offload some processing to the requester rather than doing everything within the function itself.
Using alternative libraries
Some libraries, may be more optimal than others. It's worth exploring alternative libraries that may execute faster or are lighter weight.
Optimizing database operations
If you are connecting to Supabase Postgres from your function, you can inspect the speed of your queries in the performance dashboard. If the operations are slow, you can review performance and index advisor for improvement suggestions.
Implement caching
If you are making requests to the same resource and the return values change irregularly, you may be able to implement an external cache layer, such as Upstash Redis to reduce wait times.
Restrict request load
If the function is used to evaluate user submissions, you can restrict load size to reduce computational time. You can use the below query in the log explorer to filter by the content size in bytes provided by the initial requester:
selectreq.pathnameas function_name,
res.status_code,
AVG(COALESCE(cast(headers.content_lengthasint), 0)) as avg_content_size_in_bytes,
MIN(COALESCE(cast(headers.content_lengthasint), 0)) as min_content_size_in_bytes,
MAX(COALESCE(cast(headers.content_lengthasint), 0)) as max_content_size_in_bytes
from
function_edge_logs
cross join UNNEST(metadata) as metadata
cross join UNNEST(metadata.request) as req
cross join UNNEST(sb) as sb
cross join UNNEST(req.headers) as headers
cross join UNNEST(metadata.response) as res
wherereq.pathname='/functions/v1/induce-504'group byreq.pathname, res.status_codelimit10;
When optimization is not enough
Edge Functions have a hard runtime ceiling that cannot be raised. If your use case genuinely requires more, consider:
using a serverless/edge function service, such as AWS Lambda, with fewer restrictions
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
An internal 504 from an Edge Function means the function failed to initiate a response within 150s, the max execution time.
As of now, this limit cannot be increased. If your function always needs more time than allowed, skip to When optimization isn't enough section. Otherwise, follow the steps below to speed up response times.
Step 1: Identifying slow Functions
You can filter for 504 events in the Log Explorer with the below query:
You can further explore how much time a specific function takes on average by running the below query:
Once candidate functions are identified, you can investigate more thoroughly.
Step 2: Investigating slow Functions
Add
console.timelabels around suspicious sections to pinpoint where time is being spent:The time response will show up in the function's dashboard under the
Logtab with the label you assigned it. You can use the label as a keyword to search for the timestamp.For local development, you can also step through execution with Chrome Dev Tools.
Common culprits:
Step 3: Optimize
The below suggestions are some strategies you can pursue to reduce execution times.
Parallelizing requests
If requests are independent of one another, rather than calling them sequentially, you can speed up operations by calling them in parallel with Promise.all:
Splitting up logic
The Edge Function may be doing more than it needs to. It may be better to split up it's logic into multiple parts that can be called individually and run for shorter periods.
Utilize background tasks
You can initiate functions in a background task to be handled independently of the request/response handler. The process is outlined in the Background Task docs
Offload work to the client or an external service
Instead of managing all operations within the function itself, there may be an external API that can execute jobs faster and then send back results. Alternatively, you may be able to offload some processing to the requester rather than doing everything within the function itself.
Using alternative libraries
Some libraries, may be more optimal than others. It's worth exploring alternative libraries that may execute faster or are lighter weight.
Optimizing database operations
If you are connecting to Supabase Postgres from your function, you can inspect the speed of your queries in the performance dashboard. If the operations are slow, you can review performance and index advisor for improvement suggestions.
Implement caching
If you are making requests to the same resource and the return values change irregularly, you may be able to implement an external cache layer, such as Upstash Redis to reduce wait times.
Restrict request load
If the function is used to evaluate user submissions, you can restrict load size to reduce computational time. You can use the below query in the log explorer to filter by the content size in bytes provided by the initial requester:
When optimization is not enough
Edge Functions have a hard runtime ceiling that cannot be raised. If your use case genuinely requires more, consider:
Still stuck?
Here are some further resources you can review
Beta Was this translation helpful? Give feedback.
All reactions