ReplicaKillSwitch::trigger() kills page cache for five minutes

Created on 16 February 2021, almost 4 years ago
Updated 30 April 2024, 8 months ago

Problem/Motivation

If a database "slave" is declared, ReplicaKillSwitch::trigger() is frequently called after a writing is done on the master (for example, from SqlContentEntityStorage::delete()).
This function starts a five minutes session, hence the session cookie kills page cache (and Varnish cache), cancelling the optimization.

Steps to reproduce

  1. Setup a slave database
  2. Enable page cache or add a Varnish server in front of the drupal server
  3. Post a comment as a front user
  4. Check that page cache is disabled

Proposed resolution

Use another mechanism to store this status.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇫🇷France O'Briat Nantes

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Session is still used in Drupal 10, in ReplicaKillSwitch::trigger(). If this needs to be fixed, it needs to be fixed in the most recent Drupal version, first.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇫🇷France O'Briat Nantes

    Whoa, 3 years ago I noticed this strange behavior on an old Drupal 7, I never checked on 8+ version.

    If nothing changed since 7, there's room for improvement in db performance, almost no read requests use the slave db.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    In Drupal 10, ReplicaKillSwitch::trigger() is called from SqlContentEntityStorage, which means that every time a content entity is deleted, restored, or saved, the session will be populated with that value.
    api.drupal.org does not show other references to database.replica_kill_switch, which should mean those are the only methods/functions that call ReplicaKillSwitch::trigger().

  • 🇫🇷France O'Briat Nantes

    If I recall it correctly (and it's the same Drupal 7 behavior), very few core mechanisms make usage of slave reading (comments ?).

    So what's the point to keep this api if it's only legacy code that nobody uses

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Using git grep --fixed-strings 'replica' -- '*.php', I found references to the database replica in the following files (leaving out the files containing test classes).

    • core/modules/comment/src/CommentStatistics.php
    • core/modules/node/src/Plugin/Search/NodeSearch.php
    • core/modules/search/src/Plugin/views/argument/Search.php
    • core/modules/search/src/SearchIndex.php
    • core/modules/tracker/src/Controller/TrackerController.php
    • core/modules/views/src/Plugin/views/query/Sql.php
  • 🇫🇷France O'Briat Nantes

    Nice command line (I take note).

    So what's the next move? Do you have a suggestion?

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    The next step would be to find another mechanism to use. Basing on that, a MR would be created to change the Drupal core code.

    I agree that using a session value is "quite unusual" in this case, as that value would be stored for the currently logged in user, while whenever to use the replica server should hold true for every logged in user. I could delete many entities on a site; that should stop the replica server from being used for every user who access the site after that, since the changes I caused still needs to be propagated to the replica server.

    A value stored using the keyvalue.expirable service could be used.

Production build 0.71.5 2024