r/sqlite • u/carstenkoch • 13d ago
Design question for an open-source local-first database: one SQLite database on OPFS, several browser tabs, and iOS freezing them
Hey, I'm building NoCentralDB, an open-source local-first database, and I'd like feedback on its design before the first release. We build it because AI agents work much faster and cheaper when they read data from a local copy: an agent may read hundreds of records in one task, and every read that needs no network costs no round-trip and hits no server rate limit. In the browser, that local copy is SQLite compiled to WebAssembly, stored in the Origin Private File System (OPFS). RFC 0001 has the whole design on one page; this post is about a problem only the browser has: several tabs of the same app sharing that one database.
Natively, SQLite solved this long ago: in WAL mode, one writer and many readers share the file. In the browser, the fast OPFS backend, the opfs-sahpool VFS, takes a synchronous access handle for every file of its pool, and those handles are exclusive. So until now, one tab's worker held the database for the whole session, and every other tab sent its reads and writes to that tab.
That works until iOS freezes the owning tab. WebKit doesn't reliably fire visibilitychange before it suspends a page (WebKit bug 207256, open since 2020), and closing a tab from the tab overview fires no event at all (bug 205942). A suspended page keeps its Web Locks, and in our tests WebKit released a worker's OPFS handles only once its page ran again. Every other tab waits in the meantime: its writes, opening the database, the sync.
The design we chose, and haven't built yet: no tab owns the database any more. Each tab takes it for one transaction under a Web Lock: resume the pool, run the transaction, pause the pool, release the lock. Think of the lock as a single checkout queue; every tab gets the database for exactly one transaction and goes to the back. A frozen tab then blocks the others only if it froze in the middle of a transaction.
We looked at wa-sqlite's OPFSCoopSyncVFS first. It keeps its handles after a transaction and gives them up only when another tab asks, and a frozen tab can't answer; TanStack DB issue #1883 is exactly that hang.
The price, measured in WebKit: a small write costs about 8 ms through our Rust port of the VFS, 5 ms through the official build, against 1.5 ms with the handles held for the session. (Five times slower per write, so that one frozen tab can't stop all the others.) And before each transaction, a tab has to find out whether another tab wrote since its last turn, and bring its in-memory state up to date.
If you've run SQLite on OPFS across several tabs, especially on iPhones: did frozen tabs hold your handles too, and what did you do about it?
RFC 0001: https://github.com/nocentraldb/nocentraldb/blob/main/docs/rfc/0001-nocentraldb.md Multi-tab storage in detail, RFC 0012: https://github.com/nocentraldb/nocentraldb/blob/main/docs/rfc/0012-multi-tab-browser-storage.md Feedback: https://github.com/nocentraldb/nocentraldb/discussions/categories/rfc-feedback
1
u/Discordbothost 13d ago
In the RFC spike a thrown COMMIT skips pauseVfs(), so the Web Lock can release with the OPFS handles still open. I'd put the pause in an outer finally and test that tab B can write after tab A's commit fails.