Skip to content

Asynchronous SQLite API #978

Open

Description

@losfair

What is the problem this feature will solve?

Hello,

I understand that for common-case efficiency bun:sqlite exposes a synchronous interface. But for high-latency storage providers (like database on NFS, or a custom distributed VFS like mvsqlite) an asynchronous interface would help in preventing the main thread from getting blocked on I/O.

What is the feature you are proposing to solve the problem?

Add an asynchronous SQLite interface backed by a thread pool, alongside the current synchronous interface.

What alternatives have you considered?

No response

Activity

  1. Jarred-Sumner commented on Aug 5, 2022

    @Jarred-Sumner
    Collaborator

    Probably will be necessary eventually but async will ~always be slower outside of this usecase

  2. Electroid commented on Nov 3, 2022

    @Electroid
    Contributor

    It might make sense to expose an AsyncDatabase, where all the functions are async.

  3. cztomsik commented on Dec 7, 2023

    @cztomsik

    This is not just about performance, but also about availability. If there's slow SQL query, it will block main thread, right? And no other requests can be handled (sockets will be accepted but not handled before sqlite will return)

    Maybe if there was at least .step() method, then the impact of this would be much smaller?

  4. small-angel commented on Feb 5, 2024

    @small-angel
    Contributor

    Based on a very quick read of Write-Ahead Log, it seems like that solves what I thought was not possible with sqlite - parallel queries. Not 100% sure.

  5. added
    bun.jsSomething to do with a Bun-specific API
    on Feb 8, 2025
  6. tlonny commented on Oct 27, 2025

    @tlonny

    I have a server-side application running that needs to concurrently service 100s of different SQLite databases (one per user).

    Bun's SQLite implementation certainly is fast, however at this degree of concurrency, the sheer amount of synchronous I/O grinds my event loop to a crawl.

    My "solution" is to (ab)-use Bun's new worker threads:

    • I spin up N worker threads
    • Each thread has an LRU which maps database path to database
    • Queries {sql : string, params : { ... }, databasePath: string } are passed as messages round-robin to worker threads
    • The worker that receives the message grabs the database from the LRU
    • Runs the query and posts the result back to the main thread.
    • The main thread awaits the response message
  7. tlonny commented on Oct 27, 2025

    @tlonny

    Based on a very quick read of Write-Ahead Log, it seems like that solves what I thought was not possible with sqlite - parallel queries. Not 100% sure.

    I believe this would only be true if your queries are run across multiple Bun processes/threads. If the queries all happen within the same process/thread then the synchronous nature of the API should mean the WAL confers no benefit to you.

  8. isaachinman commented on Oct 27, 2025

    @isaachinman

    @Jarred-Sumner Also requesting an optional async API. There are genuine use cases for it, and this issue has been open for years.

    Well aware it'll be slower and look worse on benchmarks, but as an opt-in it would be really useful.

  9. theshadow27 commented on Jan 16, 2026

    @theshadow27

    Another use case is DB scrubs, consistency checking, etc. When the DB gets to ~mb and you're checking relationships between json objects.

  10. ninjaasmoke commented on Jul 25, 2026

    @ninjaasmoke

    Opened #34863 AsyncDatabase, as @Electroid suggested.

    @Jarred-Sumner you were right that it's slower per query: 10k sequential reads are 8.84 ms sync vs 179.66 ms async.

    The win is availability, which is what @cztomsik was pointing at.

    One ~400 ms query while 100 /pings arrive:

    pings served during the query ping p50
    Database 0 / 100 419.93 ms
    AsyncDatabase 100 / 100 3.87 ms

    Opt-in, own sqlite3*, sync path untouched. bun:sqlite (173) and node:sqlite (112) pass unchanged.

    CI hasn't run (fork PR), Buildkite build is blocked. Could someone unblock it?

  11. added a commit that references this issue on Nov 16, 2026
    3c5092a
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

    bun.jsSomething to do with a Bun-specific APIbun:sqliteSomething to do with bun:sqliteenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions