Cache Rebuild Triggers Excessive Cache Invalidation Queries

Created on 7 December 2023, 7 months ago
Updated 14 December 2023, 7 months ago

Problem/Motivation

When running on an empty cache, Patternkit has to build up the collection data for available libraries and patterns. During this process, Patternkit iterates through all available library definitions and sets a value for caching based on what it finds for each one. Unfortunately, the default implementation of the CacheCollector class, which the LibraryNamespaceResolver service extends, triggers an invalidation every time a value is set or updated in memory. In practice, this results in every request running on an empty cache hitting the database with an update query like the one below for every library on the site.

UPDATE "cache_discovery" SET "expire"='1701904503'
WHERE "cid" IN ('patternkit.library_namespaces')

During my testing on a local environment using the standard install profile, this resulted in 200 of these update queries in a request with empty caches.

Steps to reproduce

  1. Install Drupal with the standard profile
  2. Enable the patternkit and patternkit_example modules
  3. Enable query logging in your database (Example)
  4. Clear all caches: drush cr
  5. Clear the query log for clarity (truncate if using a table, or empty the contents if logging to a file)
  6. Load any page in the browser
  7. Count the number of instances of this query in the log:
    SELECT COUNT(*) FROM mysql.general_log
    WHERE argument LIKE 'UPDATE "cache_discovery" SET "expire"=%
    WHERE "cid" IN (\'patternkit.library_namespaces\')';

Proposed resolution

Limit invalidation calls to only one per request by overriding the invalidateCache() method.

Remaining tasks

  • Add automated tests covering the issue and resolution
  • General regression testing

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

9.1

Component

Module Core

Created by

πŸ‡ΊπŸ‡ΈUnited States slucero Arkansas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024