Skip to content

Commit a2ee3d2

Browse files
author
Giovani Guizzo
authored
feat: enable WAL mode for SQLite backend to improve concurrency and performance (#116)
1 parent 6da4093 commit a2ee3d2

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ $RECYCLE.BIN/
315315
# End of https://www.toptal.com/developers/gitignore/api/node,macos,osx,windows,linux,visualstudiocode,vim,emacs
316316

317317
*.sqlite
318+
*.sqlite-wal
319+
*.sqlite-shm
320+
*.sqlite-journal
318321

319322
.sidequest.config.json
320323
.turbo
321-
.vscode
324+
.vscode

packages/backends/sqlite/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,38 @@ backend: {
7575
- **Zero Configuration** - No database server setup required, just specify a file path
7676
- **File-Based Storage** - Self-contained database in a single file for easy deployment
7777
- **ACID Transactions** - Full transaction support for data integrity and safe job claiming
78+
- **WAL Mode** - Write-Ahead Logging enabled by default for better concurrent access
7879
- **Lightweight** - Minimal resource footprint, perfect for development and small deployments
7980
- **Migration Support** - Automatic database schema management with Knex.js migrations
8081
- **Portable** - Database files can be easily backed up, moved, or shared
8182

83+
## WAL Mode and Concurrency
84+
85+
The SQLite backend automatically enables **WAL (Write-Ahead Logging) mode** for improved concurrent access. This provides several benefits:
86+
87+
- **Better Concurrency**: Allows multiple readers and one writer simultaneously
88+
- **Reduced Lock Contention**: Minimizes `SQLITE_BUSY` errors during concurrent job processing
89+
- **Improved Performance**: Faster writes and better throughput for job queue operations
90+
- **Safer Transactions**: More reliable atomic operations for job claiming
91+
92+
### WAL Mode Files
93+
94+
When WAL mode is enabled, SQLite creates additional files alongside your main database:
95+
96+
- `sidequest.sqlite` - Main database file
97+
- `sidequest.sqlite-wal` - Write-ahead log file
98+
- `sidequest.sqlite-shm` - Shared memory index file
99+
100+
These files are managed automatically by SQLite and should not be manually edited or deleted.
101+
102+
### Limitations
103+
104+
While WAL mode significantly improves concurrency, SQLite is still not recommended for high-concurrency production deployments. For production use with multiple workers or distributed systems, consider using:
105+
106+
- **PostgreSQL** (`@sidequest/postgres-backend`) - Best for production
107+
- **MySQL** (`@sidequest/mysql-backend`) - Good for production
108+
- **MongoDB** (`@sidequest/mongo-backend`) - Alternative for production
109+
82110
## License
83111

84112
LGPL-3.0-or-later
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Migration to enable WAL (Write-Ahead Logging) mode for SQLite
3+
*
4+
* WAL mode provides better concurrency for job processing:
5+
* - Allows simultaneous readers and one writer
6+
* - Reduces SQLITE_BUSY errors
7+
* - Better performance for write-heavy workloads
8+
* - More predictable behavior under concurrent load
9+
*
10+
* Learn more: https://www.sqlite.org/wal.html
11+
*/
12+
13+
exports.up = async function(knex) {
14+
await knex.schema.raw('PRAGMA journal_mode = WAL;');
15+
await knex.schema.raw('PRAGMA busy_timeout = 5000;');
16+
await knex.schema.raw('PRAGMA cache_size = -20000;');
17+
await knex.schema.raw('PRAGMA temp_store = memory;');
18+
};
19+
20+
exports.down = function(knex) {
21+
// Revert to default rollback journal mode
22+
return knex.schema.raw('PRAGMA journal_mode = DELETE;')
23+
.then(() => knex.schema.raw('PRAGMA synchronous = FULL;'));
24+
};

0 commit comments

Comments
 (0)