Skip to content

Conversation

@mathiasgrimm
Copy link
Contributor

@mathiasgrimm mathiasgrimm commented Jan 21, 2025

Similar to #54231

When using Cache with a "database" driver that uses a read/write config, it will use the "read" connection unless DB_CACHE_CONNECTION=mysql::write is used.

If the read database is lagging behind the write database, it can cause issues, especially for locks.

To test it we can use the following:

// .env
CACHE_STORE=database

// routes/web.php
Route::get('/test-cache-connection', function () {
    \Illuminate\Support\Facades\Cache::has('some-key');
});

// config/database.php
        'mysql' => [
            'read' => [
                'host' => [
                    // If you see this error means it's using the "read" connection.
                    // Invalid Host. Fails with: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for some.invalid.host.invalid failed...
                    'some.invalid.host.invalid',
                ],
            ],

            'write' => [
                'host' => [
                    // Valid Host
                    '127.0.0.1',
                ],
            ],
            // 'host' => env('DB_HOST', '127.0.0.1'),
            // ...

mathiasgrimm and others added 2 commits January 21, 2025 09:45
When using Cache with a "database" driver that uses a read/write config, it will use the "read" connection
unless DB_CACHE_CONNECTION=mysql::write is used.

If the read database is lagging behind the write database, it can cause issues, especially for locks.

To test it we can use the following:

```php
// .env
CACHE_STORE=database

// routes/web.php
Route::get('/test-cache-connection', function () {
    \Illuminate\Support\Facades\Cache::has('some-key');
});

// config/database.php
        'mysql' => [
            'read' => [
                'host' => [
                    // If you see this error means it's using the "read" connection.
                    // Invalid Host. Fails with: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for some.invalid.host.invalid failed...
                    'some.invalid.host.invalid',
                ],
            ],

            'write' => [
                'host' => [
                    // Valid Host
                    '127.0.0.1',
                ],
            ],
            // 'host' => env('DB_HOST', '127.0.0.1'),
            // ...

```
When using Cache with a "database" driver that uses a read/write config, it will use the "read" connection
unless `DB_CACHE_CONNECTION=mysql::write` is used.

If the read database is lagging behind the write database, it can cause issues, especially for locks.

To test it we can use the following:

```php
// .env
CACHE_STORE=database

// routes/web.php
Route::get('/test-cache-connection', function () {
    \Illuminate\Support\Facades\Cache::has('some-key');
});

// config/database.php
        'mysql' => [
            'read' => [
                'host' => [
                    // If you see this error means it's using the "read" connection.
                    // Invalid Host. Fails with: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for some.invalid.host.invalid failed...
                    'some.invalid.host.invalid',
                ],
            ],

            'write' => [
                'host' => [
                    // Valid Host
                    '127.0.0.1',
                ],
            ],
            // 'host' => env('DB_HOST', '127.0.0.1'),
            // ...

```
…connection' into fix-database-cache-driver-write-connection

# Conflicts:
#	src/Illuminate/Cache/DatabaseStore.php
@taylorotwell
Copy link
Member

Hmm, I dunno about this one. It actually does seem useful to use read replicas in front of a database powered cache?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants