phlib/db-helper-replication

DB helpers to complement phlib/db

0.3.1 2023-10-15 07:42 UTC

This package is auto-updated.

Last update: 2024-03-02 16:43:31 UTC


README

Code Checks Codecov Latest Stable Version Total Downloads Licence

DB helpers to complement phlib/db

This package, db-helper-replication, is split out from phlib/db-helper due to the additional process-control dependencies which users of the more typical helpers may not require or have available.

Installation

composer require phlib/db-helper-replication

Usage

Replication

The Replication helper monitors replica lag, which it stores in Memcache. This known lag can then be used to throttle long-running processes by introducing variable amounts of sleep.

Set up replica monitoring using the CLI script (you might consider using Monit to run this automatically):

./vendor/bin/db replication:monitor -c path/to/config.php -p /var/run/db-replication.pid -d start
$config = require 'path/to/config.php';
$replication = Replication::createFromConfig($config);

while ([...]) {
    [... some repetitive iteration, like writing thousands of records ...]
    
    $replication->throttle();
}

Your config file might look something like this:

<?php
$config = [
    // primary
    'host'     => '10.0.0.1',
    'username' => 'foo',
    'password' => 'bar',
    'replicas' => [
        [
            'host'     => '10.0.0.2',
            'username' => 'foo',
            'password' => 'bar',
        ],
    ],
    'storage' => [
        'class' => \Phlib\DbHelperReplication\Replication\Memcache::class,
        'args'  => [[]],
    ],
];

return $config;

License

This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.