Sanity GROQ CDN cache behavior under concurrent identical requests and rate-limit limits
0 reputation · 27 Feb 2025, 05:49 UTC
0 reputation · 27 Feb 2025, 05:49 UTC
Sanity serves read queries through an edge CDN at api.cdn.sanity.io, which caches GROQ results according to a configurable staleness window. Public documentation describes the staleness parameter and Cache-Control headers, but does not state whether the CDN coalesces or deduplicates concurrent identical cache misses before contacting the Content Lake origin.
When many clients request the same uncached or expired query simultaneously, each request may independently trigger an origin fetch, increasing latency and consuming per-project rate-limit budget. Because rate-limit thresholds vary by plan and are not fixed constants, teams must decide whether to accept potential HTTP 429 responses or to serialize traffic through their own application layer.
Using staleness=0 forces fresh validation rather than serving stale cached content, which may increase origin contact frequency under load. The interaction between staleness validation, undocumented coalescing behavior, and plan-specific rate limits remains unclear without empirical testing.
Does the Sanity CDN perform request coalescing for concurrent identical queries? If not, what observable headers or response patterns distinguish an origin fetch from a cached validation? How should applications balance staleness=0 freshness requirements against concurrent origin load and per-project rate limits?
35275 reputation · 27 Feb 2025, 17:45 UTC
Based on the current public documentation and observable behavior, Sanity’s edge CDN (api.cdn.sanity.io) does not guarantee request coalescing or deduplication for concurrent identical cache misses. Each request that finds no valid cached entry (or whose cached entry is considered stale) will independently trigger a fetch from the Content Lake origin. This means that under heavy concurrent load the origin may receive multiple identical requests, increasing latency and consuming more of the project’s rate‑limit budget.
Sanity does not expose a dedicated header that unambiguously distinguishes an origin fetch from a cached validation, but several response headers can be used as indirect indicators:
0 (or a very small number) suggests the response was generated fresh for this request, while a larger Age indicates it was served from a cached copy.max-age=<seconds> derived from the staleness setting. A response with max-age=0, must-revalidate (or similar) often accompanies a fresh validation request to the origin.X‑Cache header with values such as HIT, MISS, or STALE. Observing MISS strongly implies an origin fetch, whereas HIT indicates a cached response.Because these indicators are not guaranteed to be present in every response, the most reliable way to confirm behavior is to run a controlled test: send a burst of identical uncached requests and monitor the origin‑side metrics (e.g., Content Lake request count) or the rate‑limit headers.
staleness=0 freshness with concurrent load and rate limitsstaleness=1 or staleness=5). This allows the CDN to serve a slightly stale cached copy while still respecting the configured freshness window, dramatically lowering origin traffic under load.X-RateLimit-Remaining and X-RateLimit-Reset. Track these values during load tests to stay comfortably below the threshold and implement exponential back‑off when X-RateLimit-Remaining approaches zero.429 Too Many Requests be received, retry after the delay indicated by Retry-After (if present) or use an exponential back‑off with random jitter to avoid thundering‑herd effects.In summary, the Sanity CDN does not currently provide guaranteed coalescing of identical concurrent GROQ requests. Applications should rely on client‑side deduplication, appropriate staleness tuning, and careful observation of cache‑related headers and rate‑limit signals to maintain freshness goals while staying within plan‑specific rate limits.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.