←back to thread

Sqlite3 WebAssembly

(sqlite.org)
647 points whatever3 | 10 comments | | HN request time: 2.224s | source | bottom
Show context
simonw ◴[] No.41851934[source]
Something that would be really fun would be to run SQLite in-memory in a browser but use the same tricks as Litestream and Cloudflare Durable Objects (https://simonwillison.net/2024/Oct/13/zero-latency-sqlite-st...) to stream a copy of the WAL log to a server (maybe over a WebSocket, though intermittent fetch() POST would work too).

Then on subsequent visits use that server-side data to rehydrate the client-side database.

From https://sqlite.org/forum/info/50a4bfdb294333eec1ba4749661934... is looks like WAL mode is excluded from the default SQLite WASM build so you would have to go custom with that.

replies(10): >>41852040 #>>41852194 #>>41853089 #>>41854540 #>>41854586 #>>41854654 #>>41855596 #>>41856415 #>>41857000 #>>41858635 #
ncruces ◴[] No.41853089[source]
There are many layers of that's not how it works at play here.

In-memory SQLite databases don't use WAL. Wasm (and browser Wasm, in particular) doesn't support anything like the shared memory APIs SQLite wants for its WAL mode.

Litestream requires a very precise WAL setup to work (which just so happens to work with the default native SQLite setup, but is hard to replicate with Wasm).

Cloudflare Durable Objects may have been inspired by Litestream but works very differently (as do LiteFS, Turso, etc…)

The general idea of streaming changes from SQLite would work, but it's a lot of work, and the concurrency model of in-browser Wasm will make it challenging to implement.

(I wrote that forum post some time ago, and have WAL working in a server side Wasm build of SQLite, but none of the options to make it work would make much sense, or be possible, in browser)

replies(5): >>41853607 #>>41853645 #>>41853823 #>>41857460 #>>41860639 #
jauntywundrkind ◴[] No.41853645[source]
Then don't use in-memory sqlite? Use file backed sqlite but have your wasm implementation of those "system calls" just be to memory?

I dunno, feels like you're coming down too hard.

replies(1): >>41854002 #
ncruces ◴[] No.41854002[source]
File backed SQLite in a browser? Do you mean like OPFS?

https://sqlite.org/wasm/doc/trunk/persistence.md#opfs-wal

Again, just because the all the Lego pieces sound like they should all just fit together, doesn't mean that they will.

The VFS mechanism was primarily designed to make SQLite easy to port to multiple OSes. WAL mode is hard to port everytime you step away from a more traditional OS.

“We have SQLite in the browser, let's just stream the WAL like the Litestream hack” does not add up.

It's not impossible, but it surely took a lot of effort at Cloudflare (or at Fly, or Turso) to get there. And neither of them opened it up sufficiently to help us do the same. In a browser.

replies(2): >>41854206 #>>41857305 #
jauntywundrkind ◴[] No.41857305[source]
Emscripten's default file system provider is memfs, in memory. Maybe there would be some challenges, some spec limitations using that, but I strongly expect it closer to a weekend or two of hacking to get some special weird mystic quirkiness that WAL relies on than some long ordeal that keeps going on endlessly (to get sqlite running with WAL). https://emscripten.org/docs/api_reference/Filesystem-API.htm...

OPFS is interesting tech but again a red herring misdirecting from what had been raised, using an in-memory filesystem like the default thing that emscripten (the default toolchain) does.

replies(1): >>41857628 #
1. ncruces ◴[] No.41857628[source]
I… really don't get this.

The people on SQLite, employed to work on this full time for over a year, have this to say (on the link I posted above):

“Because the WASM build does not have shared memory APIs, activating WAL requires that a client specifically activate exclusive-locking mode for a db handle immediately after opening it, before doing anything else with it…

“WAL mode does not provide any concurrency benefits in this environment. On the contrary, the requirement for exclusive locking eliminates all concurrency support…”

I personally worked to implement shared memory WAL for a server side Wasm port of SQLite. But random internet poster decides to “strongly expect it closer to a weekend or two of hacking.”

Please, do me a favor and do spend that weekend or two for the benefit of the rest of us all. It'll sincerely be much appreciated.

PS: it was a random internet poster¹ (who's been posting in this thread) who helped me figure out how to implement shared memory WAL for my port. It still took way more than “a weekend or two.” So if you do figure out how to crack this, I'm sure that people who've been trying for the past year² will definitely appreciate it.

1: https://github.com/ncruces/go-sqlite3/discussions/69

2: https://github.com/rhashimoto/wa-sqlite

replies(1): >>41858190 #
2. jauntywundrkind ◴[] No.41858190[source]
> WAL mode does not provide any concurrency benefits in this environment.

Except we aren't interested in concurrency or performance benefits of WAL; we want it for something else entirely (replication).

replies(1): >>41858933 #
3. ncruces ◴[] No.41858933[source]
Then you don't need WAL. I didn't say replication isn't possible, I said "that's not how it works."

A bunch of different people solved this already (though none in a couple of weekends); common to all of them: they don't use WAL mode.

replies(1): >>41859123 #
4. jauntywundrkind ◴[] No.41859123{3}[source]
> The shadow WAL is a directory next to your SQLite database where WAL files are effectively recreated as a sequence. . . These WAL files contain the original WAL frames & checksums to ensure consistency.

https://litestream.io/how-it-works/

> (though none in a couple of weekends)

As I said, getting sqlite in wasm to run in WAL mode such that we could start to implement replication, like for example how litestream does. Not this strawman you build, of implementing replication.

replies(1): >>41860897 #
5. ncruces ◴[] No.41860897{4}[source]
Seriously?

Litestream works out-of-process. It needs shared memory WAL and file locking to work exactly like on "desktop" SQLite for it to even function and produce this "shadow WAL." No ifs, no buts, no inbetweens.

You just don't build Litestream without shared memory WAL. That's my entire point. These pieces you seem to think can be made to simply work together can't, in fact, work together at all, much less simply.

Exclusive locking mode WAL doesn't work with Litestream, and can't be made to work with Litestream, even with a little elbow grease.

replies(1): >>41864943 #
6. jauntywundrkind ◴[] No.41864943{5}[source]
Litestreams how-we-did-this to says they take a lock on the whole db during checkpoints to work, so I don't see why shared would be needed.

Seriously bro, take a massive chill pill. You have been way overcommitted to the "it's all impossible!!! How stupid to imagine! If only you were smart like me you'd see everything is impossible!" bit way too hard. Take a breather from your possessed absolute self certainty & derogatory belittling of others.

There's also lots of different fs impls for wasm! Just because some limitations apply to emscriptens impl doesn't mean another impl has the same limitations, or that we can't improve or work around. Use some of your hard working loves-to-tax-itself big brain to find avenues of possibility, rather than just pissing on every possibility.

replies(1): >>41867073 #
7. ncruces ◴[] No.41867073{6}[source]
I'm chill bro. Still waiting for your productive couple of weekends.

But that'll require more than looking at docs reading the words "lock" and "shared" and assuming you know what those mean.

replies(1): >>41871483 #
8. jauntywundrkind ◴[] No.41871483{7}[source]
You've been wrong about how some of these replications work, you've been wrong about why the WAL doesn't work, and you still insist on being a pompous rude brat here? You have not been chill.
replies(2): >>41871956 #>>41903568 #
9. ncruces ◴[] No.41871956{8}[source]
Litestream is out of process, requires shared memory WAL, doesn't work with exclusive mode WAL, wouldn't work work on Wasm.

LiteFS didn't support WAL mode initially, now does. The Wasm version doesn't support WAL mode at all.

mvSQLite doesn't support shared memory WAL. SQLSync doesn't support WAL at all.

Browser Wasm builds either don't have WAL enabled, or don't support shared memory WAL.

These replications are VFSes or FUSE drivers, and for those, it's much easier to work with rollback mode than WAL, especially if you don't want concurrency (or intend to provide concurrency through other means, like optimistic concurrency).

In open source, only Turso actually did a VFS to replicate the WAL. They had to break SQLite so much they call it libSQL. It also requires shared memory as is, would be hard (if possible) to port to Wasm.

Please, do take the challenge and build this. It will be much appreciated.

I've been asking the Turso guys to document their thing for a year, with little success. They have the interface (which just opens up undocumented internal SQLite interfaces), with no open source samples of how to use it (except a logger), and zero additional docs.

10. CRConrad ◴[] No.41903568{8}[source]
TBF, to this bystander ncruces isn't the one of the two of you who is coming off as more of "a pompous rude brat".