Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Missing link/section about sync and async APis for sqlite #1552

Open
8549 opened this issue Nov 23, 2023 · 7 comments
Open

[DOCS] Missing link/section about sync and async APis for sqlite #1552

8549 opened this issue Nov 23, 2023 · 7 comments
Assignees
Labels
docs/undocumented docs Improvements or additions to documentation priority Will be worked on next

Comments

@8549
Copy link

8549 commented Nov 23, 2023

At the end of these pages https://orm.drizzle.team/docs/quick-sqlite/bun and https://orm.drizzle.team/docs/quick-sqlite/better-sqlite3 there's this paragraph:

More on sync and async APIs for sqlite - read here.

The link (or the following section) appear to be missing.

@winghouchan
Copy link

@vlad-stohnii looks to be the author according to the blame. Do you remember where it was supposed to link to? 🙏

@eliot-akira
Copy link

eliot-akira commented Sep 23, 2024

I saw the missing link in the docs site and wanted to contribute a quick pull request to add it. However, that led me down a rabbit hole - apparently async API for SQLite is still an unsolved question.

It was originally added here:

But currently better-sqlite3 and bun:sqlite only provide a synchronous API.

This mismatch (?) of wrapping the synchronous interface of SQLite with an async API seems to be the cause of an issue with transaction rollback.

Someone wrote a workaround for that particular issue, but it does seem like this topic deserves its own section in the documentation.


This page in the SQLite docs gets to the heart of the matter, describing the tradeoffs of sync/async interface.

Normally, when SQLite writes to a database file, it waits until the write operation is finished before returning control to the calling application. Since writing to the file-system is usually very slow compared with CPU bound operations, this can be a performance bottleneck.

The asynchronous I/O backend is an extension that causes SQLite to perform all write requests using a separate thread running in the background. Although this does not reduce the overall system resources (CPU, disk bandwidth etc.), it does allow SQLite to return control to the caller quickly even when writing to the database.

Functionality

With asynchronous I/O, write requests are handled by a separate thread running in the background. This means that the thread that initiates a database write does not have to wait for (sometimes slow) disk I/O to occur. The write seems to happen very quickly, though in reality it is happening at its usual slow pace in the background.

Asynchronous I/O appears to give better responsiveness, but at a price. You lose the Durable property. With the default I/O backend of SQLite, once a write completes, you know that the information you wrote is safely on disk. With the asynchronous I/O, this is not the case. If your program crashes or if a power loss occurs after the database write but before the asynchronous write thread has completed, then the database change might never make it to disk and the next user of the database might not see your change.

You lose Durability with asynchronous I/O, but you still retain the other parts of ACID: Atomic, Consistent, and Isolated. Many applications get along fine without the Durability.

@L-Mario564 L-Mario564 added docs Improvements or additions to documentation docs/undocumented priority Will be worked on next labels Oct 17, 2024
@AndriiSherman AndriiSherman self-assigned this Dec 16, 2024
@kaaax0815
Copy link

@AndriiSherman @eliot-akira Is there any update to this?

@eliot-akira
Copy link

eliot-akira commented Jan 15, 2025

The original issue description is out of date now, these mentioned links don't exist anymore:

Searching the docs, there is a mention of an async API for SQLite, still without a link.

unlike any other ORM, for synchronous drivers like bun:sqlite we have both async and sync APIs

From: https://orm.drizzle.team/docs/connect-bun-sqlite


From what I can see, the solution may be a big effort.

@kaaax0815
Copy link

It seems that libsql supports async transactions

try {
const result = await transaction(tx);
await this.session.run(sql.raw(`release savepoint ${savepointName}`));
return result;
} catch (err) {
await this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
throw err;
}

@eliot-akira
Copy link

I see, a few lines above it, there is a comment:

// TODO: support transaction behavior

Also noticed in op-sqlite the transaction class is marked as async (extends SQLiteTransaction<'async'>) but the transaction callback is not awaited.

const tx = new OPSQLiteTransaction('async', this.dialect, this.session, this.schema, this.nestedIndex + 1);
this.session.run(sql.raw(`savepoint ${savepointName}`));
try {
const result = transaction(tx);
this.session.run(sql.raw(`release savepoint ${savepointName}`));
return result;
} catch (err) {
this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
throw err;

@kaaax0815
Copy link

kaaax0815 commented Jan 15, 2025

This is a project so many depend on and the code is insanely weird and unmaintained.

I hope transactions work with libsql otherwise a lot of work is wasted

Edit: It seems that drizzle support async transaction with libsql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs/undocumented docs Improvements or additions to documentation priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

7 participants