Preventing the stampede: Request collapsing in the Vercel CDN
Blog post from Vercel
Vercel's introduction of request collapsing in the CDN aims to optimize Incremental Static Regeneration (ISR) in Next.js applications by minimizing redundant function invocations during cache expiration scenarios, known as "cache stampedes." When numerous users simultaneously request an expired route, request collapsing ensures only one function invocation per region occurs, while others wait for the cached response, thus conserving compute resources and preventing backend overload. The system automatically determines cacheability through framework integration, requiring no manual configuration. It employs a two-level distributed locking mechanism—at the node and regional levels—to coordinate requests efficiently, preventing a thundering herd problem. The process utilizes double-checked locking, checking the cache both before and after acquiring locks to avoid duplicate regenerations. Additionally, the system is resilient to failures, using timeouts to handle function errors and timeouts gracefully, ensuring continuous responses even under high traffic. In production, this feature significantly reduces unnecessary function calls and is automatically enabled for all Vercel projects using ISR.