Skip to content

[6.x] Don't cache shared errors under their own URL as well - #15988

Open
ryanmitchell wants to merge 1 commit into
statamic:6.xfrom
ryanmitchell:fix/shared-errors-skip-per-url-cache
Open

ryanmitchell wants to merge 1 commit into
statamic:6.xfrom
ryanmitchell:fix/shared-errors-skip-per-url-cache

Conversation

@ryanmitchell

Copy link
Copy Markdown
Contributor

When share_errors is enabled, this stops the half measure static caching middleware from also caching each error under its own URL. Every error is served from the one shared copy anyway.

Why

With share_errors on, the first 404 is cached under /__shared-errors/{site}/404 and every later 404 is served from it. The middleware still caches each error under its real URL as well, though. So every distinct URL that 404s ends up in the cached URL index (AbstractCacher::cacheUrl()).

That index is one value per domain. Every cache write reads the whole thing, adds an entry and writes it back, while holding a single lock (withUrlsLock(), which blocks for up to 10 seconds). Nothing ever removes an error entry except a full static:clear, so the index grows with every unique bad URL.

That's normally slow and harmless. It stops being harmless when something requests endless unique URLs. We hit this on a production site. Some links had been entered without a leading slash (href="series/foo"), which a crawler resolved against the current page to build ever-deeper URLs (/product/1/series/foo/product/2/series/bar/...). Every 404 page contained the same links, so each one handed the crawler more URLs.

Within a few days the index held about 174,000 URLs (18MB serialized), 84% of them from that crawl. Every request that cached a page, error or not, was reading and rewriting 18MB under the shared lock. The web servers sat at 100% CPU with PHP-FPM out of workers, at modest traffic. The same thing can be caused by vulnerability scanners, or by any bot that appends query strings or junk paths.

With shared errors enabled, the per-URL copy buys very little: a repeat request to the same bad URL skips the controller. That isn't worth unbounded index growth, and every error is already served from the shared copy.

What changes

When share_errors is enabled and the response isn't successful, the middleware:

  • still prepares the response and caches it as the shared copy (copyError()), as before;
  • doesn't cache it under the requested URL, so it never enters the URL index;
  • doesn't persist the nocache session under the requested URL. It's still persisted under the shared-errors URL, which is how the shared copy restores its regions.

Nothing changes when share_errors is off: errors are still cached and tracked per URL, so it_caches_and_tracks_404s and the 404 invalidation tests are untouched.

One side effect: a page published at a URL that previously 404'd no longer needs that URL invalidated before it shows, because no 404 was cached there.

Tests

  • shared_errors_are_only_cached_under_the_shared_errors_url replaces nocache_session_is_written_under_the_real_url_for_shared_errors. It checks that two different erroring URLs (one requested twice) leave only the shared-errors URL in the index, that the nocache region still renders, and that the session is only stored under the shared-errors URL. It fails without the change.
  • Updated the comments in SharedErrorsStaticCachingTest that described repeat hits as "cached under its own URL". Those tests still pass, so repeat hits keep fresh CSRF tokens and nocache regions.

Not covered

The URL index itself still grows with distinct cached successful URLs, such as pages with many query string variations. That's a bigger design question (e.g. sharding the index, or not tracking per-URL keys at all) and is out of scope here.

🤖 Generated with Claude Code

With share_errors enabled every error is served from the shared copy, but the
middleware also cached each one under its requested URL. That added every
distinct erroring URL to the cached URL index, a single value rewritten under
a lock on every cache write, so it grew without bound when bots requested
endless unique URLs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant