Skip to content

LSCache stale-while-revalidate — serving fast while fetching fresh

Stale-while-revalidate lets LSCache serve an expired cache entry to the next visitor while it asynchronously fetches the fresh version. Here is when to enable it and the gotchas.

The classic cache trade-off is “fast but stale” vs “fresh but slow on cache miss”. Stale-while-revalidate (SWR) breaks the tradeoff: when a cache entry expires, the next request gets the stale copy instantly while LSCache fetches a fresh copy in the background. The visitor experience is fast cache hits all the time, and the cache stays nearly current. LSCache supports SWR through the LiteSpeed module — most teams never turn it on because they do not know it exists.

What’s happening

Normal full-page caching has two states for any URL: in cache (fresh) or not in cache (miss). A cache miss is slow because PHP/MySQL has to render the page. SWR adds a third state: in cache but expired. On a request for an expired entry, LSCache serves the stale page immediately and queues a background regeneration. The next request after a few seconds will get the fresh version. The first visitor after expiry gets stale content; the next gets fresh.

The tradeoff: the first visitor after expiry sees content that might be a few minutes old. For a marketing site, news site, or any content where minutes-old is fine, this is a great deal. For a logged-in user dashboard or an ecommerce cart, it is not — those should not be cached anyway.

How to verify

LSCache stale settings live in the plugin config or .htaccess:

grep -i stale /home/<user>/public_html/.htaccess
wp option get litespeed.conf.cache-ttl_default
wp option get litespeed.conf.cache-ttl_default_stale

For a live test: warm the cache, wait past TTL, request again with curl -v:

curl -s -I https://example.com/page
sleep 600  # past the cache TTL
curl -s -I https://example.com/page

The second response should still be fast (low TTFB), and a header like x-litespeed-cache-control should show the stale path was used. The third request a few seconds later should show the fresh re-rendered version.

The fix

  1. Set a normal TTL through the LSCache plugin > Cache TTL. For a marketing site, 1 hour to 1 day is typical.
  2. Enable stale TTL — in the plugin’s cache settings, set “Default Public Cache TTL Stale” to a value larger than the normal TTL (e.g., 2x). This is how long after expiry LSCache will still serve the stale copy while regenerating.
  3. Decide what is cacheable. Public pages: yes. Cart/checkout: no. WP admin: no. Logged-in user pages: usually no, or use ESI for the personalized fragments.
  4. Watch for stampede behavior. If many requests for the same expired URL arrive simultaneously, LSCache should serialize the regeneration. Check tail -f /usr/local/lsws/logs/access.log | grep <URL> during regen — only one origin hit per URL should appear.
  5. For high-traffic sites, also enable LSCache Crawler. The crawler proactively refreshes popular URLs before they expire, so most users never hit the stale path at all.

The combined effect: cache hit rate stays high, stale window is short, origin gets predictable load. Stack Harbor configures LSCache including SWR as part of cPanel server administration.