The database is 44 KB. Eleven pages of 4096 bytes. I checked it twice before I believed the number.
That file holds the purchase and sales ledger of a small trading business: nine tables, 31 products, 30 purchase records, 10 receipts, 38 line items. The app on top is 625 lines of Flask, ten routes, 22 write statements, fifteen commit() calls. There is no explicit index anywhere in the schema. It has run since September for four people over office WiFi. I have been telling people this design is enough without ever putting a stopwatch on it.
So I did. Everything below comes from copies of that file on one Windows laptop, Python 3.13.14, SQLite 3.53.1.
The question was narrow. When one person opens the report page, does everyone else's save wait? The answer is yes, and the number is worse than I guessed.
The cost of one open report
Eight threads, each inserting a single row and committing, twenty times. One extra thread held a read transaction for two seconds and ran the group-by query that sits behind the reports page. Busy timeout was five seconds, which is what sqlite3.connect gives you when you do not ask for anything.
| Journal mode | Timeout | Failed writes | p50 | p95 | Worst |
|---|---|---|---|---|---|
| delete (current) | 5 s | 0 of 160 | 3.5 ms | 5.6 ms | 2736 ms |
| delete (current) | 1 s | 112 of 160 | 1391 ms | 1684 ms | 1831 ms |
| WAL | 5 s | 0 of 160 | 17.5 ms | 97 ms | 410 ms |
| WAL | 1 s | 0 of 160 | 16.7 ms | 90 ms | 388 ms |
Read the worst column first. In the current setup a save waits 2.7 seconds for a report to finish, and 2.7 seconds is exactly how long the report takes. Had I ever shortened the timeout, 112 of those 160 saves would have failed outright with database is locked. Nobody has complained loudly enough for me to connect the two facts.
The mechanism is not mysterious. With journal_mode=delete a reader holds a shared lock and a writer needs an exclusive one, so they take turns at the file level. WAL lets readers and the single writer work on separate regions, and the same two-second report then costs a writer 410 ms instead of 2736 ms.
That is the real finding, and it is uncomfortable. The five-second default timeout is the only thing keeping this app from throwing errors in production. It keeps them quiet by making people wait.
The number that made me doubt the test
My first benchmark, run before the reader thread existed, said WAL was six times slower. Median write latency went from 3.7 ms to 25.9 ms at sixteen threads, and I nearly wrote that down as "WAL is not worth it here".
That was my mistake, and it cost an evening. The app opens a connection per request, g.conn in a before_request hook and closed in teardown. My benchmark copied that habit. Each insert paid for connect, WAL header work, commit, and then, when the connection closed, a checkpoint of the write-ahead log.
Give each thread one connection and reuse it for forty inserts and the ordering flips:
| Journal mode | New connection per insert | Reused connection |
|---|---|---|
| delete | 5.3 ms | 3.8 ms |
| WAL | 15.2 ms | 0.7 ms |
Same file, same machine, same hour. The whole gap lives in who pays for the checkpoint. So WAL is right for these writes, and connect-per-request is a separate tax that WAL makes visible. I had been measuring my own request pattern and blaming the storage engine.
Then the fix broke the backup
backup.py is 39 lines. It calls shutil.copy2(erp.db, backup/erp_YYYYMMDD.db), keeps thirty days, and runs from start.bat before the server comes up. In rollback journal mode that is a defensible snapshot. I tried to break it today: twenty copies taken while four writers hammered the file, and PRAGMA integrity_check came back clean on all twenty.
Switch to WAL and that same line becomes a lie. I committed 50 rows to a copy, held the connection open, and then copied the .db file the way the script does.
rows after 50 commits: 50 rows inside the copied .db: 0 rows inside VACUUM INTO output: 50 size of the -wal file: 412032 bytes All fifty rows were in the write-ahead log. The backup was 44 KB of stale pages, stamped with today's date. It would have passed every check I have, because a stale SQLite file is a perfectly valid SQLite file.
That is the part I want on the record. The latency fix and the backup script were each defensible alone. Together they turn a working backup into an empty one, and nothing in the logs would say so. My integrity_check test is what gave me false confidence, since I ran it in the mode where copying happens to work.
What I changed, and what it cost
Three lines in the connection helper, one index, one rewritten backup.
conn = sqlite3.connect(DB, timeout=15) conn.execute("PRAGMA journal_mode=WAL") conn.execute("PRAGMA synchronous=NORMAL") The journal mode is persistent per file, so the pragma only needs to run once, but running it on connect is cheap and survives a file being restored from an old backup. synchronous=NORMAL is what makes WAL worth having. FULL keeps the fsync on every commit and hands back most of the gain. The trade is that a power cut can cost the last few transactions instead of none. For a ledger on an office laptop with a thirty-day backup window I will take that. I would not take it for a payments table without saying so out loud.
For the index I loaded 99,538 line items into a copy, because the current 38 rows answer nothing.
38 rows: 200 runs in 10.1 ms 99538 rows: 5 runs in 206 ms (no index) 99538 rows: 5 runs in 76 ms (index on sale_items.sale_id) SQLite was quietly building an AUTOMATIC COVERING INDEX on sale_id once per execution. One CREATE INDEX idx_items_sale ON sale_items(sale_id) removed that, and the plan went from a scan plus automatic index to a plain index search. At today's volume the index buys nothing. The query cost tracks the table, and a file that needs three years to reach 100k rows still gets there.
Where the ceiling actually is
The database grew to 3.06 MB at 99,538 line items, so three years of this office is a rounding error on any disk. Size is not the constraint and will not be for a decade.
The constraint is the single writer. WAL lets readers run alongside it, and it does nothing about two writers. Two people pressing save in the same second still queue, and that queue is invisible until it exceeds the timeout. At maybe thirty writes a day, a queue of one happens rarely and a queue of two almost never.
So my line for staying on SQLite is a latency line, not a size line. If a save ever takes longer than a second at your real write rate, the storage engine is not your problem and has not been for a while. If saves sit under 100 ms and the worst case only shows up while the report is open, you are one pragma away from done. Moving to Postgres at that point would buy you a daemon, a port, a password file, and a backup story you have to write yourself.
What I have not tested
The phone. A colleague uses the mobile view over WiFi on a weak signal, and a connection that drops mid-request fails differently from a lock that waits. I have numbers for contention and none for flaky WiFi.
Nor do I know whether four waitress threads is the right number. Four is the default in waitress 3.0.2, and so are a connection limit of 100 and a 120 second channel timeout. I read those out of Adjustments() rather than from docs. At this volume it has never been the constraint, as far as I can tell. "As far as I can tell" is carrying a lot of weight in that sentence.
Do this one thing first
Before you touch journal mode, open your backup script and ask what it copies. If the answer is a plain file copy and you are about to enable WAL, replace the copy with VACUUM INTO. It takes a consistent snapshot of a live database in one line:
conn.execute("VACUUM INTO ?", (dst,)) Run that against a copy, commit fifty rows, keep the connection open, then compare row counts between the copy and the original. Matching counts means you are free to turn WAL on. An empty copy means you just found the bug that would have eaten a month of receipts, and you found it before it did.
Then measure your own report page. Mine was costing somebody 2.7 seconds per save, and I only know that because I stopped guessing.