← Back to articles
Performance & Databases

Caching is More Than Just Redis & Database Queries. A Complete Performance Map.

A complete map of caching layers in modern applications: browser cache, full page cache, opcode caching, fragment caching, Redis, MySQL query cache, and aggregate tables.

✦
Featured article ↗

Caching is More Than Just Redis & Database Queries. A Complete Performance Map.

I came across a deep dive on caching types today, and it inspired me to structure my own knowledge. We often think of caching as Redis or a database query cache. But in reality, it's an entire ecosystem operating at every level of an application!

So, what is a cache? Simply put, a cache is an intermediate buffer with fast access where data is stored. Its core mission is to enable work with large volumes of data in the shortest time possible, using limited resources, be it server power or user bandwidth.

Let's break down where these "accelerators" live.

🖥️ 1. Client-Side Caching (Browser Cache)

This is where we save on traffic and latency between the user and the server. The browser is instructed to use a local copy, and the server responds with a 304 Not Modified status.

  • Caching Files & Images: Perfect for media-rich sites. Headers like Expires and Cache-Control tell the browser to store resources for weeks. The catch: if a file changes, the user might not see the update immediately.
  • HTTPS Caching: Security headers (e.g., Strict-Transport-Security) force the browser to always use HTTPS for your domain.
  • Certificate Authority (CA) Caching: Stores the "stamp" of trust for your SSL certificate, so the browser doesn't have to validate it with the CA on every visit.
  • Page Caching: The most intelligent type. Using headers like ETag and Last-Modified, the server checks if a page has changed since the user's last visit. If not, it sends a 304, and the browser loads the local copy.

The Bottom Line: Browser caching saves traffic and speeds up return visits, but to prime it, a user must visit the site first. So, while it helps, it doesn't dramatically reduce the initial server load.

🚀 2. Server-Side Caching

Here, data is stored on the server and works on a "one-to-many" principle. This is our main artillery in the battle for performance.

  • 2.1. Full Page Caching: The most effective method. A fully rendered HTML page is served from memory instantly, allowing even a modest server to handle millions of requests. Ideal for guest users where content isn't personalized. The downside: not suitable for user-specific pages. Often used with a TTL (e.g., from 2 minutes).
  • 2.2. PHP Opcode Caching: (OPcache, APC). This caches the compiled script bytecode, so it doesn't need to be compiled on every request. Crucially reduces CPU load.
  • 2.3. Partial (Fragment) Caching: The most complex yet powerful tool. It allows you to cache dynamic parts of a page (e.g., a sidebar, product list) with dependencies on database tables, user sessions, or GET parameters. The key benefit: drastically reducing database queries for authenticated users. Scaling a pool of application servers is easier than scaling a database cluster.
  • 2.4 / 2.5. In-Memory Data Caching (Redis, Memcached): This is where serialized data lives: results of heavy computations, configuration files, system state. This is our primary tool for caching application data, not just rendered HTML.
  • 2.6. MySQL Query Cache: A classic, but with caveats. Queries using functions like UNIX_TIMESTAMP() constantly invalidate the cache. The solution is to use conditions that change less frequently to allow adjacent queries to be cached.
  • 2.7. Caching via Aggregate/Materialized Views: The golden rule: "Reads should significantly outnumber writes." If data changes infrequently but is read often, pre-calculate it and store it in a summary table.

🗺️ Summary: It's All About Strategy

Effective caching isn't a single silver bullet; it's a multi-layered defense. A well-built strategy looks like a set of Russian dolls: CDN -> Web Server (Full Page Cache) -> Backend (Fragment Cache) -> In-Memory Cache (Redis) -> Database Cache -> Aggregate Tables.

Understanding which tool to use at which level is the true art of building fast and responsive systems.

Technologies & topics

Article tags

No articles match these filters.

Have a project or an idea to discuss?

Let's talk ↗