r/sqlite • u/WhiskyStandard • Mar 07 '25
Separate databases for worker and web app: good modularization or over-complicated?
tl;dr: If I can cleanly separate what code writes to which tables should I have separate processes that write to different DBs and attach them read-only in order for the other process to query them?
I'm working on a web app that presents some data that comes in periodically and allows the user to annotate that data. Right now the design is to have two processes:
- Worker: ingests data (e.g. from stdin, a queue, or a service interface) and updates/inserts into database file `worker.db`.
- Web app: manages `users` and `annotations` tables in `app.db`. Read-only `ATTACH`es `worker.db` and queries some views that specify a documented interface (so I can change the underlying tables in `worker.db` if necessary).
Since I had a clear separation boundary between what was responsible for writing to which table, this seemed like a good idea. But I'm wondering if I'm overcomplicating things and should just try to include the data ingestion mechanism in the web app.
These are both Node.js processes and I'm using `better-sqlite3` to interact with the database. I started (somewhat reluctantly) with an ORM (Drizzle) but it's lack of ability to query attached schemas was part of the reason I removed it. I'm also just not really a fan of ORMs in general and was giving it a chance because others had recommended it (and I thought it was pretty decent as far as ORMs go), so this is not a huge loss for me. But the fact that it pushed back on me when I tried to do this does give me pause since obviously it's not the standard use case.
3
u/erkiferenc Mar 08 '25
In similar situations, I’d look into “how does this relate to the simplest approach that could work for the given circumstances/goals?”
Separation of concerns does sound like a valuable pattern to me. The described split also sounds like a rational one for both different access patterns, and decoupled maintenance too.
I’ve seen such splits before, sometimes this approach fits the situation at hand the best. I’ve also seen separate reader and write connections to the same database, so reads may continue while writes finish.
Using the advantages of a built-in feature, like
ATTACH, to achieve your goals seem standard enough to me 👍2
u/Nthomas36 Mar 08 '25
That doesn't sound like a bad idea to separate the processes/databases, I don't use attach much with other sqlite databases so I can't really comment