Skip to content

Cache invalidation patterns — LSCache, Cloudflare API, Redis FLUSHDB

Every cache layer has its own invalidation primitive. Using FLUSHDB when you meant LSCache purge, or purging Cloudflare when LSCache still holds the stale copy, is the most common cause of "I deployed, why is the old version still showing".

After a deploy, customers see the old version of the page. Three caches are between them and the new code: Cloudflare at the edge, LSCache on the cPanel host, and Redis as the object cache. Each has its own purge mechanism, and purging the wrong one wastes time and confuses the customer. The right pattern is layer-specific and ordered: edge first, server cache next, object cache last. Skipping a layer means the next request rebuilds the cache from the stale layer below it.

What’s happening

The cache hierarchy on a typical cPanel WordPress site:

  1. Browser cache — controlled by Cache-Control headers, invalidated by changing the URL (hashed filenames) or by max-age=0 on HTML.
  2. CDN cache (Cloudflare/BunnyCDN) — purgable via API or dashboard. URL-level or wildcard.
  3. LSCache (LiteSpeed full-page cache) — purgable via plugin or via lscache_helper command. Tag-based.
  4. Object cache (Redis/Memcached) — purgable via wp cache flush, FLUSHDB (dangerous), or selective key deletion.
  5. OpCache — invalidated on file mtime change (if configured) or by opcache_reset() from a PHP script.
  6. Database query cache — implicit, invalidated on write.

A deploy that changes a CSS file but not its filename: invalidate browser cache by changing the URL (hash), then purge Cloudflare for that path, then purge LSCache, then optionally clear object cache if any plugin stored the asset URL there. A deploy that changes PHP logic: purge OpCache, restart PHP-FPM, then purge everything else.

How to verify

After a purge, the next request should be a miss everywhere up to the point where you successfully invalidated:

curl -I -H "Cache-Control: no-cache" https://example.com/page
# Look for: cf-cache-status: MISS, x-litespeed-cache: miss

curl -I https://example.com/page
# Second request: cf-cache-status: HIT (or DYNAMIC if uncacheable),
# x-litespeed-cache: hit

For Redis:

redis-cli --no-auth-warning INFO stats | grep keyspace_hits
redis-cli --no-auth-warning DBSIZE

After wp cache flush, DBSIZE on the WP-prefixed keys should drop. keyspace_hits rate goes to zero briefly, then rebuilds.

The fix

The deploy-time invalidation sequence we follow:

  1. Edge first. Purge Cloudflare via API: curl -X POST https://api.cloudflare.com/client/v4/zones/<zone>/purge_cache -H "Authorization: Bearer <token>" -d '{"files":["https://example.com/page1"]}'. Wildcard purge for big deploys: {"purge_everything": true}.
  2. Server full-page cache. On the cPanel host: wp litespeed-purge all (or selectively wp litespeed-purge tag <tag>). Or via the LSCache plugin’s purge button.
  3. Object cache. wp cache flush from the site root, or per-key deletion via redis-cli DEL <key>. Avoid FLUSHDB unless the Redis instance is single-tenant; on shared Redis it nukes everyone’s data.
  4. OpCache. If you changed PHP files in-place (no atomic deploy), call opcache_reset() via a one-off URL or restart PHP-FPM cleanly: whmapi1 restart_service service=apache_php_fpm.
  5. Verify. curl the affected URL with Cache-Control: no-cache and confirm fresh content. Then a normal curl should show a fresh cache entry repopulated from the new origin.

Document the invalidation steps in the deploy runbook — they are easy to skip when something goes wrong. Stack Harbor builds deploy + invalidation flows as part of deployment management.