Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Making Things Faster Without Hiding Problems
PHP Development

Making Things Faster Without Hiding Problems

Caching in PHP applications: fix slow queries first, then cache costly data that rarely changes, with invalidation designed in and a plan for a cold cache.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

Fix the queries first. Then cache the expensive things that change rarely, with invalidation designed in from the start rather than added later.

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.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

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

More on PHP Development

PHP Development

Why PHP Is Still a Sensible Choice

Is PHP still a good choice for business applications? Where modern, typed PHP fits, where another language is better, and the hosting and hiring arguments.

PHP Development

Improving Old Code Without a Rewrite

Modernising a legacy PHP application without a rewrite: move to a supported PHP version, add tests around what matters, then strangle rather than replace.

Start here

Adding caching to make a slow application faster?

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

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →