# Netlify serverless functions slow execution time

**URL:** <https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224>\
**Category:** Support\
**Tags:** lambda-functions, nextjs\
**Created:** [June 24, 2023, 1:22pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224 "2023-06-24T13:22:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stas1407](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stas1407/32/51565_2.png) [@Stas1407](https://answers.netlify.com/u/Stas1407)\
**Post date:** [June 24, 2023, 1:22pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224/1 "2023-06-24T13:22:48Z")

</div>

My website became really slow after deploying to Netify and I have no idea why. It is rather obvious that response times can be slower due to geographical location and distances between servers but in my case the functions’ execution time became 3-5 seconds longer.

Code of the example function (next.js api endpoint):

```auto
export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse<ExampleAnswerApiResponse>
) {
    if (req.method === 'POST') {
        const start = performance.now()
        console.log("Starting processing...")
        try {
            console.log(`Getting session (${performance.now() - start})`)
            const session = await getServerSession(req, res, authOptions)
            if (!session)
                return res.status(401).json({ success: false, code: "UNAUTHORIZED" });
            console.log("Got session: ", session.user.name, "(", performance.now() - start, ")")

            console.log(`Starting porcessing body (${performance.now() - start})`)
            const body = await JSON.parse(req.body)
            const type = body.type
            if (!type || (type !== "answering_pois" && type !== 'asking_pois' && type !== 'speech')) return res.status(400).json({ success: false, code: "DATA_MISSING" })
            console.log(`Got body: type = ${type} (${performance.now() - start})`)

            console.log(`Getting answer id and example answer (${performance.now() - start})`)
            const answer = await db.answer.findFirstOrThrow({
                select: {
                    id: true,
                    question: {
                        select: {
                            exampleAnswer: true
                        }
                    }
                },
                where: {
                    userId: session.user.id,
                    finished: false,
                    question: {
                        type: type
                    }
                }
            })
            console.log(`Got answer: answer_id = ${answer.id} (${performance.now() - start})`)

            console.log(`Updating answer (${performance.now() - start})`)
            const r = await db.answer.update({
                data: {
                    seenExampleAnswer: true
                }, where: {
                    id: answer.id
                }
            })
            console.log(`Updated answer: ${r.id} (${performance.now() - start})`)

            console.log(`returning (${performance.now() - start})`)
            return res.status(200).json({
                success: true,
                code: null,
                exampleAnswer: answer.question.exampleAnswer
            })
        } catch (err) {
            console.log(err)
            return res.status(500).json({
                success: false,
                code: "UNKNOWN_ERROR"
            })
        }
    } else {
        return res.status(405).send({ success: false, code: 'INVALID_METHOD' })
    }
}

```

On development server and db:

 ![Zrzut ekranu 2023-06-24 o 14.17.20](https://global.discourse-cdn.com/netlify/original/3X/1/3/139e33b4f96a253a443f3dd01345ee96d2048405.jpeg)

On development server and production db:

 ![Zrzut ekranu 2023-06-24 o 14.40.04](https://global.discourse-cdn.com/netlify/original/3X/c/4/c4400ebfcd035426bc31d2e1f95adf2ae6af83ba.jpeg)

On netlify using the same production db as above:

 ![Zrzut ekranu 2023-06-24 o 14.41.18](https://global.discourse-cdn.com/netlify/original/3X/f/0/f098763ec385b23d3a0174a321ce2a158402bcdc.jpeg)  
(All timestamps are in ms)

Queries execution time in production db is around 10ms (planet scale):

 ![Zrzut ekranu 2023-06-24 o 15.02.22](https://global.discourse-cdn.com/netlify/original/3X/e/b/ebef793aad7e292c9e22d2500c0c2ee09e2a6696.jpeg)

Additionally I have no idea why getServerSession is suddenly taking over 1s on netlify when it theoretically doesn’t make any requests.

Also there is over 1s gap between last console.log and end of execution (console.log - 2505ms, total function duration - 3818ms)

website: [https://mywebsite1234abvjkjsdf.netlify.app](https://mywebsite1234abvjkjsdf.netlify.app)

I’ve been stuck on this for the past few days so I would really appreciate any help

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [June 25, 2023, 12:24pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224/2 "2023-06-25T12:24:21Z")

</div>

Netlify Functions by default as in `us-east-1`. Where’s your DB?

---

<div class="post-metadata">

**Author:** ![Stas1407](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stas1407/32/51565_2.png) [@Stas1407](https://answers.netlify.com/u/Stas1407)\
**Post date:** [June 25, 2023, 12:36pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224/3 "2023-06-25T12:36:17Z")

</div>

My db is in Frankfurt. Is it possible that it results in over 1s delay?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [June 25, 2023, 12:40pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224/4 "2023-06-25T12:40:25Z")

</div>

Can’t say. But since that seems to be the first major bottleneck based on your timestamps, I can only think of that being the logical conclusion. On pro and above plans, we can set the location of Functions to `eu-central-1` which is Frankfurt, if I recall correctly.

---

<div class="post-metadata">

**Author:** ![Stas1407](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stas1407/32/51565_2.png) [@Stas1407](https://answers.netlify.com/u/Stas1407)\
**Post date:** [June 25, 2023, 12:54pm UTC](https://answers.netlify.com/t/netlify-serverless-functions-slow-execution-time/95224/5 "2023-06-25T12:54:56Z")

</div>

Okay then thank you very much for the support I will try to move the db.
