Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
PHP Development

Making Things Faster Without Hiding Problems

Last updated:

Fix the cause before caching the symptom

Caching a slow query makes it fast until the cache misses, at which point the slowness reappears at the worst moment. It also makes the underlying problem invisible.

The order is always: find the slow thing, understand why, fix it if you can, and cache what remains genuinely expensive.

What is worth caching

  • Expensive aggregations that change infrequently
  • Configuration and reference data
  • Third-party API responses with a sensible lifetime
  • Rendered fragments that are costly and change rarely
  • Computed values with a clear invalidation trigger

Invalidation is the hard part

  1. Decide when each cached item becomes stale, before caching it
  2. Prefer event-based invalidation over time-based where correctness matters
  3. Use short lifetimes where staleness is acceptable and invalidation is awkward
  4. Never cache anything user-specific under a shared key

The classic bug is a cache keyed by question rather than by user, so one person sees another's data. It is easy to introduce and embarrassing to discover.

Cache layers

LayerUse for
Application cacheComputed values, configuration
Query result cacheExpensive aggregations
Fragment cacheCostly rendered sections
Full page cachePublic pages only
HTTP cache headersStatic assets

Know what happens when it is empty

A cold cache after a deployment or a restart means every request does the expensive work at once. On a busy system that can be enough to bring it down.

Either warm the cache after deployment, or make sure the uncached path is survivable. Discovering this during a restart at a busy moment is unpleasant.

Frequently asked questions

Should we cache database queries?

After fixing the slow ones. A properly indexed query is usually fast enough not to need caching.

Which cache backend?

Redis for most business applications. The file system is adequate at very small scale and does not scale across servers.

How long should things be cached?

As long as staleness is acceptable. Decide that per item rather than applying one lifetime everywhere.

What about caching user-specific data?

Key it by user, always. Shared keys for user-specific content is the classic and serious mistake.

Keep reading

Adding caching to make a slow application faster?

Check the queries first. Caching a query problem defers it rather than solving it.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Web DevelopmentCustom Software Development