[ReactJS]

16 Nov 2026

-

8 min read time

Before Replacing Next.js, Find Out What Is Retaining Your Server Requests

Learn how to trace retained SSR requests, test a server-only fix, and decide whether a Next.js migration is worth the risk.

Kalle Bertell

By Kalle Bertell

A timer holds down a completed request, representing a timeout that keeps an SSR request in memory.

High memory use in a Next.js server is a reason to investigate, not a framework verdict. Before approving a migration, find out what remains alive after a representative server-side render, how long it stays alive, and whether a targeted change removes it. A framework move is expensive; it should solve a measured problem rather than stand in for a diagnosis.

First, identify which memory problem you have

A large server process can reflect several different costs: modules loaded at startup, live requests under load, a cache that grows to a useful limit, or completed requests still reachable through timers and other references. Those cases call for different decisions. The Next.js memory guide notes that the server preloads page modules at startup in exchange for faster responses. That footprint is not evidence that a finished request is being retained. Nor will a build-memory setting fix retention in a running production server.

Start by separating the environment. Does memory rise during next dev, during next build, or while a production build serves traffic? If the concern is SSR request retention, reproduce it against the running production build. Use the routes, headers and concurrency patterns that matter to your application. Warm the process, run a controlled batch, allow an idle period, then repeat. Record request counts and timing so the next run is comparable.

Measure per-pod RSS, heapUsed, external and arrayBuffers alongside traffic, latency, garbage-collection activity and replica count. Compare heap measurements after garbage collection across load cycles, but do not treat a flat post-GC JavaScript heap as proof that the service is safe: RSS, external allocations and peaks under load still matter. Conversely, high RSS alone does not tell you what retained a request. The question is whether memory stabilizes at an acceptable level under your workload, including after the suspected retention window has passed.

The React Query timer that kept requests reachable

A useful case study comes from Rohit Kashyap’s investigation of an application using React Query v3. In a local production-build test with a mix of SSR routes, measured heap after forced garbage collection rose from 112 MB following a 56-request warm-up to 312 MB after another 280 requests, then to 934 MB after roughly 1,400 more. With a server-only timer fix, the corresponding measurements were 70, 75 and 84 MB. He also reports that average production memory fell from about 4.8 GiB to about 1.2 GiB per pod. Those numbers describe one application, not a savings estimate for yours.

The important finding was the retaining path, not merely the size difference. Kashyap traced completed ServerResponse objects through pending timeouts and React Query objects to an AsyncLocalStorage store associated with each request. Node’s documentation explains that a store remains accessible to asynchronous operations created inside its run() callback. A timer created in that context can therefore keep request-associated state reachable after rendering has finished.

The reported timers held requests for two to five minutes, not forever. That distinction matters when interpreting a memory graph: eventual release can produce a plateau rather than unbounded growth. It does not make the cost harmless. At sustained traffic, several minutes of completed requests can overlap. Whether that overlap is affordable depends on your request volume, what each context retains, and the memory available to each pod.

Completed requests overlap while a timer keeps them in memory, consuming capacity even if they are eventually released.

Do not generalize the remedy into “disable cache eviction.” Kashyap’s server QueryClient was request-scoped and discarded after rendering. A client deliberately kept alive across requests has a different cache lifecycle; removing its eviction mechanism may make memory behavior worse. Browser cache behavior is another separate decision. The transferable lesson is to find the reference that extends a request’s lifetime, then change only the lifecycle that is wrong.

Follow retaining paths, not just large objects

Once a repeatable load test shows unwanted retention, compare safely obtained heap snapshots before and after the workload. Look for completed-request objects and follow their retaining paths toward roots. A list of the largest individual objects may miss the problem: many modest per-request objects can add up without any one of them looking exceptional.

Inspect pending timers, AsyncLocalStorage stores, query clients, instrumentation, application caches and framework-owned signals. For each candidate, ask three concrete questions: was it created during a request, should it outlive that request, and what releases it? Check behavior after an idle interval long enough to cover a suspected timer. If memory drops then, you may have bounded but costly retention. If it does not, keep tracing rather than assuming the same timer explains everything.

