Out of the box, SQLite uses a rollback journal, and a write locks out readers for a moment. For a single script that’s fine. For an app where a web page reads while a background job writes, you get those database is locked errors. Write-ahead logging (WAL) is the fix SQLite itself offers, and switching is one line.
Open the database in the sqlite3 shell and run:
sqlite3 app.db "PRAGMA journal_mode=WAL;"
It answers wal when it worked. If it answers with the old mode, such as delete, the switch failed and nothing changed. The nice part is that WAL mode sticks: unlike the other journal modes, it is stored in the database file, so the app picks it up the next time it opens the file without any change to its code. You can check the current mode any time with PRAGMA journal_mode;.
In WAL mode, changes go into a separate log file first and are copied into the main file later during a checkpoint, which SQLite does automatically. Readers don’t block writers and a writer doesn’t block readers. There is still only one writer at a time, so it doesn’t turn SQLite into a server database, but for a small self-hosted app it often removes the lock errors.
Now the caveats, and they matter. WAL needs all processes to share memory on the same machine, so it does not work on a network file system such as an NFS or SMB share. Keep the database on a local disk. You’ll also see two extra files next to the database, app.db-wal and app.db-shm. Don’t delete them while something has the database open, and don’t back up only the .db file with cp, because recent changes can still be in the -wal file. Use the shell’s .backup command or stop the app first. Finally, if an app ships its own database settings (many containers do), check its docs before you change the mode under it.
Further reading: SQLite: Write-Ahead Logging, SQLite: PRAGMA journal_mode