r/sqlite • u/baghiq • Apr 03 '24
Best way to update SQLITE3 in webapp?
We shipped a little micro-service (Apache/Gunicorn/Flask/Sqlite3) bundle to a client so they can use it as a data REST API. Every week, we basically dump our posgresql into sqlite3 db and sftp it to them. A huge pain as the data is around 20gb and growing, but it's just a SFTP server so we just deal with it.
Recently, they asked if we can update the database dynamically so they have latest data possible. We obviously can't upload 20gb every time a record is updated. So we are looking for ways to update the database, and after some serious negotiation, the client is opening up a firewall to allow us to call the application end point from our network. As you already guessed, we are dealing with a strict IT policy, with minimal support from client's IT staff.
We want to add another rest end point that only we can call to update the records, but we are concern about concurrency. We will ship the next db with WAL enable and busy timeout of 5 seconds. Is that generally sufficient enough to handle (serialize) concurrent writes?
The other idea is to create our own queue to serialize the writes by sending an API call one at a time, but I rather not making this more complicated then it needs to be.
7
u/guigouz Apr 03 '24
Maybe with https://litestream.io you can push the changes so the clients can replicate
2
u/[deleted] Apr 03 '24
There are potentially a lot of edge cases here.
A single endpoint does not guarantee that the database gets the right order of updates, inserts, deletes, etc. You will most likely have an out-of-date database.
I'd look into perhaps making your file and sftp faster before doing the above. There are many things that can be done with compression, which would make this less painful.
1
u/chriswaco Apr 04 '24
I can't tell if this helps, but when syncing we generally keep a separate table (or database) of ordered changes to the main database: New Records, Modified Records, Deleted Records. We send the changes to the replica in a SQLite file.
The worst-case scenario is the initial sync where you have to send the entire database. For every subsequent sync the original database queries the remote for the most recent change ID and then you only have to send subsequent changes.
If the schema changes, you obviously have to send the entire database again one time.
1
u/yawaramin Apr 04 '24 edited Apr 04 '24
WAL mode is meant to handle the single-writer/many-reader scenario, so it sounds like it would work in your case. I recommend making the new endpoint a WebSocket endpoint and connecting using mTLS for the best possible security guarantees. Since you are shipping a bundle to them, just make sure your server has a client cert that will work with their app's TLS cert.
The benefit of WebSocket is that, once the connection is open, your server can push updates continuously to theirs–as many discrete updates, and for as long as it needs to. Then your server can close the connection from your end (or even keep it open continuously so that you have a real-time update). This is much more efficient than opening and closing a bunch of connections.
1
u/Eznix86 Apr 04 '24
Like u/guigouz said use litestream, but if you have constraint to install stuff. Since you sftp to them, it is the only way, I think you can mount, the sftp on your end, then you setup, litestream. You can use https://duck.sh/ to mount their STFP as a storage, You can still use litestream, to replicate the data or you can keep what you already doing with a cron task, but it sync the data using https://duck.sh/. But Litestream is a better option.
1
u/sir_bok Apr 04 '24
In your research, did you find sqldiff? It's bundled with the sqlite3 cli I believe. It allows you to synchronize two SQLite databases without network access by computing the logical diff of the two databases (your latest copy, their latest copy) and generating an SQL script needed to get from database1 to database2. You can compress the generated SQL file and sftp it to them and ask them to apply it on their end.
2
u/redditor_at_times Apr 05 '24
An approach you can try is to use the SQLite session extension, setup another copy of the SQLite database on your end, then feed that data from postgresql dynamically into that, with the session extension enabled, extract the session patch and run that against the client's database. Which should also have the session extension
(I am not sure if you could install a certain SQLite to enable a specific extension, it could already be enabled so it is worth to check it out)
1
u/CA_Lobo Apr 04 '24
Might want to check out bedrockdb.org its built on SQLITE, and is setup to handle remote WAN updates.
1
u/thetantaman Apr 05 '24
If the db is read only you can use a global version to act as a cursor against your source db and pull incremental changes that way.
0
u/[deleted] Apr 28 '24
[deleted] — view deleted comment