It involves the automatic storage of certain query results. When the same query is executed again, the DB can serve results from the cache, instead of performing re-query.

Benefit of Transparent Query Caching 🔍

  • Speed: Frequently executed queries return results faster as they are fetched from the cache.
  • Reduced Load: Reduces the workload on the DB engine.

Appropriate Use-cases 🎯

This caching mechanism shines in read-heavy applications where:

  • The same queries are executed frequently.
  • Data doesn’t change often, minimizing cache invalidations.

MySQL’s Historical Use of Transparent Query Caching ⚖️

MySQL employed a memory-based structure for caching. When a SELECT query was made, MySQL would first inspect this cache. If the result was present, it was returned immediately; if not, the query proceeded as usual and might subsequently be cached.

Deprecation of MySQL’s Query Cache and Reasons ❌

MySQL deprecated its query cache starting from version 8.0 due to:

  • Contention Issues: A global mutex lock during cache access caused bottlenecks in high concurrency environments.
  • Invalidation Overhead: Frequent data updates could lead to the cache being invalidated and repopulated regularly, negating its benefits.

Leveraging External Caches and Their Advantages 🔧

Since MySQL removed out of the box support to transparent caching. Devs can use tools like Redis and Memcached, they even overcome the MySQL’s pitfalls:

  • Granularity: Devs have control over cache content and invalidation, rather than broad automatic invalidations.
  • Optimized Locking Mechanisms: Designed with high-concurrency in mind, avoiding global mutex contention issues.
  • Flexible Invalidation: Rely on strategies like Time-To-Live (TTL) or custom logic, providing better control over cache lifespan.

<
Previous Post
ELI5 Kafka’s Consumer Group Protocol
>
Next Post
Beyond Burnout: A Guide to Purposeful Unwinding