Huge increase in CPU time after enabling edge runtime

Hey all, on 7th December I switched a specific route to edge (export const runtime = “edge”;)

I’m, noticing, that my active CPU increased drastically since then:

I haven’t changed anything else. The route.ts is quite simple, it returns some templated html, no external calls / APIs.

route.ts:

import { NextRequest, NextResponse } from "next/server";

export const runtime = "edge"; // this caused the increase in CPU time, no other deployment that day

export async function GET(
    request: NextRequest,
    { params }: { params: Promise<{ module: string[] }> }
) {
    const { module: moduleArray } = await params;
    const moduleName = moduleArray.join("/");

    // Safety check: ensure we have a module name
    if (!moduleName) {
        const redirectResponse = NextResponse.redirect("https://github.com/atomicgo", 308);
        redirectResponse.headers.set(
            "Cache-Control",
            "public, s-maxage=31536000, stale-while-revalidate=31536000, max-age=31536000, immutable"
        );
        return redirectResponse;
    }

    const searchParams = request.nextUrl.searchParams;
    const goGet = searchParams.get("go-get");

    // If go-get=1 is present, return the HTML with meta tags
    if (goGet === "1") {
        const html = `<!DOCTYPE html>
<html>
  <head>
    <meta name="go-import" content="atomicgo.dev/${moduleName} git https://github.com/atomicgo/${moduleName}.git">
    <meta name="go-source" content="atomicgo.dev/${moduleName} https://github.com/atomicgo/${moduleName} https://github.com/atomicgo/${moduleName}/tree/main{/dir} https://github.com/atomicgo/${moduleName}/blob/main{/dir}/{file}#L{line}">
  </head>
</html>`;

        return new NextResponse(html, {
            headers: {
                "Content-Type": "text/html; charset=utf-8",
                "Cache-Control": "public, s-maxage=31536000, stale-while-revalidate=31536000, max-age=31536000, immutable",
            },
        });
    }

    // Otherwise, redirect to GitHub
    const redirectResponse = NextResponse.redirect(
        `https://github.com/atomicgo/${moduleName}`,
        302
    );
    redirectResponse.headers.set(
        "Cache-Control",
        "public, s-maxage=31536000, stale-while-revalidate=31536000, max-age=31536000, immutable"
    );
    return redirectResponse;
}

(Yes I know, that code needs some refactoring)

I would be glad, if someone could explain me, why running on edge is increasing the overall active CPU time by so much.

Also, is Edge the correct runtime for this function? It has low traffic (couple thousand requests / day), but the HTML for the same parameters is completely static after first generation(could be cached forever).

In my limited knowledge about Vercel (new here), I thought edge was supposed to be faster, as it’s closer to the users. Also the runtime is smaller afaik, so I don’t understand the change in metrics.

Thanks in advance!!

More screenshots:

P95:

I’m checking internally for the details on why the metrics look the way they do because I agree this is strange

Vercel has many regions around the world where your functions can be deployed, but they’re generally just deployed to 1 or 2 (and should be kept near your DB). When you use the regular node runtime, it runs on that server

When you switch to Edge runtime, it runs on whichever of these regions is closer to the user, meaning instead of 1 possible server you now have 20

  • each of these caches the response independently so you lower your cache hit rate
  • since there’s less traffic at any one region, the risk of cold starts gets higher
  • the edge servers are also less powerful machines, so they may take longer to do the same compute

For those reasons I wouldn’t see a strong pressing need to use the edge runtime here – optimizing for cache hits makes more sense to me

But the increase in active CPU time is more than I would expect it to be for a function that’s basically a switch statement, I’ll keep digging here

1 Like

Quick update: after disabling edge, the active CPU time instantly dropped again:

So it definitely has something to do with edge.