← DeployCloud

Caching Strategies Guide

Caching is layers: the browser, the CDN edge, an application cache like Redis, and the database's own memory. Each layer trades freshness for speed at a different price.

Decide a staleness budget per data type before touching code β€” marketing pages can lag minutes, a shopping cart cannot lag at all. The budget tells you which layer is allowed to cache what.

HTTP and CDN caching

Static assets with hashed filenames get Cache-Control: public, max-age=31536000, immutable. Dynamic HTML and APIs use short s-maxage values plus stale-while-revalidate so the CDN serves instantly and refreshes in the background. Cloudflare caches static files by default and caches HTML or API responses when you opt in via cache rules; purge by URL is universal, while tag-based purge exists on Fastly and Cloudflare's enterprise tier. Send ETags so clients revalidate cheaply.

πŸ’¬ Chat with our AI β†’

Application caching with Redis and KV

The workhorse pattern is cache-aside: read the cache, miss, load from the database, write back with a TTL. Redis β€” Upstash for serverless, ElastiCache on AWS, Memorystore on GCP β€” gives strong consistency and rich structures for counters and leaderboards. Cloudflare KV is different: reads are fast at every edge, but writes propagate globally over tens of seconds, which suits config and feature flags, not anything transactional.

πŸ’¬ Chat with our AI β†’

Invalidation that actually works

Prefer boring TTLs over clever purging; most data tolerates thirty seconds of staleness. Where precision matters, use versioned keys β€” bump user:42:v7 to v8 on write and old entries expire naturally. Protect hot keys from stampedes with a single-flight lock or by serving stale while one worker refreshes. Never cache authenticated responses at a shared CDN unless they are keyed per user.

πŸ’¬ Chat with our AI β†’

What not to cache

Skip caching for money movements, inventory counts near zero, permission checks and anything feeding a write decision. A stale read that triggers a wrong write costs more than every millisecond caching ever saved. When in doubt, measure first: cache your three slowest queries, not everything.

πŸ’¬ Chat with our AI β†’

Frequently asked questions

Should I use Redis or Cloudflare KV?

Redis for consistency, counters and read-after-write needs. KV for read-heavy, rarely-written data like config, where seconds of propagation delay are harmless.

How long should TTLs be?

As long as your staleness budget allows: minutes for catalogs, seconds for dashboards, zero for balances. Start short and lengthen with evidence.

What is a cache stampede?

A hot key expires and hundreds of requests hit the database at once. Prevent it with single-flight locking or stale-while-revalidate.

Should I cache at the CDN or in the app?

Both, for different data: the CDN for shared public responses, the app cache for per-user or composed data. The layers complement rather than compete.

πŸ’¬ Chat with our AI β†’

πŸ’¬ Chat with our AI β†’

Generate a blueprint free β†’

Explore the full desk on the home page β†’