Unverified Commit b01bb6c7 authored by Josh Story's avatar Josh Story Committed by GitHub

Craigandrews/ensure isr lru is written if requests collapsed (#93766)

Adopted from: #93699

## Summary

When multiple ISR revalidation invocations (different `x-invocation-id`)
arrive concurrently for the same path, the Batcher correctly
deduplicates them into a single render. The LRU is only written inside
`handleRevalidate`, and only the winning invocation executes this code.
Batched invocations return the shared promise directly and never write
to the LRU. Their follow-up requests miss the LRU and trigger a new
render, cascading under load.

Moved the minimal mode LRU write from `handleRevalidate` to `get()`,
after the Batcher resolves. Every caller now writes to the LRU under its
own invocation key, so follow-up requests always find a cache entry.

## Test Plan

- Unit test: concurrent invocations with different `invocationID` values
verify only one render executes and follow-up requests hit the LRU

---------
Co-authored-by: 's avatarCraig Andrews <craig.andrews@vercel.com>
parent eab3ab87
import ResponseCache from './index'
import { CachedRouteKind, type ResponseCacheEntry } from './types'
import { RouteKind } from '../route-kind'
import RenderResult from '../render-result'
import { HTML_CONTENT_TYPE_HEADER } from '../../lib/constants'
function mockIncrementalCache() {
return {
get: jest.fn().mockResolvedValue(null),
set: jest.fn().mockResolvedValue(undefined),
}
}
function makeCacheEntry(html: string): ResponseCacheEntry {
return {
value: {
kind: CachedRouteKind.APP_PAGE,
html: RenderResult.fromStatic(html, HTML_CONTENT_TYPE_HEADER),
rscData: Buffer.from('rsc-payload'),
postponed: undefined,
status: 200,
headers: undefined,
segmentData: undefined,
},
cacheControl: { revalidate: 60, expire: undefined },
}
}
describe('ResponseCache', () => {
describe('minimal mode LRU population for batched invocations', () => {
it('should populate LRU for all batched invocationIDs, not just the winner', async () => {
const cache = new ResponseCache(true)
const incrementalCache = mockIncrementalCache()
let renderCount = 0
let resolveRender: () => void
const renderStarted = new Promise<void>((r) => {
resolveRender = r
})
const responseGenerator = jest.fn(async () => {
renderCount++
if (renderCount === 1) {
resolveRender()
await new Promise((r) => setTimeout(r, 50))
}
return makeCacheEntry(`render-${renderCount}`)
})
const promiseA = cache.get('/test', responseGenerator, {
routeKind: RouteKind.APP_PAGE,
incrementalCache,
invocationID: 'invocation-a',
})
await renderStarted
const promiseB = cache.get('/test', responseGenerator, {
routeKind: RouteKind.APP_PAGE,
incrementalCache,
invocationID: 'invocation-b',
})
const [resultA, resultB] = await Promise.all([promiseA, promiseB])
expect(renderCount).toBe(1)
expect(resultA).not.toBeNull()
expect(resultB).not.toBeNull()
// Follow-up request for invocation-b should hit the LRU
const followUpB = await cache.get('/test', responseGenerator, {
routeKind: RouteKind.APP_PAGE,
incrementalCache,
invocationID: 'invocation-b',
})
expect(renderCount).toBe(1)
expect(followUpB).not.toBeNull()
})
it('should use TTL-based LRU when invocationID is absent', async () => {
const cache = new ResponseCache(true)
const incrementalCache = mockIncrementalCache()
let renderCount = 0
const responseGenerator = jest.fn(async () => {
renderCount++
return makeCacheEntry(`render-${renderCount}`)
})
await cache.get('/test', responseGenerator, {
routeKind: RouteKind.APP_PAGE,
incrementalCache,
})
const followUp = await cache.get('/test', responseGenerator, {
routeKind: RouteKind.APP_PAGE,
incrementalCache,
})
expect(renderCount).toBe(1)
expect(followUp).not.toBeNull()
})
})
})
...@@ -295,6 +295,14 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -295,6 +295,14 @@ export default class ResponseCache implements ResponseCacheBase {
} }
) )
if (this.minimal_mode && response?.cacheControl) {
const cacheKey = createCacheKey(key, invocationID)
this.cache.set(cacheKey, {
entry: response,
expiresAt: Date.now() + this.ttl,
})
}
return toResponseCacheEntry(response) return toResponseCacheEntry(response)
} }
...@@ -362,9 +370,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -362,9 +370,7 @@ export default class ResponseCache implements ResponseCacheBase {
context.isFallback, context.isFallback,
responseGenerator, responseGenerator,
previousIncrementalCacheEntry, previousIncrementalCacheEntry,
resolved, resolved
undefined,
context.invocationID
) )
// Handle null response // Handle null response
...@@ -417,8 +423,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -417,8 +423,7 @@ export default class ResponseCache implements ResponseCacheBase {
responseGenerator: ResponseGenerator, responseGenerator: ResponseGenerator,
previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null, previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null,
hasResolved: boolean, hasResolved: boolean,
waitUntil?: (prom: Promise<any>) => void, waitUntil?: (prom: Promise<any>) => void
invocationID?: string
) { ) {
return this.revalidateBatcher.batch(key, () => { return this.revalidateBatcher.batch(key, () => {
const promise = this.handleRevalidate( const promise = this.handleRevalidate(
...@@ -428,8 +433,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -428,8 +433,7 @@ export default class ResponseCache implements ResponseCacheBase {
isFallback, isFallback,
responseGenerator, responseGenerator,
previousIncrementalCacheEntry, previousIncrementalCacheEntry,
hasResolved, hasResolved
invocationID
) )
// We need to ensure background revalidates are passed to waitUntil. // We need to ensure background revalidates are passed to waitUntil.
...@@ -446,8 +450,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -446,8 +450,7 @@ export default class ResponseCache implements ResponseCacheBase {
isFallback: boolean, isFallback: boolean,
responseGenerator: ResponseGenerator, responseGenerator: ResponseGenerator,
previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null, previousIncrementalCacheEntry: IncrementalResponseCacheEntry | null,
hasResolved: boolean, hasResolved: boolean
invocationID: string | undefined
) { ) {
try { try {
// Generate the response cache entry using the response generator. // Generate the response cache entry using the response generator.
...@@ -467,25 +470,15 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -467,25 +470,15 @@ export default class ResponseCache implements ResponseCacheBase {
}) })
// We want to persist the result only if it has a cache control value // We want to persist the result only if it has a cache control value
// defined. // defined. The minimal mode LRU write is handled in get() so that
if (incrementalResponseCacheEntry.cacheControl) { // every caller — including batched invocations — populates the cache.
if (this.minimal_mode) { if (incrementalResponseCacheEntry.cacheControl && !this.minimal_mode) {
// Set TTL expiration for cache hit validation. Entries are validated
// by invocationID when available, with TTL as a fallback for providers
// that don't send x-invocation-id. Memory is managed by LRU eviction.
const cacheKey = createCacheKey(key, invocationID)
this.cache.set(cacheKey, {
entry: incrementalResponseCacheEntry,
expiresAt: Date.now() + this.ttl,
})
} else {
await incrementalCache.set(key, incrementalResponseCacheEntry.value, { await incrementalCache.set(key, incrementalResponseCacheEntry.value, {
cacheControl: incrementalResponseCacheEntry.cacheControl, cacheControl: incrementalResponseCacheEntry.cacheControl,
isRoutePPREnabled, isRoutePPREnabled,
isFallback, isFallback,
}) })
} }
}
return incrementalResponseCacheEntry return incrementalResponseCacheEntry
} catch (err) { } catch (err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment