You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What makes me hesitant to use better-sqlite3 is because node-sqlite3 more popular, if this is really
"better" why is the other party more popular?
Reacted by Artur Klesun, Bubzy_H, polv, ehsan shahshahani, camhart, Daemon, rushii, Tadashi, zneix, Eli and 27 more
Reacted by Doug Moscrop, Ste Vaidis, Gilbert, John Haugeland, Mahesh Bandara Wijerathna, Parthiv Seetharaman, Zim, TensorCore, Pkmmte Xeleon, Weihang Lo and 7 more
There's a restaurant down the street from me called Yaso Tangbao. They have delicious beef soup and dumplings with a fantastic ginger vinegar sauce. Everyone that I've brought there has said it was some of the best food they've ever had. Right next to it, there's a McDonalds. Millions of people go to McDonalds every day, but Yaso Tangbao attracts a much more modest following. McDonalds is certainly more popular. Does this mean it's better? The obvious answer is "no", it just has greater brand recognition.
Brand recognition, whether you like it or not, controls all of our lives, even in the open source marketplace. As another example, the popular test framework mocha was actually unmaintained for years. It had so many bugs at one point that the number of issues in its GitHub repo reached quadruple digits. Despite this, it remained the most popular test framework in Node.js. Why? Because it has brand name recognition. People trustmocha, regardless of objective truth about its quality. (now it is maintained again, because of popular demand).
Even the mysql driver for Node.js is more popular than mysql2, despite them sharing the same authors. The authors have been very vocal about mysql2 being the superior successor to mysql, but many people still don't even know mysql2 exists simply because mysql came first. Newcomers will still choose mysql because it appears first when searched at npmjs.com.
In open source, being first is more important than being better. I can't think of a single example of a Node.js package that is more popular than the one it's trying to replace, even if it's clearly superior.
In any case, here are the reasons to choose better-sqlite3:
1. It's bug-free
Despite node-sqlite3 having 3x as many stars, it has 10x as many issues in GitHub. If you search through them, you'll see countless issues about race conditions, data corruption, crashing, unexpected behavior, and more. Alternatively, if you search through the better-sqlite3 issues, you'll exclusively find installation issues and suggestions for enhancement. It's safe to assume that if you can install better-sqlite3, it will work as expected.
2. It's faster
See the benchmarks results, where the performance of better-sqlite3 and node-sqlite3 are compared.
3. It's simpler to use
Let's say we'd like to execute an atomic transaction that performs 2 steps:
update some data
read some data
The proper implementation in node-sqlite3 looks like this:
const{ promisify }=require('util');const{ Database }=require('node-sqlite3');// First, we have to promisify everything, since node-sqlite3 is callback-basedconstopen=promisify((path,cb)=>newDatabase(path,function(err){cb(err,this);}));constget=promisify(Database.prototype.get);construn=promisify(Database.prototype.run);constclose=promisify(Database.prototype.close);// To safely handle a transaction, we must open a separate database connection per transaction// See: https://github.com/mapbox/node-sqlite3/issues/304#issuecomment-45280758asyncfunctionmyTransaction(){constdb=awaitopen('data.db');letresult;try{awaitrun.call(db,'BEGIN');try{awaitrun.call(db,'UPDATE ...');result=awaitget.call(db,'SELECT ...');awaitrun.call(db,'COMMIT');}catch(err){try{awaitrun.call(db,'ROLLBACK');}catch(_){/* manually ignore cases where the sqlite3 automatically rolled back the transaction */}throwerr;}}finally{awaitclose.call(db);}returnresult;}
The proper implementation in better-sqlite3 looks like this:
Not to mention, transactions in better-sqlite3 can automatically be nested. Can you imagine trying to write a nested transaction in node-sqlite3? Nightmare.
4. Features
In better-sqlite3, you can register custom functions and aggregate functions written in JavaScript, which you can run from within SQL queries.
In better-sqlite3, you can iterate through the cursor of a result set, and then stop whenever you want (you don't have to load the entire result set into memory).
In better-sqlite3, you can conveniently receive query results in many different formats (here, here, and here).
In better-sqlite3, you can safely work with SQLite's 64-bit integers, without losing precision due to JavaScript's number format.
None of the features listed above are possible in node-sqlite3.
Conclusion
better-sqlite3 wasn't first to the marketplace, so it doesn't have the brand recognition or years of legacy usage that node-sqlite3 has. But really, use whatever you want.
Reacted by Mike Escalante, Andrew Kaiser, Jeroen Claassens, Andrey Rublev, WillAvudim, Alex Rybakov, Michael Wills, punkish, zoubingwu, Greg Carter and 258 more
Reacted by Octo
Reacted by ehsan shahshahani, Daemon, Justin Chan, evercreeper, Soham Bhattacharyya, NemoStein, João Saraiva, Twilight, Cherry, Alexander Carls and 7 more
Reacted by Pranav Ramesh, Jan Doe, Twilight, TurtleIdiot, Michael Ford, Daemon, evercreeper, Soham Bhattacharyya, Wesley Bliss, João Saraiva and 11 more
Reacted by Wout Mertens, Alex Rybakov, punkish, samuba, tsemerad, Pedro Augusto de Paula Barbosa, Zach Zodkoy, Matthew McEachen, Girish Ramakrishnan, Gujie-Novade and 63 more
Reacted by Brian Harrington, punkish, Pedro Augusto de Paula Barbosa, Evgeniy Istomin, Luka Barbić, Pranav Ramesh, ⚠️ DO NOT USE! ⛔️, Twilight, MarxSoul55, TurtleIdiot and 23 more
Reacted by Dawadam, robertoslalom and Adrian Acurero
I did some comparison to node-sqlite3 by myself
first of all, synchronous calls should be avoided in a web server
just because fs.writeFile is slower than fs.writeFileSync it doesn't mean you should use fs.writeFileSync (there is a use case for each)
i've ran 1.000 inserts and updates and there was no difference in execution time
(maybe node-sqlite3 did some improvements meanwhile)
for 10.000 selects better-sqlite3 won but most of the time you are not using sqlite for selects (I guess)
the problem I had with node-sqlite3 is that it uses too much memory and doesn't release enough; I don't think is a memory leak but it bothers me and this is the reason I was looking into better-sqlite3 to solve my problem;
the sync only architecture is a huge trade-off for me
JoshuaWise should implement async or he should contribute to node-sqlite3 to share his improvements
Reacted by Artur Klesun, zadam, Raj Satra, Arnaud Dagnelies, Thomas Mur and maneshwar
Reacted by Alper Cinar, Matthew McEachen, Ty Smith, Frank, Nathan Liebrecht, wyattis, Harminder Virk, Joey Smalen, Leandro Facchinetti, Mahesh Bandara Wijerathna and 10 more
@alin1771 With fs.writeFile, you can write multiple file simultaneously, where fs.writeFileSync can only write one file at a time. The fact you're missing is that both node-sqlite3 and better-sqlite3 can only execute one SQL statement at a time. Queries in SQLite3 are serialized, regardless of whether you wrap them in asynchronous primitives.
You should test how fast your queries are (e.g., 0.1 milliseconds), and decide how much server latency is an unacceptable amount for your users (let's say, 200 milliseconds). In this example, you'd be able to handle 2000 concurrent requests before your server latency becomes unacceptable. You could switch to node-sqlite3 to unblock your web server while you run queries, but that doesn't remove the bottleneck, it only shifts where the bottleneck occurs, because even node-sqlite3 is only able to do one query at a time.
Because of SQLite3's serialized nature, it's better to optimize for speed, not concurrency (which is limited to 1).
@alin1771 Without seeing your benchmark code, I cannot asses why you're not getting the performance observed by the benchmark in this repo, which is publicly reviewable by all. I'd start by making sure you turn on WAL mode, which is a necessity for any web server using SQLite3.
db.pragma('journal_mode = WAL');
Reacted by Matthew McEachen, tanvp112, ProFatXuanAll, ehsan shahshahani, Joey Smalen, Conrad Sollitt, Soham Bhattacharyya, NemoStein, Arch4Arts, Cherry and 15 more
with fs.writeFile not only you can write multiple files but you are also not blocking the main thread and this is the most important;
my tests looks something like this: for (i = 0; i < 1000; i++) { db.prepare('UPDATE [test] SET value=? WHERE id=?').run(Math.random(), i) }
vs db.serialize(() => {for (i = 0; i < 1000; i++) { db.run('UPDATE [test] SET value=? WHERE id=?', [Math.random(), i]) }})
for me, both are fast enough I could say but better-sqlite3 blocks the main thread;
indeed WAL increase the speed by a lot but this rely on my SSD availability at that time I guess, I dont know the magic behind it.
I use multiple processes for the same database and to call a checkpoint after each update is not ideal, setInterval is not a proper solution; checkpoint method is not async either.
I like your code, is very clean and light.
I will probably do this sync to async migration by myself and I hope you will see the benefit of it at some point and we can merge
Reacted by Artur Klesun, sanperrier, Giacomo Rebonato, Thomas Mur, Alyx and Chris Savvopoulos
@alin1771 If keeping the main thread unblocked is very important to you, you may be interested in this PR which enables better-sqlite3 to work in Worker Threads. This would probably be the best of both worlds for many people. I still need to review the changes, but the PR may be accepted in the near future.
Reacted by LeMoussel, Yaroslav Vorobev, Colin Leung, polv, Michael J. Ryan, Michael Ford, Soham Bhattacharyya, moreandmore and Ruby
One issue I see with your benchmark is that in better-sqlite3, db.prepare() only needs to be invoked once for a particular SQL string. The resulting Statement object can be reused over and over, which significantly improves performance. This isn't possible with node-sqlite3.
So a proper usage would actually be:
conststmt=db.prepare('UPDATE [test] SET value=? WHERE id=?');for(i=0;i<1000;i++)stmt.run(Math.random(),i);
Reacted by Eugene Ilyin, nickk, moreandmore and PRATIK SAVALIYA
you can do the same with node-sqlite3, they support prepare as well;
I understand these improvements but they are not a real use-case and there is no point to improve a benchmark code;
most of the time these update queries come from different api calls, from different users and I cannot group them;
I will take a look into that PR but from what I know worker_threads are not as performant as "native" async; but sounds good anyway
most of the time these update queries come from different api calls, from different users and I cannot group them;
You can still reuse prepared statements by keeping a global object of all prepared statements used throughout the application. If you don't like globals, you could move it into its own module.
you can do the same with node-sqlite3, they support prepare as well;
Oops, I was wrong, it looks like you can reuse prepared statements in node-sqlite3 now. Thanks for correcting me.
Something else you might be interested in is that node-sqlite3 doesn't support transactions unless you use a different database connection for every transaction. You would think db.serialize() would be enough, but it doesn't allow you to handle errors within transactions. See this thread. That's one of the main reasons I built better-sqlite3. Opening a new database connection for each transaction was far too expensive for me.
Not to argue, but hoping to better grasp the design/intent:
Because of SQLite3's serialized nature, it's better to optimize for speed, not concurrency
Seems like this philosophy pays off when most HTTP requests need to hit the database and the database I/O is the lion's share of the time required to process each request. Seems like this philosophy would not pay off if, say, half your HTTP requests need to do no disk I/O at all. In this latter case, presumably, all the time spent by sqlite3 waiting on the disk could have been spent moving along the HTTP requests that do not access the disk.
So probably WAL mode tilts the equation in favor of ignoring concurrency (if there are enough updates in the mix) and SSD instead of spinning disk likewise raises the odds that concurrency will not help enough to care. Both factors that reduce the time wasted making V8 wait for disk I/O.
Opening a new database connection for each transaction was far too expensive for me.
I think here you're saying you can reuse an sqlite connection instead. I think I understand that sqlite3 does not separate transaction context from connection. I mean, I don't get any kind of separate transaction object that would allow me to be conducting two separate transactions at the same time over the same connection. So if someone did not follow your advice to avoid letting a transaction spread across one event loop tick, they would either need to serialize access to one sqlite3 connection, or else open a new connection to handle the next transaction that comes along before the first connection is available to be reused.
Queries in SQLite3 are serialized,
But the at-most-one writer does not block readers in WAL mode, right? In which case, it seems like not being able to overlap a read-only connection with another connection reserved for updates might leave significant performance gains on the table, depending on the mix of requests (recalling the UW research that suggests server throughput under load can be dramatically improved by giving priority to shorter-running requests). Ultimately, the disk itself is serialized, but we typically try to keep it loaded up with requests so it's never waiting on us.
Seems like this philosophy pays off when most HTTP requests need to hit the database and the database I/O is the lion's share of the time required to process each request. Seems like this philosophy would not pay off if, say, half your HTTP requests need to do no disk I/O at all. In this latter case, presumably, all the time spent by sqlite3 waiting on the disk could have been spent moving along the HTTP requests that do not access the disk.
That's correct.
So probably WAL mode tilts the equation in favor of ignoring concurrency (if there are enough updates in the mix) and SSD instead of spinning disk likewise raises the odds that concurrency will not help enough to care. Both factors that reduce the time wasted making V8 wait for disk I/O.
Again, correct.
I think here you're saying you can reuse an sqlite connection instead. I think I understand that sqlite3 does not separate transaction context from connection. I mean, I don't get any kind of separate transaction object that would allow me to be conducting two separate transactions at the same time over the same connection. So if someone did not follow your advice to avoid letting a transaction spread across one event loop tick, they would either need to serialize access to one sqlite3 connection, or else open a new connection to handle the next transaction that comes along before the first connection is available to be reused.
This is correct. However, it's important to note that while node-sqlite3 (the popular async library) does provide a way to serialize access to a connection, it's not sufficient for writing transactions because it doesn't provide a way to catch errors and rollback the transaction. That's essentially the topic of this thread.
But the at-most-one writer does not block readers in WAL mode, right? In which case, it seems like not being able to overlap a read-only connection with another connection reserved for updates might leave significant performance gains on the table, depending on the mix of requests (recalling the UW research that suggests server throughput under load can be dramatically improved by giving priority to shorter-running requests). Ultimately, the disk itself is serialized, but we typically try to keep it loaded up with requests so it's never waiting on us.
I've tried running benchmarks where async connections (node-sqlite3) were trying to perform read requests concurrently with an overlapping writer connection, but I couldn't get it to perform as well as this library. Perhaps you could try to replicate the UW research by implementing some kind of priority queue, where each prepared statement was ranked with a performance cost. Maybe it'll work, maybe it won't. Either way, no async library I'm aware of (e.g., node-sqlite3) is implemented like that currently.
I want to chime in to this conversation, because better-sqlite3 has done a lot to make the lives of many beginner nodejs developers simpler, even if they might not know at face value that it does. I'm the developer of a small-ish database wrapper called Enmap, which has evolved a lot since I started writing it years ago. It started with level-db, then I moved to having multiple plugins for the backend (sqlite being only one of them).
For the last year, however, I've locked myself into using better-sqlite3 because Enmap's main focus is to be simple to use for beginners, and I've found that having the ability to make synchronous requests to the database has helped tremendously in this endeavour. Every other database connector required the uses of promises, or the loading of the entire data set in memory, in order for me to offer the features that Enmap offers. For example, I could not synchronously auto-fetch data when it's required if I was using the sqlite (or, god forbids, the sqlite3 callback hell) modules.
better-sqlite3 being the only sync connector has helped Enmap grow to the point where I'm expecting to reach a million total downloads by the end of the summer. And when I'm not using my own module, I'll automatically default to better-sqlite3 for rapid prototyping because it's faster, simpler, and requires less setup.
To counter the shameless self-promotion, I'll add that as far as I'm concerned, the fact that my main "competitor" quick.db has switched to better-sqlite3 in order to remove promises was probably due in part to my own switch, but it's undeniably popular, having itself reached a million downloads this very week. Both of us have a great deal of gratitude owed to Joshua for our popularity and continued success.
Reacted by Joshua Wise, Ashutosh Raj, Coderintherye, Evgeniy Istomin, Twilight, Danshil Kokil Mungur, MarxSoul55, Matt Portrey, Ben Smith, Eugene Ilyin and 7 more
2 Cents, since I stumbled across this: I'm using better-sqlite3 professionally since 3 years and never been disappointed. The blocking was never a problem to us, but that's because SQLite is very fast (if handled properly; PRAGMA page_size / temp_store / journal_mode / synchronous anyone? 😀 ) and the task at hand was focused build and reading those gb's of sqlite files.
I can see why it's still common to use sync calls for simplicity and I didn't dig deep enough to get an idea of the current involved overhead for async writes. I can also see why for other tasks, blocking the main thread can be an issue. Node.js already handles async writes in its own thread and the best CPU is the one with 100% load all the time.
The performance overhead regarding async dropped alot since the initial choice for sync, so with async/await, which increases simplicity regarding asynchroniousity and the performance increases for promises, there might be a chance to go this async route nowadays without losing too much performance or even adding some. After all, not blocking the main thread might parallel the preparation of upcoming queries while writing the current one. Have there been PR's for direct asynchroniousity or only for worker threads?
Either way, better-sqlite3 never failed us. We have some complex things going on and this package has never been an issue, but a huge benefit regarding performance.
That's the architecture choice of JavaScript, non-blocking. If you need to do something blocking, in my view, unless you have an absurdly clear and robust reason, you're probably doing something wrong.
If you're worried about race conditions, just use await and you're good to go (for same context, for other contexts is not a race conditions, is only other access to other parts of DB); none of the arguments above make any sense.
Reacted by Michael, aggregate1166877 and Erick Tamayo
I'm sorry, but this just doesn't make any sense to me.
I have operations that take several seconds due to the volume of some tables. This is running on the client side. With everything being synchronous, the process is blocked for that time while it has a million other things to do, including responding to user events, since this data modification is not crucial for the application startup.
Sure, there are a million ways to solve this, but what absolutely makes no sense to me is why this wasn't solved exactly the way JS does it, without blocking the process and delivering everything out of the box? Why leave the overhead of having to solve optimization details to the developer while the runtime delivers the ready solution? Excuse me, but it makes absolutely no sense to me.
If we were to spin up workers or anything that "emulates" threads in Node, I'd rather use Python, which has much better quality libs. What's the point of using JS if we need to program in a blocking manner?
I don't understand the reasoning behind this choice.
This would only deliver more performance, more efficiency, more ease. For those who don't want concurrency issues, the language delivers everything ready to avoid race conditions.
Anyway, just venting.
I used it in electron js for a simple pos system so blocking the thread for 100ms isn't a problem considering the performance of my competition. There is no right tool for every case, i was just saying that it's very fast.
I'm sorry, but this just doesn't make any sense to me.
I have operations that take several seconds due to the volume of some tables. This is running on the client side. With everything being synchronous, the process is blocked for that time while it has a million other things to do, including responding to user events, since this data modification is not crucial for the application startup.
Sure, there are a million ways to solve this, but what absolutely makes no sense to me is why this wasn't solved exactly the way JS does it, without blocking the process and delivering everything out of the box? Why leave the overhead of having to solve optimization details to the developer while the runtime delivers the ready solution? Excuse me, but it makes absolutely no sense to me.
If we were to spin up workers or anything that "emulates" threads in Node, I'd rather use Python, which has much better quality libs. What's the point of using JS if we need to program in a blocking manner?
I don't understand the reasoning behind this choice.
This would only deliver more performance, more efficiency, more ease. For those who don't want concurrency issues, the language delivers everything ready to avoid race conditions.
Anyway, just venting.
There are plenty of plug-and-play Worker Thread libraries if you need to run queries outside the main thread. A solution like that will always perform faster than using the non-blocking node-sqlite3 library. Not to mention, if your queries are well-indexed and they run on SSDs, small queries should run in microseconds, so they're actually faster to do in the main thread because the overhead of multi-threading outweighs the query itself.
Your only argument seems to be "Node.js does it this way and therefore it's the best and only way", which is simply not true. If your only use-case is to run very slow read-only queries at a low throughput, then yeah, the node-sqlite3 library will probably be better for you. For most other use-cases, better-sqlite3 provides significant advantages. And even in that case, if you're willing to sacrifice performance, simplicity, and features, just to use a certain paradigm, then nobody here is stopping you from doing so.
There's really no merit in venting on the internet about the design choices of libraries given to the public for free, which you're not paying for, and you're not forced to use. If you don't like it, you can go make your own.
Constructive criticism backed by real-world data is always welcome.
Reacted by Thomas Mur, absolutlabs, Melroy van den Berg, ROTGP and Lütfi Altın
Don't get me wrong, @JoshuaWise , I'm not a native speaker of the language. When I searched for the term, I thought in my native language. The idea I wanted to convey here was actually the opposite. Of course, the library is yours, and you've done and continue to do an excellent service to the community by making it available to everyone. What I meant was precisely in that sense, saying that I didn't want to criticize you or your design decisions here because, after all, as you rightly said, it's your project, and no one is obligated to use it.
I've been using another one for over a year because for me, being non-blocking means real performance. I don't make so many queries that any performance gain with this or that library means anything; hardly an application running on the client side makes enough queries to justify it, but if there are heavy queries, being blocking will always cost a lot.
I think you've done and continue to do good work here, including following this thread and being willing to respond even when you wouldn't have to. I and everyone here are grateful for that.
I just came here and wanted to express this design decision of mine for my projects, so I can better inform others, not to suggest that you change anything or criticize anything. If for your project and so many other projects, being blocking doesn't cause problems, then great, but it's not the case for everyone.
I think using a solution like the one you recommended to make it non-blocking goes against the whole purpose of using a library, which is to save time and keep complexity low. If it's about making workarounds, I don't think it's worth using a lib. Simply an await should be enough, as it's not the case, I think it's another lib for me, and for those with heavy queries.
Again, I just wanted to leave this recorded here for indexing purposes and maybe help someone in the future with these points.
I'm sorry if you felt attacked or insulted by anything; I can tell you absolutely that it wasn't my intention, and thank you for the excellent work in the library!
I understand that doing a benchmark is not easy. And doing a good benchmark is harder. And executing a fair benchmark is even more difficult. But yeah...
For my current situation, I am about to switch to better-sqlite3. Here's why:
I am updating an old personal Electron application that uses SQLite3. I was strongly tempted to convert from an ipcRenderer.send request/ipcMain.send response model to ipcRenderer.invoke/ipcMain.handle - it fits the logic better. BUT, that is problematic with node-sqlite3. Its database operations (get, all, etc.) are asynchronous and do not return promises. I found no obvious way to have the handler await the completion of the node-sqlite3 operation rather than return prematurely. (The send/send model doesn't have this issue, since the ipcMain.send responses are in the completion callback.)
So, better-sqlite3's synchronous model fits the Electron ipcRenderer.invoke/ipcMain.handle model well, while node-sqlite3's particular asynchronous model would be a force fit.
Your mileage may vary.
Reacted by Michael, Melroy van den Berg, Renan Oliveira and aggregate1166877
Reviving this discussion once again, taking advantage of the performance topic, to ask if running the db in memory provides extra performance by avoiding some disk IO bottleneck? Theoretically, yes, but has anyone ever tried it? I suppose this is more of a SQLite question than a better-sqlite3 question, but I'm loving the tests and benchmarks here and would love to hear some insight.
ronburk: it seems like not being able to overlap a read-only connection with another connection reserved for updates might leave significant performance gains on the table, depending on the mix of requests (recalling the UW research that suggests server throughput under load can be dramatically improved by giving priority to shorter-running requests). Ultimately, the disk itself is serialized, but we typically try to keep it loaded up with requests so it's never waiting on us.
Even if the disk i/o is serialized (which need not be true for SSD drives), there are multiple opportunities for parallelism higher up in the stack. Caches, indexes, computations can all be parallelized.
JoshuaWise: I've tried running benchmarks where async connections (node-sqlite3) were trying to perform read requests concurrently with an overlapping writer connection, but I couldn't get it to perform as well as this library.
But that doesn't address the possibility of this library improving in performance even further due to async. The overhead of switching contexts in the event loop is extremely negligible compared to the rest of the operations (caching, indexing, disk i/o). So, I agree with @ronburk, that there might be a lot being left on the table with a sync API.
JoshuaWise: The proper implementation in better-sqlite3 looks like this:
You could achieve the same ergonomics from an async API using Async Context. Some of those features are relatively new, so compatibility for very old versions of node might be a problem.
Supporting an async API maybe extra work, and of course it is the choice of the maintainers whether they want to invest in it. But I think there are benefits to it.
Another use case for parallelism: my server currently handles several dozen SQLite files. Some of them contain files such as images and PDFs. I'm reluctant to go the synchronous way for all I/O.
"I’ve found the technical discussions in this thread incredibly helpful while building data infrastructure for resource-constrained 'potato' hardware in India. On low-power CPUs, I hit the exact event-loop contention discussed here and needed a way to move database tasks to worker threads without losing the raw speed of better-sqlite3.
Following the worker-thread orchestration logic often recommended by the maintainers, I’ve built a production-ready implementation: better-sqlite3-pool.
The Key Moat: I built a compatibility adapter that perfectly mimics the legacy sqlite3 callback API. This allows it to act as a drop-in replacement for ORMs like TypeORM, Sequelize, and Knex.js that usually default to the slower async driver. You get better-sqlite3 performance inside environments that expect the standard async interface.
It features a 1-Writer / N-Reader worker architecture and a compatibility adapter that allows it to work as a drop-in driver for ORMs like TypeORM, Sequelize, and Knex.js. I verified the reliability by porting 100% of the original better-sqlite3 test suite.
If anyone is looking for a battle-tested way to use the worker-thread approach discussed here, I hope this helps. Huge thanks to the maintainers for such an incredible engine!"
Following the worker-thread orchestration logic often recommended by the maintainers, I’ve built a production-ready implementation: better-sqlite3-pool.
That's interesting. Because node-sqlite3 is not maintained anymore.
Activity
JoshuaWise commentedon May 8, 2019
There's a restaurant down the street from me called Yaso Tangbao. They have delicious beef soup and dumplings with a fantastic ginger vinegar sauce. Everyone that I've brought there has said it was some of the best food they've ever had. Right next to it, there's a McDonalds. Millions of people go to McDonalds every day, but Yaso Tangbao attracts a much more modest following. McDonalds is certainly more popular. Does this mean it's better? The obvious answer is "no", it just has greater brand recognition.
Brand recognition, whether you like it or not, controls all of our lives, even in the open source marketplace. As another example, the popular test framework
mochawas actually unmaintained for years. It had so many bugs at one point that the number of issues in its GitHub repo reached quadruple digits. Despite this, it remained the most popular test framework in Node.js. Why? Because it has brand name recognition. People trustmocha, regardless of objective truth about its quality. (now it is maintained again, because of popular demand).Even the
mysqldriver for Node.js is more popular thanmysql2, despite them sharing the same authors. The authors have been very vocal aboutmysql2being the superior successor tomysql, but many people still don't even knowmysql2exists simply becausemysqlcame first. Newcomers will still choosemysqlbecause it appears first when searched at npmjs.com.In open source, being first is more important than being better. I can't think of a single example of a Node.js package that is more popular than the one it's trying to replace, even if it's clearly superior.
In any case, here are the reasons to choose
better-sqlite3:1. It's bug-free
Despite
node-sqlite3having 3x as many stars, it has 10x as many issues in GitHub. If you search through them, you'll see countless issues about race conditions, data corruption, crashing, unexpected behavior, and more. Alternatively, if you search through thebetter-sqlite3issues, you'll exclusively find installation issues and suggestions for enhancement. It's safe to assume that if you can installbetter-sqlite3, it will work as expected.2. It's faster
See the benchmarks results, where the performance of
better-sqlite3andnode-sqlite3are compared.3. It's simpler to use
Let's say we'd like to execute an atomic transaction that performs 2 steps:
The proper implementation in
node-sqlite3looks like this:The proper implementation in
better-sqlite3looks like this:Not to mention, transactions in
better-sqlite3can automatically be nested. Can you imagine trying to write a nested transaction innode-sqlite3? Nightmare.4. Features
In
better-sqlite3, you can register custom functions and aggregate functions written in JavaScript, which you can run from within SQL queries.In
better-sqlite3, you can iterate through the cursor of a result set, and then stop whenever you want (you don't have to load the entire result set into memory).In
better-sqlite3, you can conveniently receive query results in many different formats (here, here, and here).In
better-sqlite3, you can safely work with SQLite's 64-bit integers, without losing precision due to JavaScript's number format.None of the features listed above are possible in
node-sqlite3.Conclusion
better-sqlite3wasn't first to the marketplace, so it doesn't have the brand recognition or years of legacy usage thatnode-sqlite3has. But really, use whatever you want.alin1771 commentedon Nov 5, 2019
I did some comparison to node-sqlite3 by myself
first of all, synchronous calls should be avoided in a web server
just because fs.writeFile is slower than fs.writeFileSync it doesn't mean you should use fs.writeFileSync (there is a use case for each)
i've ran 1.000 inserts and updates and there was no difference in execution time
(maybe node-sqlite3 did some improvements meanwhile)
for 10.000 selects better-sqlite3 won but most of the time you are not using sqlite for selects (I guess)
the problem I had with node-sqlite3 is that it uses too much memory and doesn't release enough; I don't think is a memory leak but it bothers me and this is the reason I was looking into better-sqlite3 to solve my problem;
the sync only architecture is a huge trade-off for me
JoshuaWise should implement async or he should contribute to node-sqlite3 to share his improvements
JoshuaWise commentedon Nov 5, 2019
@alin1771 With
fs.writeFile, you can write multiple file simultaneously, wherefs.writeFileSynccan only write one file at a time. The fact you're missing is that bothnode-sqlite3andbetter-sqlite3can only execute one SQL statement at a time. Queries in SQLite3 are serialized, regardless of whether you wrap them in asynchronous primitives.You should test how fast your queries are (e.g., 0.1 milliseconds), and decide how much server latency is an unacceptable amount for your users (let's say, 200 milliseconds). In this example, you'd be able to handle 2000 concurrent requests before your server latency becomes unacceptable. You could switch to
node-sqlite3to unblock your web server while you run queries, but that doesn't remove the bottleneck, it only shifts where the bottleneck occurs, because evennode-sqlite3is only able to do one query at a time.Because of SQLite3's serialized nature, it's better to optimize for speed, not concurrency (which is limited to 1).
@alin1771 Without seeing your benchmark code, I cannot asses why you're not getting the performance observed by the benchmark in this repo, which is publicly reviewable by all. I'd start by making sure you turn on WAL mode, which is a necessity for any web server using SQLite3.
alin1771 commentedon Nov 5, 2019
with fs.writeFile not only you can write multiple files but you are also not blocking the main thread and this is the most important;
my tests looks something like this:
for (i = 0; i < 1000; i++) { db.prepare('UPDATE [test] SET value=? WHERE id=?').run(Math.random(), i) }vs
db.serialize(() => {for (i = 0; i < 1000; i++) { db.run('UPDATE [test] SET value=? WHERE id=?', [Math.random(), i]) }})for me, both are fast enough I could say but better-sqlite3 blocks the main thread;
indeed WAL increase the speed by a lot but this rely on my SSD availability at that time I guess, I dont know the magic behind it.
I use multiple processes for the same database and to call a checkpoint after each update is not ideal, setInterval is not a proper solution; checkpoint method is not async either.
I like your code, is very clean and light.
I will probably do this sync to async migration by myself and I hope you will see the benefit of it at some point and we can merge
JoshuaWise commentedon Nov 5, 2019
@alin1771 If keeping the main thread unblocked is very important to you, you may be interested in this PR which enables
better-sqlite3to work in Worker Threads. This would probably be the best of both worlds for many people. I still need to review the changes, but the PR may be accepted in the near future.JoshuaWise commentedon Nov 5, 2019
One issue I see with your benchmark is that in
better-sqlite3,db.prepare()only needs to be invoked once for a particular SQL string. The resulting Statement object can be reused over and over, which significantly improves performance. This isn't possible withnode-sqlite3.So a proper usage would actually be:
alin1771 commentedon Nov 5, 2019
you can do the same with node-sqlite3, they support prepare as well;
I understand these improvements but they are not a real use-case and there is no point to improve a benchmark code;
most of the time these update queries come from different api calls, from different users and I cannot group them;
I will take a look into that PR but from what I know worker_threads are not as performant as "native" async; but sounds good anyway
thank you @JoshuaWise
JoshuaWise commentedon Nov 5, 2019
You can still reuse prepared statements by keeping a global object of all prepared statements used throughout the application. If you don't like globals, you could move it into its own module.
Oops, I was wrong, it looks like you can reuse prepared statements in
node-sqlite3now. Thanks for correcting me.Something else you might be interested in is that
node-sqlite3doesn't support transactions unless you use a different database connection for every transaction. You would thinkdb.serialize()would be enough, but it doesn't allow you to handle errors within transactions. See this thread. That's one of the main reasons I builtbetter-sqlite3. Opening a new database connection for each transaction was far too expensive for me.ronburk commentedon Jan 22, 2020
Not to argue, but hoping to better grasp the design/intent:
Seems like this philosophy pays off when most HTTP requests need to hit the database and the database I/O is the lion's share of the time required to process each request. Seems like this philosophy would not pay off if, say, half your HTTP requests need to do no disk I/O at all. In this latter case, presumably, all the time spent by sqlite3 waiting on the disk could have been spent moving along the HTTP requests that do not access the disk.
So probably WAL mode tilts the equation in favor of ignoring concurrency (if there are enough updates in the mix) and SSD instead of spinning disk likewise raises the odds that concurrency will not help enough to care. Both factors that reduce the time wasted making V8 wait for disk I/O.
I think here you're saying you can reuse an sqlite connection instead. I think I understand that sqlite3 does not separate transaction context from connection. I mean, I don't get any kind of separate transaction object that would allow me to be conducting two separate transactions at the same time over the same connection. So if someone did not follow your advice to avoid letting a transaction spread across one event loop tick, they would either need to serialize access to one sqlite3 connection, or else open a new connection to handle the next transaction that comes along before the first connection is available to be reused.
But the at-most-one writer does not block readers in WAL mode, right? In which case, it seems like not being able to overlap a read-only connection with another connection reserved for updates might leave significant performance gains on the table, depending on the mix of requests (recalling the UW research that suggests server throughput under load can be dramatically improved by giving priority to shorter-running requests). Ultimately, the disk itself is serialized, but we typically try to keep it loaded up with requests so it's never waiting on us.
Did I get any of that right?
JoshuaWise commentedon Jan 24, 2020
That's correct.
Again, correct.
This is correct. However, it's important to note that while node-sqlite3 (the popular async library) does provide a way to serialize access to a connection, it's not sufficient for writing transactions because it doesn't provide a way to catch errors and rollback the transaction. That's essentially the topic of this thread.
I've tried running benchmarks where async connections (node-sqlite3) were trying to perform read requests concurrently with an overlapping writer connection, but I couldn't get it to perform as well as this library. Perhaps you could try to replicate the UW research by implementing some kind of priority queue, where each prepared statement was ranked with a performance cost. Maybe it'll work, maybe it won't. Either way, no async library I'm aware of (e.g., node-sqlite3) is implemented like that currently.
eslachance commentedon Mar 11, 2020
I want to chime in to this conversation, because better-sqlite3 has done a lot to make the lives of many beginner nodejs developers simpler, even if they might not know at face value that it does. I'm the developer of a small-ish database wrapper called Enmap, which has evolved a lot since I started writing it years ago. It started with level-db, then I moved to having multiple plugins for the backend (sqlite being only one of them).
For the last year, however, I've locked myself into using better-sqlite3 because Enmap's main focus is to be simple to use for beginners, and I've found that having the ability to make synchronous requests to the database has helped tremendously in this endeavour. Every other database connector required the uses of promises, or the loading of the entire data set in memory, in order for me to offer the features that Enmap offers. For example, I could not synchronously auto-fetch data when it's required if I was using the
sqlite(or, god forbids, thesqlite3callback hell) modules.better-sqlite3 being the only sync connector has helped Enmap grow to the point where I'm expecting to reach a million total downloads by the end of the summer. And when I'm not using my own module, I'll automatically default to better-sqlite3 for rapid prototyping because it's faster, simpler, and requires less setup.
To counter the shameless self-promotion, I'll add that as far as I'm concerned, the fact that my main "competitor" quick.db has switched to better-sqlite3 in order to remove promises was probably due in part to my own switch, but it's undeniably popular, having itself reached a million downloads this very week. Both of us have a great deal of gratitude owed to Joshua for our popularity and continued success.
x3cion commentedon Apr 16, 2020
2 Cents, since I stumbled across this: I'm using better-sqlite3 professionally since 3 years and never been disappointed. The blocking was never a problem to us, but that's because SQLite is very fast (if handled properly; PRAGMA page_size / temp_store / journal_mode / synchronous anyone? 😀 ) and the task at hand was focused build and reading those gb's of sqlite files.
I can see why it's still common to use sync calls for simplicity and I didn't dig deep enough to get an idea of the current involved overhead for async writes. I can also see why for other tasks, blocking the main thread can be an issue. Node.js already handles async writes in its own thread and the best CPU is the one with 100% load all the time.
The performance overhead regarding async dropped alot since the initial choice for sync, so with async/await, which increases simplicity regarding asynchroniousity and the performance increases for promises, there might be a chance to go this async route nowadays without losing too much performance or even adding some. After all, not blocking the main thread might parallel the preparation of upcoming queries while writing the current one. Have there been PR's for direct asynchroniousity or only for worker threads?
Either way, better-sqlite3 never failed us. We have some complex things going on and this package has never been an issue, but a huge benefit regarding performance.
patarapolw commentedon May 10, 2020
I am looking back into
better-sqlite3because,sqlite3.each/betterSqlite3.iterateHowever, I still have problem with Electron, where
node-sqlite3is probably guaranteed to succeed. #12633 remaining items
renanoliveira0 commentedon Apr 26, 2024
That's the architecture choice of JavaScript, non-blocking. If you need to do something blocking, in my view, unless you have an absurdly clear and robust reason, you're probably doing something wrong.
If you're worried about race conditions, just use await and you're good to go (for same context, for other contexts is not a race conditions, is only other access to other parts of DB); none of the arguments above make any sense.
WassimYazbekk commentedon Apr 26, 2024
I used it in electron js for a simple pos system so blocking the thread for 100ms isn't a problem considering the performance of my competition. There is no right tool for every case, i was just saying that it's very fast.
JoshuaWise commentedon Apr 26, 2024
There are plenty of plug-and-play Worker Thread libraries if you need to run queries outside the main thread. A solution like that will always perform faster than using the non-blocking
node-sqlite3library. Not to mention, if your queries are well-indexed and they run on SSDs, small queries should run in microseconds, so they're actually faster to do in the main thread because the overhead of multi-threading outweighs the query itself.Your only argument seems to be "Node.js does it this way and therefore it's the best and only way", which is simply not true. If your only use-case is to run very slow read-only queries at a low throughput, then yeah, the
node-sqlite3library will probably be better for you. For most other use-cases,better-sqlite3provides significant advantages. And even in that case, if you're willing to sacrifice performance, simplicity, and features, just to use a certain paradigm, then nobody here is stopping you from doing so.JoshuaWise commentedon Apr 26, 2024
There's really no merit in venting on the internet about the design choices of libraries given to the public for free, which you're not paying for, and you're not forced to use. If you don't like it, you can go make your own.
Constructive criticism backed by real-world data is always welcome.
renanoliveira0 commentedon Apr 27, 2024
Don't get me wrong, @JoshuaWise , I'm not a native speaker of the language. When I searched for the term, I thought in my native language. The idea I wanted to convey here was actually the opposite. Of course, the library is yours, and you've done and continue to do an excellent service to the community by making it available to everyone. What I meant was precisely in that sense, saying that I didn't want to criticize you or your design decisions here because, after all, as you rightly said, it's your project, and no one is obligated to use it.
I've been using another one for over a year because for me, being non-blocking means real performance. I don't make so many queries that any performance gain with this or that library means anything; hardly an application running on the client side makes enough queries to justify it, but if there are heavy queries, being blocking will always cost a lot.
I think you've done and continue to do good work here, including following this thread and being willing to respond even when you wouldn't have to. I and everyone here are grateful for that.
I just came here and wanted to express this design decision of mine for my projects, so I can better inform others, not to suggest that you change anything or criticize anything. If for your project and so many other projects, being blocking doesn't cause problems, then great, but it's not the case for everyone.
I think using a solution like the one you recommended to make it non-blocking goes against the whole purpose of using a library, which is to save time and keep complexity low. If it's about making workarounds, I don't think it's worth using a lib. Simply an
awaitshould be enough, as it's not the case, I think it's another lib for me, and for those with heavy queries.Again, I just wanted to leave this recorded here for indexing purposes and maybe help someone in the future with these points.
I'm sorry if you felt attacked or insulted by anything; I can tell you absolutely that it wasn't my intention, and thank you for the excellent work in the library!
melroy89 commentedon May 3, 2024
Maybe a good idea to re-evaluate the benchmark from 2020 with todays result: https://github.com/WiseLibs/better-sqlite3/blob/master/docs/benchmark.md#results. I'm just curious.
I understand that doing a benchmark is not easy. And doing a good benchmark is harder. And executing a fair benchmark is even more difficult. But yeah...
JoshuaWise commentedon May 4, 2024
@melroy89 Here's one person who recently ran the benchmarks and got even better results than the ones advertised here: #1120
houd1ni commentedon May 5, 2024
Maybe update the official bench results then ?
DanKonigsbach commentedon May 19, 2024
For my current situation, I am about to switch to better-sqlite3. Here's why:
I am updating an old personal Electron application that uses SQLite3. I was strongly tempted to convert from an ipcRenderer.send request/ipcMain.send response model to ipcRenderer.invoke/ipcMain.handle - it fits the logic better. BUT, that is problematic with node-sqlite3. Its database operations (get, all, etc.) are asynchronous and do not return promises. I found no obvious way to have the handler await the completion of the node-sqlite3 operation rather than return prematurely. (The send/send model doesn't have this issue, since the ipcMain.send responses are in the completion callback.)
So, better-sqlite3's synchronous model fits the Electron ipcRenderer.invoke/ipcMain.handle model well, while node-sqlite3's particular asynchronous model would be a force fit.
Your mileage may vary.
wilyJ80 commentedon Aug 1, 2024
Reviving this discussion once again, taking advantage of the performance topic, to ask if running the db in memory provides extra performance by avoiding some disk IO bottleneck? Theoretically, yes, but has anyone ever tried it? I suppose this is more of a SQLite question than a
better-sqlite3question, but I'm loving the tests and benchmarks here and would love to hear some insight.t1u1 commentedon Dec 25, 2024
Even if the disk i/o is serialized (which need not be true for SSD drives), there are multiple opportunities for parallelism higher up in the stack. Caches, indexes, computations can all be parallelized.
But that doesn't address the possibility of this library improving in performance even further due to async. The overhead of switching contexts in the event loop is extremely negligible compared to the rest of the operations (caching, indexing, disk i/o). So, I agree with @ronburk, that there might be a lot being left on the table with a sync API.
You could achieve the same ergonomics from an async API using Async Context. Some of those features are relatively new, so compatibility for very old versions of node might be a problem.
Supporting an async API maybe extra work, and of course it is the choice of the maintainers whether they want to invest in it. But I think there are benefits to it.
paleo commentedon Dec 28, 2024
Another use case for parallelism: my server currently handles several dozen SQLite files. Some of them contain files such as images and PDFs. I'm reluctant to go the synchronous way for all I/O.
cwtuan commentedon Jan 8, 2025
The usage trend over the past three years, shows that
better-sqlite3has be significant increase in popularity.dilipvamsi commentedon Mar 10, 2026
"I’ve found the technical discussions in this thread incredibly helpful while building data infrastructure for resource-constrained 'potato' hardware in India. On low-power CPUs, I hit the exact event-loop contention discussed here and needed a way to move database tasks to worker threads without losing the raw speed of better-sqlite3.
Following the worker-thread orchestration logic often recommended by the maintainers, I’ve built a production-ready implementation: better-sqlite3-pool.
The Key Moat: I built a compatibility adapter that perfectly mimics the legacy
sqlite3callback API. This allows it to act as a drop-in replacement for ORMs like TypeORM, Sequelize, and Knex.js that usually default to the slower async driver. You getbetter-sqlite3performance inside environments that expect the standard async interface.It features a 1-Writer / N-Reader worker architecture and a compatibility adapter that allows it to work as a drop-in driver for ORMs like TypeORM, Sequelize, and Knex.js. I verified the reliability by porting 100% of the original better-sqlite3 test suite.
GitHub: : https://github.com/dilipvamsi/better-sqlite3-pool
npm:: https://www.npmjs.com/package/better-sqlite3-pool
If anyone is looking for a battle-tested way to use the worker-thread approach discussed here, I hope this helps. Huge thanks to the maintainers for such an incredible engine!"
paleo commentedon Mar 10, 2026
That's interesting. Because node-sqlite3 is not maintained anymore.
So I recently created this Knex connector for Libsql Client: https://github.com/paleo/knex-libsql-client
This means we have two ways of keeping our I/O asynchronous. It's great to have such an abundance!