Plan snapshots as an operational event. Node warns that taking a V8 heap snapshot can require about twice the current heap size and blocks the event loop. A memory-starved production pod is a poor place to discover either constraint. Reproduce on an appropriately sized instance where possible, and budget capacity and interruption risk if a production snapshot is necessary.

Keep the test conditions with the evidence: installed versions, routes, request mix, concurrency, warm-up, idle interval and the measurements taken at each point. Without those details, a smaller post-fix snapshot may simply reflect a different workload. A good investigation should let another engineer rerun the comparison and challenge the diagnosis.

Match the fix to the Query version and client scope

Check the installed React Query or TanStack Query version before changing configuration. TanStack’s v4 migration guide says v4 changed the server-side default cacheTime to Infinity, avoiding a manual garbage-collection timer by default. That does not rule out an explicitly configured finite cacheTime in an application. In v5 the setting is called gcTime; the fetched query-core implementation defaults it to Infinity on the server when unspecified, but schedules a timeout for a valid finite value. Inspect your installed package and your overrides rather than assuming the current source matches your deployment.

This is not only a historical v3 concern. A TanStack Query issue reports a separate Next.js 16 App Router reproduction using @tanstack/query-core v5.101.0. With an explicit 24-hour gcTime, the reporter observed a timer and approximately 1.4 MB retained per render. That is one reporter’s measurement, not a universal per-render cost. It does show why “we upgraded past v3” is not enough to close the investigation.

Inventory the server QueryClient scope and all relevant per-hook cacheTime or gcTime overrides. If retaining paths demonstrate finite SSR timers and the client is genuinely request-scoped, test the smallest server-only configuration change that removes those timers. Leave browser eviction behavior unchanged. Add a regression test that detects unwanted server-side timers, then repeat the original load test with the same routes, timing and measurements. If the server client is intentionally long-lived, assess cache ownership and eviction separately; Infinity is not a blanket prescription.

Decide whether the framework is actually the cause

Dependency timers are not the only plausible diagnosis. A Next.js Cache Components issue reports repeated memory growth on 16.3.3 and a plateau on a 16.4 canary under the same workload. A separate Next.js backport PR describes a fix for retention through a composite abort signal following cache prerendering. An issue reproduction, an upstream change and a patch in your installed release are different evidence. Verify which behavior and fix your version actually contains before committing to a rewrite or assuming an upgrade resolves it.

If retaining paths point to cache boundaries, examine those boundaries rather than treating every memory increase as a Query problem; our Cache Components migration guide provides context for that decision. A supported upgrade or a small configuration fix may be cheaper and safer than moving frameworks. Equally, repeated framework-specific regressions, a poor fit between the rendering model and the product, or broader operating costs can justify a migration. Changing frameworks does not inherently remove Node request-context or dependency-timer risks.

Compare options using measured effect, implementation and maintenance cost, and rollback difficulty. Include fleet behavior, not just heap size. Kashyap reports that lower memory changed scaling behavior and initially increased latency. If you need help diagnosing production Next.js behavior or weighing a React/Next.js migration, that comparison is the right scope for ReactJS & Next.js development support, not a promise that a rewrite will reduce memory.

A decision checklist for the next load test

  1. Classify the phase. Confirm whether the issue is in development, build, startup or a running production server. Use a production build to investigate SSR request retention.

  2. Establish a baseline. Record installed versions, representative routes and headers, concurrency, warm-up and idle intervals, request counts, per-pod memory categories, latency, garbage-collection activity and replica count.

  3. Test the lifetime. Compare post-GC samples across repeatable cycles and check for a plateau after any suspected timer duration. Account for RSS and external memory as well as the JavaScript heap.

  4. Prove the retaining path. Obtain snapshots with enough capacity, then trace completed requests toward roots. Check timers, request context, query clients, instrumentation, caches and framework-owned signals.

  5. Run the smallest credible fix. Verify client scope and explicit Query overrides, or confirm that a relevant framework fix is present in an installed release. Protect the change with a regression test and rerun the same workload.

  6. Make the architecture decision. Record the verified cause, post-fix memory and latency, fleet scaling, maintenance burden, and the scope and rollback plan for any framework move. Revisit migration if the targeted fix is inadequate or the operating and rendering-model costs still warrant it.

Kalle Bertell

By Kalle Bertell

More from our Blog

Keep reading