Request memoization and parallel fetching

Intermediate · 7 min

How Next.js deduplicates identical fetches in one render, when React.cache is needed instead, and how to avoid request waterfalls with Promise.all.

Before thisFetching data in Server ComponentsQuerying a database directly

Stav · 29 min · from 0:20

Request memoization and parallel fetching

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by StavReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Identical fetch GET requests (same URL and options) in one server render are memoized, so several components can ask for the same data and only one network request goes out.
  • Memoization lasts for a single render pass and is not shared between requests; it is separate from persistent caching.
  • Memoization does not apply in Route Handlers, and for non-fetch work such as an ORM query you wrap the function in React.cache.
  • Awaiting two independent requests one after the other creates a waterfall; start both first, then await them together with Promise.all.
  • Preloading means calling a deduplicated data function early, without await, so its request overlaps with other blocking work.

The problem with fetching close to where data is used

The last two lessons ended on the same advice: fetch inside the component that needs the data. It keeps components self-contained. A <UserBadge> fetches its own user, a <Sidebar> does the same.

It also sets a trap. A page might render <Header>, <Sidebar>, and <Profile>, and all three need the current user. If each one made its own request, one page view would call the same endpoint three times.

Memoization: one ticket for three waiters

Think of a kitchen with one ticket printer. Three waiters ask for table 4’s order. The kitchen does not cook it three times; the first request prints the ticket and the others read it.

Next.js does this for fetch. During a single server render, GET requests with the same URL and options are memoized. Call the same endpoint from a layout, a page, generateMetadata, and generateStaticParams, and it executes once. The docs describe this as separate from persistent caching: memoization lasts only for one render pass, while cached responses persist across requests.

A few edges to know:

  • Opt out by passing an AbortController signal to fetch.
  • It does not apply in Route Handlers, because they are not part of the React component tree.
  • The cache is created per request. Nothing is shared between visitors.

React.cache for everything that is not fetch

An ORM call is not fetch, so nothing deduplicates it. Wrap the function in React.cache:

import { cache } from 'react'
import { db, eq, users } from '@/lib/db'

export const getUser = cache(async (id: string) => {
  return db.query.users.findFirst({ where: eq(users.id, id) })
})

Calls with the same id in one request now return the same memoized result. It has the same per-request scope as fetch memoization.

There is one boundary to remember when you reach use cache. The docs state that React.cache runs in an isolated scope inside a use cache boundary: a value stored outside is not visible inside. Pass data into a cached function through its arguments.

Waterfalls

Deduplication saves repeated work. A waterfall is a different cost: requests that could overlap but run one after another.

export default async function Page({ params }: PageProps<'/artist/[username]'>) {
  const { username } = await params
  const artist = await getArtist(username)   // waits...
  const albums = await getAlbums(username)   // ...then starts
  return <h1>{artist.name}</h1>
}

If each call takes 300 ms, the page takes 600 ms, even though neither request needs the other. Start both and wait for both:

const artistData = getArtist(username)
const albumsData = getAlbums(username)
const [artist, albums] = await Promise.all([artistData, albumsData])

Calling getArtist without await starts the request immediately; Promise.all waits for the pair. Now the page takes roughly the longest of the two. If one request failing should not sink the other, the docs point to Promise.allSettled.

Some waterfalls are real, because the second request needs the first one’s result (an artist’s playlists need the artist id). The docs’ advice there is to put the dependent part behind <Suspense> so the rest of the page is not held back, and to make sure the first request is fast.

Preloading

Sometimes a component renders after other blocking work, so its request starts late even though its inputs were known earlier. Preloading starts it early. Call the data function without await before the blocking work, then call it again where the data is used:

preload(id)                              // starts the request
const isAvailable = await checkIsAvailable(id)
return isAvailable ? <Item id={id} /> : null // reuses it

This only works if matching calls deduplicate: fetch does automatically, an ORM function needs React.cache, and under Cache Components a use cache function qualifies as well.

What the video covers

Stav’s walkthrough is a general Next.js caching video; this lesson clips the 20 to 420 second range, which his chapters label as request memoization explained, a visual flow, memoization details (duration, revalidation, opting out), and a code demo. It does not cover parallel fetching or React.cache, which is why those sections are written here. The video was recorded in May 2025, before Cache Components, so treat it as a clear picture of memoization itself, not of Next.js 16’s caching as a whole.

Where this fits

Memoization is the smallest of several caches. Next: the four caches, where it sits next to the others. The React primitive behind much of this is covered in the use hook.

Check yourself

  1. 01Two Server Components on one page call the same fetch URL. How many network requests go out, and why?

    One. Identical fetch GET requests with the same URL and options are memoized during a single server render pass, so the second call reuses the first one's result.

  2. 02How long does request memoization last?

    For a single render pass of a single request only. A later request starts with nothing memoized, which is what separates it from persistent caching.

  3. 03Why is await getArtist() followed by await getAlbums() slower than starting both and using Promise.all?

    The second request does not begin until the first one finishes, so the delays add up, while starting both first lets them run at the same time.

  4. 04Which tool deduplicates a Prisma query called from both a page and generateMetadata?

    React.cache, because automatic memoization only covers fetch and a database function has to opt in.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark