# React cache()

> Memoize expensive database queries across Server Components.

Subject: Next.js | Lesson 30 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/react-cache
Previous: [Dynamic Rendering (Opting Out)](https://codewithanimation.in/nextjs/dynamic-rendering.md) | Next: [Server Actions (Forms)](https://codewithanimation.in/nextjs/server-actions-forms.md)

## Extending Request Memoization

We learned earlier that Next.js automatically memoizes `fetch()` calls. If five components `fetch()` the same URL, only one network request is made.

But what if you aren't using `fetch`? What if you are querying a database directly using Prisma, Drizzle, or a MongoDB client? React's `cache()` function extends request memoization to ANY async function.

## The Component Era Problem

In modern Next.js, the best practice is for components to fetch their own data rather than passing props down through a dozen layers (prop drilling).

But if `Layout.tsx`, `Header.tsx`, and `Profile.tsx` all need the current user's database record, querying the DB in all three places would be disastrous for performance. `cache()` solves this.

## Wrapping Your Queries

You import `cache` from React (not Next.js). You wrap your data access function: `const getUser = cache(async (id) => await db.user.find(id))`.

Now, you can call `getUser(1)` in as many Server Components as you want. The first component triggers the database query; the subsequent components instantly receive the cached result from memory.

## Common mistake: The Accidental DDOS

A very common pitfall for Next.js beginners is assuming ALL data fetching is automatically memoized.

A developer might write `await db.products.findMany()` in five different Server Components on the same page. Without wrapping it in `cache()`, the page load triggers five identical heavy database queries, severely degrading server performance.

## Tip: Single Request Lifecycle

Remember that React `cache()` only lives for the duration of a SINGLE server request. It clears itself immediately after the page is rendered.

It is completely distinct from the Next.js Data Cache, which persists data across multiple users and requests over time.

## Try it: The ORM Avalanche

Watch the render cycle of a page that queries a database without `fetch`. Notice how the un-cached version batters the database with redundant queries, while the `cache()` version resolves the first query and serves the rest from memory.

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

## Check yourself

### Question 1

When should you explicitly use React's `cache()` function?

- A. When you want to cache a standard fetch() call
- B. When you want to save data permanently in the browser
- C. When making direct database queries (like Prisma or SQL) in Server Components
- D. When you want to cache data across all users globally

Correct answer: C

Direct database queries or third-party SDK calls are not automatically memoized like `fetch`. You must wrap them in `cache()` to prevent duplicate DB hits during a single render.

- Why not A: fetch() is automatically memoized by Next.js.
- Why not B: That requires LocalStorage or IndexedDB.
- Why not D: React cache() only lasts for a single request.

### Question 2

How long does data stored in React's `cache()` persist?

- A. Until the user closes their browser
- B. Until the Next.js server restarts
- C. Until the revalidation timer expires
- D. Only for the duration of the current server request/render

Correct answer: D

`cache()` is strictly tied to a single request lifecycle (Request Memoization). Once the page finishes rendering and is sent to the client, the cache is destroyed.

- Why not A: cache() is a server-side mechanism.
- Why not B: That describes the Full Route Cache or Data Cache.
- Why not C: cache() does not have a revalidation timer.

## Remember this

- Next.js only auto-memoizes `fetch()` calls.
- Use React `cache()` to memoize direct database queries (Prisma, SQL, SDKs).
- It prevents duplicate work when multiple Server Components request the same data.
- `cache()` data only lives for the duration of a single HTTP request.
- Failing to use `cache()` with ORMs can lead to severe database overload.
