Netlify serverless functions slow execution time

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):

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

            console.log(`Starting porcessing body (${ - 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} (${ - start})`)

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

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

            console.log(`returning (${ - start})`)
            return res.status(200).json({
                success: true,
                code: null,
                exampleAnswer: answer.question.exampleAnswer
        } catch (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:

On development server and production db:

On netlify using the same production db as above:

(All timestamps are in ms)

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

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)


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

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

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

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.

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