Skip to content

Support concurrent access to multiple databases #104

Closed

Description

@francescotescari

Hello! ✋
In our use case, we are managing several databases from our application concurrently.
We are taking into consideration the different VFSs of wa-sqlite, but after some testing, we realized that when we access concurrently different databases (no concurrent access to the same database), we encounter all sorts of possible errors (some listed below).

  • Is concurrent access to multiple databases supported?
  • If yes, what is the proper way to achieve it? Should we create a VFS for each DB?

I put together a tiny demo that helps us (and maybe someone else) replicate the concurrency problems, currently available here. It is similar to the original demo (access it at demo/concurrency.html), with the difference that it can be run on several databases concurrently and also protect them with a mutex. Interestingly, by accessing the different databases exclusively with a mutex, there are no errors.

My suspect is that asyncify + concurrency use breaks the sqlite implementation. In this case, the only options are either using a sync implementation (e.g. sync file handle pool) or prevent any concurrent access with a mutex (with possible hit on performance)

Here are some errors happening when running SQLite concurrently:

Uncaught (in promise) RuntimeError: memory access out of bounds
    at wa-sqlite-async.wasm:0xf0993
wa-sqlite.wasm:0x3fffa Uncaught (in promise) RuntimeError: null function or function signature mismatch
    at wa-sqlite.wasm:0x3fffa
OriginPrivateFileSystemVFS.js:116 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'filename')
    at OriginPrivateFileSystemVFS.js:116:30

...

Activity

  1. rhashimoto commented on Jul 21, 2023

    @rhashimoto
    Owner

    My suspect is that asyncify + concurrency use breaks the sqlite implementation.

    Your suspicion is correct. You are effectively trying to run multiple threads here, but an Asyncify-ed WASM module instance is in general not reentrant:

    It is not safe to start an async operation while another is already running. The first must complete before the second begins.

    I've never tried to do this so I don't know exactly what exceptions you might get, but I would expect it to fail spectacularly.

    In this case, the only options are either using a sync implementation (e.g. sync file handle pool) or prevent any concurrent access with a mutex (with possible hit on performance)

    You could try either of those things. I haven't tried creating multiple database connections in a single WASM module instance, but I believe it should be possible as long there is at most one WASM call executing at a time. I probably wouldn't do this, though, because the bugs here will be race conditions which are difficult to track down.

    If you can dedicate a separate Worker to each database connection then I would recommend doing that. That would maximize your concurrency since you will actually be running in separate threads.

    If you need everything in one Javascript context then another thing you can try is creating multiple WASM module instances and put a database connection in each one. This is something else I have never tried but I think should work.

    Finally, if you multiplex your database queries onto a single connection, you can just ATTACH your other databases. This will provide no concurrency at all but might give you the fewest headaches.

  2. added
    expected behavior
    on Jul 21, 2023
  3. francescotescari commented on Jul 21, 2023

    @francescotescari
    Author

    You could try either of those things. I haven't tried creating multiple database connections in a single WASM module instance, but I believe it should be possible as long there is at most one WASM call executing at a time. I probably wouldn't do this, though, because the bugs here will be race conditions which are difficult to track down.

    Is this true also for the sync version or just for the async? AFAIU this concurrency problem is probably caused by asyncify. I was thinking that the sync access pool (that doesn't use asyncify) should not have any concurrency issues since it's a fully sync API and implementation, isn't it 🤔

    If you can dedicate a separate Worker to each database connection then I would recommend doing that. That would maximize your concurrency since you will actually be running in separate threads.

    We have 4 databases but we don't write continuously to all of them, I'll try to evaluate whether a mutex is sufficient, but thanks for the suggestion 🙏

    If you need everything in one Javascript context then another thing you can try is creating multiple WASM module instances and put a database connection in each one. This is something else I have never tried but I think should work.

    This sounds reasonable, assuming sqlite doesn't use a lot of memory. Is there a trivial way to achieve this? Do we just need to load the sqlite module multiple times?

    Finally, if you multiplex your database queries onto a single connection, you can just ATTACH your other databases. This will provide no concurrency at all but might give you the fewest headaches.

    Even if we do that, we still need to prevent our application from trying to access the single connection concurrently, so we need the mutex anyway.

    Thanks for the help!

  4. rhashimoto commented on Jul 21, 2023

    @rhashimoto
    Owner

    Is this true also for the sync version or just for the async? AFAIU this concurrency problem is probably caused by asyncify.

    Yes, you're correct. A non-Asyncify build should not have these problems, and you can use AccessHandlePoolVFS with it. So that may be the best thing to try first.

    Do we just need to load the sqlite module multiple times?

    If you import the ES6 module like this:

    import SQLiteESMFactory from 'wa-sqlite/dist/wa-sqlite-async.mjs';

    Then you would call SQLiteESMFactory() multiple times. But again, I've never tried that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    expected behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions