Use cache not returning cached data in deployment

Hi,

I’ve got a function that runs two expensive SQL queries to fetch the data for a page. After migrating to Next 16 I want to make use of the “use cache” directive to cache the data for faster responses and reduced pressure on our db.

On a React server component it calls

const customer = await getCustomer(); // using cookies
const data = await getExpensiveData(customer, params)

and passes data to components.

params is a serializable object with filtering data (e.g. start date) used in the queries. We use query params to get the params.

The function looks like this

export async function getExpensiveData(
  customer: string,
  params: GetExpensiveDataParams
) {
  "use cache: private"; // tried without : private as well
  cacheTag(`expensive-data-${customer}`);
  cacheLife(CACHELIFE.EXPENSIVEDATA);

  // strongly simplified:
  const db = getDbForCustmer(customer)
  const data1 = await db.select().from(table1)
  const data2 = await db.select().from(table2)
  return mergeData(data1, data2)
}

with CACHELIFE.EXPENSIVEDATA being a constant defined in another file as:

{
    stale: 30, // we want to check for invalidation fast
    revalidate: 1 * 60 * 60, // we want to revalidate every hour
    expire: 2 * 60 * 60, // we want to expire the cache after 2 hours
}

When hitting the page on my dev server for the first time the loading time is long (e.g. 15s), subsequents requests to the page are much faster. However once the client cache becomes stale and the server is hit again, there seems to be no server cache implemented for the dev server so requests to the db are fired again.

However on Vercel deployments (preview or production) there is no cache whatsoever for this function. Every single reload takes the full 15 seconds.

Expected behavior: First request takes 15 seconds, next requests (without changed query params) are substantially faster.

Current behavior: Every request takes ~15 seconds and seems to hit the db.

I tried a bunch of different configurations, but after a few hours of playing around with it I wasn’t able to produce cache hits.

I’m not sure wether this is a bug, or I am hitting certain limitations I’m not aware of, or my mental model is just wrong.

My mental model of the “use cache” looks like this:

Since revalidate is set to an hour, I would expect cache hits.

This has been resolved.

Sam Selikoff helped me out. This specific scenario requires to use use cache: remote.