Ryan Chandler

Enabling WAL mode with SQLite in Laravel

1 min read

SQLite's WAL (Write-ahead Logging) mode improves the reliability and performance of SQLite databases by changing where data is written to.

Instead of writing changes directly to the database.sqlite file, they instead get written to a separate log file first. That log file then gets merged into the main file in one go, allowing you to write without blocking reads from the database.

It's really easy to enable this mode in Laravel!

You just need to update the journal_mode configuration value for SQLite databases inside of the database.php config file, setting it to WAL.

return [

    //...
    
    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DB_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
            'busy_timeout' => null,
            'journal_mode' => 'WAL',
            'synchronous' => null,
        ],

        // ...

    ],

];

When Laravel creates the SQLite database connection, it will run PRAGMA journal_mode=WAL and all of your database writes will use the separate log file instead.