craftcms / craftcms/cms

[4.x]: DB Replica

Open
#15,305 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug craft4
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

What happened?
Description

I am trying to set up DB replica on one of our websites and I am not having much luck. It used to work fine on Craft 3 but after an upgrade to Craft 4, I am getting memory exhausted error and the website won't load at all.

PHP memory_limit is set to 512M but no matter what I change it to, it has no effect on the issues I am having.

This is my app.php config

<?php

use craft\helpers\App;

return [
    'modules' => [
        'my-module' => \modules\Module::class,
    ],
    'components' => [
        'cache' => craft\cache\DbCache::class,
        'session' => function() {
            // Get the default component config
            $config = craft\helpers\App::sessionConfig();

            // Override the class to use DB session class
            $config['class'] = yii\web\DbSession::class;

            // Set the session table name
            $config['sessionTable'] = craft\db\Table::PHPSESSIONS;

            // Instantiate and return it
            return Craft::createObject($config);
        },
        'queue' => [
            'ttr' => 900
        ],
        'db' => function() {
            // Get the default component config (using values in `db.php`):
            $config = App::dbConfig();

            // Define the default config for replica connections:
            $config['replicaConfig'] = [
                'username' => App::env('DB_REPLICA_USER'),
                'password' => App::env('DB_REPLICA_PASSWORD'),
                'tablePrefix' => App::env('DB_TABLE_PREFIX'),
                'attributes' => [
                    // Use a smaller connection timeout
                    PDO::ATTR_TIMEOUT => 10,
                ],
                'charset' => 'utf8',
            ];

            // Define the replica connections, with unique DSNs:
            $config['replicas'] = [
                ['dsn' => App::env('DB_REPLICA_DSN_1')],
            ];

            // Instantiate and return the configuration object:
            return Craft::createObject($config);
        }
    ],
    'bootstrap' => ['my-module'],
];
Steps to reproduce
  1. Use the above config on Craft 4 to configure the replica
Expected behavior

Website loads up without any issues

Actual behavior

Website is fully blank and the following error can be found in the phperrors.log file.

[05-Jul-2024 09:50:58 UTC] PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /Users/user/Documents/sites/website/vendor/yiisoft/yii2/db/Query.php on line 708
[05-Jul-2024 09:50:58 UTC] PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /Users/user/Documents/sites/website/vendor/yiisoft/yii2/web/Response.php on line 818

Craft CMS version

4.10.2

PHP version

8.3

Operating system and version

Mac OS Sonoma 14.5 as well as Ubuntu 22.04 LTS

Database type and version

MySQL 8

Image driver and version

No response

Installed plugins and versions

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the app.php database component configuration and the phperrors.log output. Reproduce the Craft 4.10.2 setup, then inspect the referenced vendor/yiisoft/yii2/db/Query.php and vendor/yiisoft/yii2/web/Response.php locations. Done means the replica configuration no longer causes the site to fail with the reported memory errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.