# Request Memoization

> Deduplicating fetch calls during a single render.

Subject: Next.js | Lesson 22 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/request-memoization
Previous: [redirect & permanentRedirect](https://codewithanimation.in/nextjs/redirect-api.md) | Next: [The Data Cache](https://codewithanimation.in/nextjs/data-cache.md)

## The end of prop drilling

In traditional React, if your Header, Sidebar, and Main content all needed the same user data, you had to fetch it at the top of the tree and pass it down via props or Context.

With Request Memoization in the App Router, you can simply call `fetch('/api/user')` directly inside the Header, the Sidebar, and the Main content. Next.js will automatically deduplicate these identical requests.

## How it works

When the first component calls `fetch`, Next.js checks a short-lived memory cache. Since it's empty, the request goes to the external database.

The database responds, and Next.js saves the result in the memory cache. When the second and third components call the exact same `fetch`, Next.js intercepts them and instantly returns the cached data, completely bypassing the network.

## The lifespan of the cache

This is the most critical detail to memorize: Request Memoization only lasts for the duration of a *single* server render pass.

As soon as the page finishes rendering and the HTML is sent to the client, this cache is completely destroyed. It does not persist across different users, and it does not persist across multiple page reloads.

## Common mistake: Axios and Prisma

Next.js achieves this magic by overriding the native web `fetch` API. If you are using a third-party library like Axios, or a database ORM like Prisma, automatic memoization will NOT work.

If you call `prisma.user.findMany()` in three components, you will hit your database three times. To fix this, you must wrap your database calls in React's `cache()` function (`import { cache } from 'react'`).

## Tip: POST requests

Request memoization only applies to `GET` requests. If you execute a `POST`, `PUT`, or `DELETE` request, it will never be memoized, even if the URL and payload are identical.

## Try it: The Deduplication Engine

Click 'Render Page' to simulate a Next.js server render. Watch three isolated components request the exact same data. The first request will hit the database, but the remaining two will instantly resolve from the glowing React Memory Cache.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/request-memoization

## Check yourself

### Question 1

How long does Request Memoization cache your data?

- A. Until the next deployment
- B. For 30 seconds by default
- C. Only for the duration of a single server render pass
- D. Until the user closes their browser

Correct answer: C

Request Memoization is extremely short-lived. It exists solely to deduplicate fetches while a single page is being rendered on the server, and is destroyed immediately after.

- Why not A: That is the Full Route Cache, not Request Memoization.
- Why not B: Request Memoization is not time-based.
- Why not D: It is a server-side cache, it has no connection to the user's browser session.

### Question 2

You are using Prisma to query your database in two different Server Components. How do you ensure the database is only hit once?

- A. It happens automatically in Next.js 15
- B. Wrap the database query function with React's cache()
- C. Pass the data down as props from a parent layout
- D. Use the useMemo hook

Correct answer: B

Because Prisma does not use the native `fetch` API under the hood, you must manually opt-in to deduplication by wrapping your data-fetching function in `React.cache()`.

- Why not A: Next.js only automatically memoizes the native fetch() API.
- Why not C: While possible, it defeats the architectural benefit of Server Components.
- Why not D: useMemo is a Client Component hook for values, not a Server Component cache mechanism.

## Remember this

- You can confidently call the same `fetch` in multiple components without prop-drilling.
- The cache is destroyed immediately after the page finishes rendering.
- Only `GET` requests using the native `fetch` API are automatically memoized.
- Use `React.cache()` to memoize database ORMs (like Prisma, Drizzle) or Axios.
