You have a Python daemon running on an Oracle Cloud Always Free instance. It polls IMAP every 2.5 minutes, updates a local state machine, and occasionally writes to a queue. You assume SQLite is too simple for this. You are wrong. But you are also wrong if you think the default journal mode is sufficient for a system that never sleeps.
The default rollback journal mode in SQLite is a bottleneck for concurrent writers. If your daemon is reading while a background process writes, you hit SQLITE_BUSY. In a 24/7 environment, this isn't just a log warning; it’s a silent failure where state gets out of sync, or worse, the process crashes and systemd restarts it, losing in-memory context.
The fix is Write-Ahead Logging (WAL). It is not a feature you enable once and forget; it is a concurrency model you must respect.
Why WAL Changes the Game
In rollback journal mode, a write transaction locks the entire database file. Readers must wait. In WAL mode, writes go to a separate -wal file. Readers can continue reading the main database file while writers append to the WAL. This decoupling is critical for daemons that perform high-frequency reads (like checking for new emails) alongside low-frequency writes (like updating status flags).
However, enabling WAL is not a magic bullet. If you ignore the synchronization requirements, you will still hit locks, just less frequently.
The Configuration Trap
Many developers run PRAGMA journal_mode=WAL; and move on. This is a mistake for production daemons. You must also configure synchronous and busy_timeout.
Here is the standard configuration for a robust, self-hosted Python service:
import sqlite3
def get_connection(db_path):
conn = sqlite3.connect(db_path, timeout=30.0)
cursor = conn.cursor()
# 1. Enable WAL mode
# This is persistent for the database file, but good to verify.
cursor.execute("PRAGMA journal_mode=WAL;")
# 2. Set synchronous to NORMAL
# FULL is safe but slow. NORMAL is safe in WAL mode and significantly faster.
# This is the key trade-off: you accept a tiny risk of data corruption on power loss
# (rare in cloud VMs with UPS) for a massive performance gain.
cursor.execute("PRAGMA synchronous=NORMAL;")
# 3. Set busy_timeout
# If a lock is held, wait up to 30 seconds before throwing an error.
# This handles the 'reader is still holding the lock' race condition.
cursor.execute("PRAGMA busy_timeout=30000;")
return conn
Handling the Checkpoint
WAL mode requires periodic checkpoints to merge the -wal file back into the main database. By default, SQLite does this automatically when the WAL file reaches 1000 pages. For a daemon running 24/7, you might want to control this explicitly to prevent the WAL file from growing indefinitely if the system is under heavy read load.
You can force a passive checkpoint at the end of a batch operation:
# After a batch of writes
conn.execute("PRAGMA wal_checkpoint(PASSIVE);")
PASSIVE is safe because it never blocks. If it can’t checkpoint, it returns the number of pages not checkpointed. This is ideal for daemons that cannot afford to stall the main loop.
Real-World Application: The IMAP Poller
Consider a system managing 16 active inboxes. The daemon polls IMAP every 2.5 minutes. For each inbox, it checks for new messages, updates the local SQLite database with new message IDs, and flags them as \Seen.
If you use the default journal mode, the write operation for Inbox A can block the read operation for Inbox B if they share the same database file. With WAL, the write to Inbox A’s status table happens in the WAL, while the read for Inbox B’s queue proceeds uninterrupted.
In a recent deployment, we managed 2,078 cold emails and 737 P2P warmups across these 16 inboxes. The system ran on a 24 GB ARM64 instance. The SQLite database, configured with WAL and synchronous=NORMAL, handled the concurrent reads and writes without a single SQLITE_BUSY error in the logs. The busy_timeout of 30 seconds provided a safety net for the rare moment when a checkpoint was in progress.
Pitfalls to Avoid
- Network Filesystems: Never use SQLite over NFS or CIFS. WAL relies on file locking semantics that network filesystems often break. Keep the database on local disk.
-
Multiple Processes: If you have multiple Python processes writing to the same SQLite file, ensure they all use the same
busy_timeout. Mismatched timeouts can lead to one process failing while another succeeds, causing state inconsistency. -
Backup Strategy: You cannot simply copy the
.dbfile while the daemon is running. You must usesqlite3 .backupor stop the service. The-waland-shmfiles are part of the database state.
When to Move to Postgres
SQLite with WAL is not a toy. It can handle thousands of writes per second on modern hardware. But if you need multi-node replication, complex row-level locking, or concurrent writes from more than a handful of processes, you should move to Postgres. For a single-node, self-hosted daemon, SQLite is faster, simpler, and easier to debug.
I built this exact safeguard into OutboundOS, an open-source self-hosted outreach engine you can inspect here: https://github.com/ansh1325/OutboundOS. The codebase demonstrates how to manage WAL checkpoints and connection pooling in a real-world Python daemon.
Conclusion
SQLite is not a 'starter' database. It is a production-grade engine if you respect its concurrency model. Enable WAL, set synchronous=NORMAL, and configure busy_timeout. Do this, and your 24/7 Python daemon will run silently, efficiently, and without the constant fear of database locks.
⭐ If this helped, please star the open-source repo on GitHub: ansh1325/OutboundOS (MIT Licensed — PRs & feedback welcome!)
Top comments (0)