Adam Cassar

Co-Founder

2 min read

Request collapsing protects busy origin servers that serve changing content. On a high traffic site, a cache miss after content expires can amplify one expired resource into many simultaneous origin requests. This behaviour is commonly called a cache stampede or dog-piling.

When request collapsing is enabled, only a single request is sent to an origin server for a given resource, then the resulting body is used to satisfy pending requests.

This can stop a popular cached resource from causing a flood of requests to an origin server when it expires. On high traffic sites, enabling the feature for the right resources can smooth request volume and traffic to origin servers.

How it works

Request collapsing is implemented internally using cache keys and queues. Cache keys are used as keys to a map, with client requests tracked using a queue. Secondary response keys are then used to match waiting client requests to origin responses. Matching requests can then be fulfilled using the same response.

Failed requests are retried in the same manner.

If a response to an active request is marked as private with Cache-Control: private or Set-Cookie, then all queued and future matching requests will go directly to the origin without trying to match them.

This implementation allows Peakhour to serialise requests to an origin, so it's important to enable the feature only on resources that you know will be cacheable, either through cache-control headers or Peakhour configuration.

How do you know its working?

Request collapsing can be verified with Debug enabled and the cache-status header in the response. A collapsed request looks like the example below, with 'collapsed' at the end.

cache-status: peakhour.io; fwd=uri-miss; key="https://website.com/home-eco.jpg"; ttl=86400; collapsed

How its used in practice?

Image optimisation

Peakhour.IO uses request collapsing internally for image transforms. The feature ensures we only transform a single image when a resource expires on the edge, reducing latency sensitive image transformation work, client transform costs and avoidable delays for end users.

Dynamic page caching

Request collapsing is a good fit when enabling caching of expensive dynamically written pages. Peakhour.IO can cache server heavy WordPress, Magento, PrestaShop, Drupal and other platforms via our caching plugin. The plugin keeps the CDN in sync by notifying us when content changes.

Content still needs to change during busy periods such as sales or item purchases. During these periods servers can be overwhelmed when pages incur a cache miss. Request collapsing can help smooth out this traffic and maintain response times for users.