# \[Support Guide\] Why is my function taking long or timing out?

**URL:** <https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689>\
**Category:** Support Guides\
**Tags:** lambda-functions, support-guide\
**Created:** [August 17, 2022, 11:08pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689 "2022-08-17T23:08:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gualter](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/gualter/32/35068_2.png) [@gualter](https://answers.netlify.com/u/gualter)\
**Post date:** [August 17, 2022, 11:08pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/1 "2022-08-17T23:08:25Z")

</div>

_Last updated by Netlify Support in July 2025_

In this guide, we’ll be talking about synchronous functions. We won’t be covering Edge Functions or Background Functions since those are not bound by the same limits as standard synchronous functions or Scheduled Functions are.

Netlify Functions are a core feature of our product. By delegating your server-side logic to them, you leverage serverless functions that are [version-controlled, built, and deployed along with the rest of your Netlify site](https://docs.netlify.com/functions/overview/).

However, [there are some limitations on function execution](https://docs.netlify.com/functions/overview/#default-deployment-options), with the **30 second execution limit** being one of the most critical factors to have in mind when designing your serverless code.

## Function crashing

If your function takes longer than the allowed execution time, you’ll most likely be presented with this message:

 ![image|600](https://global.discourse-cdn.com/netlify/original/3X/6/5/656d885ee0d80a05c32e3cb4ba29e2c6b1a15f6c.png)

That would be the error displayed when you directly visit the function URL. However, if you’re calling the function from your frontend using APIs like `fetch` or libraries like `axios`, you most likely won’t see this page. In that case, you’d have to check the dev tools in your browser and inspect the Network tab to check for that request. You could then see the above message in the “Response” tab.

Let’s dive into a few scenarios where this happens and how we can **take the first steps** towards mitigating them…

## Everything was running smooth… What happened?

Your site was running fine and everything working great. You’re already thinking about the next cool feature to roll out when you notice that someone DM’s you :this: **that** :this: screenshot from your production site.

Well this is strange! That isn’t supposed to happen. First thing you do is open your site immediately and try to reproduce the case…

… but it’s working fine now 🤔

Why am I not able to replicate this? Why is it only happening **sometimes**?

Typically reasons for this issue may include, but are not limited to:

- Your code is taking longer to execute due to some condition we (or even you for that matter) cannot predict or control (e.g. slow third-party API response);
- Your code may have been running at its limit before (near the limit of execution time) and all it took was a small delay to cause the failure. Maybe your code is not optimized enough or is too complex for a one function to handle.
- Your code does not reliably exit - perhaps you have a code path that will never complete somewhere in your function?

## My function never worked! It’s always timing out! What is wrong?

You’re excited on making your first step towards [the future of web development](https://jamstack.org/). You’re either starting a new project or thinking about migrating an existing site over to Netlify but are already running into some roadblocks along the way.

No worries, we’re here to help. You’ve set up your Netlify Function properly and followed every instruction “by the book” but your code is still timing out.

Typically reasons for this issue may include, but are not limited to:

- Guess what? **Basically the same as above**. Maybe slightly different but the root cause is most likely related to the same causes (slow APIs, extensive code, etc.)

## My site uses a framework such as Gatsby or Nextjs. What is happening?!

### Gatsby related functions

[To support Gatsby Functions and SSR/DSG render modes](https://docs.netlify.com/integrations/frameworks/gatsby/#auto-generated-netlify-functions), the Essential Gatsby build plugin automatically generates Netlify Functions called ` __api` , `__ ssr` , ` __dsg` , and `__ ipx`.

If you are using Gatsby version `>=5.12.0 `, [gatsby-adapter-netlify](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-adapter-netlify) will be installed automatically, and provide support for Gatsby Functions.

### Next.js related functions

When using the [Essential Next.js Build Plugin](https://github.com/netlify/netlify-plugin-nextjs), it [creates three Netlify functions](https://github.com/netlify/netlify-plugin-nextjs/#generated-functions) that handle requests that haven’t been pre-rendered. These are `___netlify-handler` (for SSR and API routes), `___netlify-odb-handler` (for ISR and fallback routes), and `_ipx` (for images).

### So… What’s happening?

If the function that is timing out is one of the framework-related ones, it’ll probably boil down to the same reasons. However, these functions have additional logic to sustain their execution that uses part of the allowed execution time.

## How to mitigate all scenarios

Take a closer look at your code.

Is it relying on any third-party service? Is that service working properly (might be a good thing to check their statuspage or logs)? Presumably, running the function locally will show the same behavior around the speed of API calls/responses so you can more easily debug.

If you have a framework-related function, is it timing out only for a particular page request? If so, what makes this page different than the others?

Try breaking-down your function into smaller parts. Instead of making 10 requests in the same API call, try splitting it into 2 functions with 5 calls each. Or, consider doing the “synchronous” part of the work in a function (verify credit card here), and the asynchronous part (place order with warehouse here) in a [background function](https://docs.netlify.com/functions/background-functions/), which can run for up to 15 minutes. Note that you can call a background function from a normal function; it will not block the primary function’s execution.

Sometimes, region also makes a difference. Netlify Functions, by default run in `us-east-1` (N. Virginia) region. So, if you are making API calls to services who have their servers located in the other end of the world, the responses will take longer. If you have a Pro or higher account, our staff can adjust the region of your functions for you per site or account wide to most AWS regions that are capable of running lambdas.

A few times, you might be missing something simple. You might have made all the necessary optimizations, but missing the final `return` statement to actually send the response back to the client. For example:

```auto
export async function handler(event, context) {
  const data = await doStuff()
  return {
    body: JSON.stringify(data)
    statusCode: 200
  }
}

```

In the above example, if you’re missing the `return` statement, the function will execute the `doStuff()` function and keep on waiting forever even after successfully retrieving data from that function. Your Function code might not always be this simple, so you might need to make sure that your Function is actually firing the `return` statement at the correct time.

### Logging execution time

The best way to see how long your Function is taking for a particular task is by logging its execution time. The best way to do that is by using `console.time()`. Here’s an example:

```auto
export async function handler(event, context) {
  console.time('doStuff')
  const data = await doStuff()
  console.timeEnd('doStuff')
  return {
    body: JSON.stringify(data)
    statusCode: 200
  }
}

```

In your Function console, you’d see something like: `doStuff: # seconds`. This will show how long your `doStuff()` call took. Again, depending on your Function code - for instance if it is using features like `async/await` statements, Promises, etc. you might have to adapt the above to correctly calculate those timings.

Once you know what piece of code is acting as a bottleneck in your Function, you can work on changing that.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [October 24, 2022, 7:59pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/2 "2022-10-24T19:59:48Z")

</div>

Automatically bumped on October 24, 2022, 7:59pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [January 1, 2023, 4:00am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/3 "2023-01-01T04:00:22Z")

</div>

Automatically bumped on January 1, 2023, 4:00am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [March 9, 2023, 12:35pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/4 "2023-03-09T12:35:59Z")

</div>

Automatically bumped on March 9, 2023, 12:35pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [May 15, 2023, 8:27pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/5 "2023-05-15T20:27:18Z")

</div>

Automatically bumped on May 15, 2023, 8:27pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [July 23, 2023, 6:05am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/6 "2023-07-23T06:05:23Z")

</div>

Automatically bumped on July 23, 2023, 6:05am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [September 29, 2023, 2:18pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/7 "2023-09-29T14:18:12Z")

</div>

Automatically bumped on September 29, 2023, 2:18pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [December 4, 2023, 10:37pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/8 "2023-12-04T22:37:02Z")

</div>

Automatically bumped on December 4, 2023, 10:37pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [January 27, 2024, 5:32am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/9 "2024-01-27T05:32:52Z")

</div>

Automatically bumped on January 27, 2024, 5:32am UTC



---

<div class="post-metadata">

**Author:** ![cyrilcabo](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/cyrilcabo/32/59181_2.png) [@cyrilcabo](https://answers.netlify.com/u/cyrilcabo)\
**Post date:** [February 29, 2024, 12:55pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/10 "2024-02-29T12:55:02Z")

</div>

Hey @gualter , we’re facing this same issue with our **Next.js app** as well, but ours takes longer – **~18-22 seconds on pages with SSR**. It’s important to note, that this **does not happen everytime** , it only happens after some stale time. We’ve reached to Netlify support, and told us it’s something on our end. My experiment, however, says something different.

Note: We don’t have any complex logic in any of the SSR functions. And even if we have an SSR function where we just return static data, we still have that ~18 seconds TTFB.

**What I did:**

- Add logging to SSR function, and the whole SSR func only runs for about 200 ms.
- Deployed the same app to Heroku, and the SSR func in Heroku also runs for 200ms.
- In netlify, after some stale time, TTFB is ~18 seconds. SSR execution ~200ms.
- In heroku, TTFB is ~1-3 seconds. SSR exectuion ~200ms.

If it’s on our end, wouldn’t we experience something similar with Heroku as well? Or are we just missing something? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [March 5, 2024, 4:09pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/12 "2024-03-05T16:09:18Z")

</div>

Hi thanks for writing into our Forums! 👍🏿 I see we are working with you in our helpdesk. Please refer to ticket # **229087**

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [May 7, 2024, 6:54pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/13 "2024-05-07T18:54:53Z")

</div>

Automatically bumped on May 7, 2024, 6:54pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [July 9, 2024, 3:14am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/14 "2024-07-09T03:14:29Z")

</div>

Automatically bumped on July 9, 2024, 3:14am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [September 10, 2024, 11:54am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/15 "2024-09-10T11:54:00Z")

</div>

Automatically bumped on September 10, 2024, 11:54am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [November 7, 2024, 7:47pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/16 "2024-11-07T19:47:22Z")

</div>

Automatically bumped on November 7, 2024, 7:47pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [January 5, 2025, 3:01am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/17 "2025-01-05T03:01:14Z")

</div>

Automatically bumped on January 5, 2025, 3:01am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [March 3, 2025, 9:33am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/18 "2025-03-03T09:33:27Z")

</div>

Automatically bumped on March 3, 2025, 9:33am UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [April 28, 2025, 3:42pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/19 "2025-04-28T15:42:14Z")

</div>

Automatically bumped on April 28, 2025, 3:42pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [June 21, 2025, 9:03pm UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/20 "2025-06-21T21:03:39Z")

</div>

Automatically bumped on June 21, 2025, 9:03pm UTC



---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/system/32/13_2.png) [@system](https://answers.netlify.com/u/system)\
**Post date:** [August 15, 2025, 2:53am UTC](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689/21 "2025-08-15T02:53:23Z")

</div>

Automatically bumped on August 15, 2025, 2:53am UTC



[Next page](https://answers.netlify.com/t/support-guide-why-is-my-function-taking-long-or-timing-out/71689.md?page=2)
