r/devops • u/Volcan300 • Jul 25 '26
Troubleshooting How to properly deal with a local sqlite database in a stateful node/sqlite webapp?
I'm building a simple web app with node/express that uses a local sqlite database. (I'm not experienced in devops, btw)
Although I've already managed to get this app running in production by doing a manual deploy via SSH and cloning my repo from github, I'm having issues understanding how I'm going to deal with the database (.db) file in production.
Since my database is running on the same debian VPS as the application, I'm starting to realize I need to be a little bit more thoughtful and stabilish some sort of process if I ever need to restore a db backup or do migrations on this database.
I though about leaving the db file outside the project directory, which should allow me to safely pull new code that I push to production while leaving the db file intact, (as long as I'm not changing db structure/doing migrations) but I'm not quite sure how to deal with backups, migrations and whatever scenarios might come down the line.
Do any of you have suggestions on how to deal with this sort of stateful local database scenario?
12
u/fletch3555 Lead DevOps Engineer Jul 25 '26
In my opinion, if you're using sqlite, it's not in production. There's a time and a place, and directly backing a web app is almost always not it. That said, we don't know anything about your apps architecture or what it's used for. I would highly recommend looking up sqlite pros/cons before continuing down this path.
3
u/Floss_Patrol_76 Jul 25 '26
sqlite is fine for a single-user prototype, the trap isnt concurrency, its backup/restore. dont just cp a live .db file, it can grab a half-written page and restore corrupt - use sqlite3 yourdb .backup out.db, or litestream if you want continuous off-box replication, and turn on WAL mode so reads dont block the writer. keep the db on its own path outside the repo like you planned, point the app at it with an env var, and run migrations as a versioned step in the deploy instead of by hand over ssh.
3
u/Zenin The best way to DevOps is being dragged kicking and screaming. Jul 25 '26
Consider looking at something like Flyway for handing DB migrations. Works fine with sqlite and if/when you upgrade to a real RDMS it'll work great there too. Easy to tie into your deployment workflow.
1
u/bitclaw_ 2d ago
I run a lot of these, one SQLite file per user in my case, and what bit me first was the database file sitting inside the folder the deploy replaces, so keep it in a data directory outside the app, on a disk that survives a redeploy, and if it runs in a container mount that directory as a volume or it's gone on the next deploy. Then turn on WAL mode and set a busy_timeout, I use 10 seconds, that way reads don't block the writer and a second write waits instead of failing with SQLITE_BUSY. SQLite has one writer at a time, so if you run several Node processes keep the write transactions short. For backups don't copy the file while the app is running, use VACUUM INTO or the backup API to write a copy and send that to S3 or R2, and restore it once on another machine so you know the backup actually works.
1
u/crashorbit Creating the legacy systems of tomorrow Jul 26 '26
Regardless of the database engine, schema and metadata changes are a pain. I've linked an article that might be helpful.
You always end up with scripts that migrate from version n to version n+1. Notice that database versions don't have to match code versions. It might even be better if they have different versions.
There are tools that can help but it's probably better to understand the problem space before figuring out how to manage it with a complex tool.
https://www.sqliteforum.com/p/managing-database-versions-and-migrations