-
-
Notifications
You must be signed in to change notification settings - Fork 401
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
Isn't synchronous I/O bad? #181
Comments
There are many legitimate concerns with synchronous filesystem I/O. However, in the case of SQLite3, there are also reasons to not be concerned. What if I'm using a network drive, or a slow spinning disk drive?For a Node.js program, this is very rare. Most Node.js programs are web servers, and most server providers use SSDs by default, including AWS, Google Cloud, Digital Ocean, etc. SSDs are the default choice for web servers and relational databases in 2018. If you're not using an SSD, then you're already making a bad decision, regardless of which relational database you choose to use, and regardless of which SQLite3 library you choose. The primary reason to use a spinning disk drive is to spend less money, which is typically only relevant for very large drives (in the terabyte range), and if your database size is in the terabyte range, then you probably shouldn't be using SQLite at all. What if I'm using an operating system with a poor filesystem cache?In 2018, nearly all modern filesystems are very efficient. When it comes to reading data, modern filesystem caches have an extremely high cache hit rate. If your database is extremely large (in the terabyte range) then random access patterns will be difficult for the filesystem to deal with, but, as noted previously, if your database size is in the terabyte range then you shouldn't be using SQLite3 at all. On the other hand, with a 60 GB database (for example), most modern filesystems are able to use cache very effectively, even with semi-random access patterns. Isn't it slow to write data, even with an SDD?When it comes to writing data, the typical bottleneck is What if I'm running queries that return large amounts of data?This is usually the primary reason to discourage synchronous filesystem I/O. Even if you're using an SSD and a modern operating system, reading very large chunks of data (> 1 MB) will inevitably be slow, which is bad when that blocks your main thread. However, most system architects keep large chunks of data (such as images or videos) in separate files—not part of the relational database itself (only keeping filenames in the database). This is considered good practice even in full-fledged RDBMSs such as PostgreSQL or MySQL. Therefore, most common database reads are in the kilobyte range, or smaller. This type of access pattern is extremely fast on modern operating systems. In fact, it's typically so fast that the overhead of an asynchronous operation (thread management) would actually cause more latency than simply reading synchronously. This is not always the case, but it commonly is. The nature of SQLite3Even if you're using an asynchronous SQLite3 library, SQLite3 is only capable of executing one query at a time. You cannot execute queries in parallel within a single process. Typical asynchronous SQLite3 libraries (such as If your queries are unavoidably slow, then an asynchronous SQLite3 library would make things easier for your main thread, but it won't save you from the real bottleneck in that case: your database, which is only able to execute queries one-by-one. If you fall into this category, you should be using a full-fledged RDBMS such as PostgreSQL or MySQL. In summary, if your queries are slow, SQLite3 is not for you, regardless of which library you choose. A synchronous SQLite3 library avoids the overhead of thread management, which is great. But in addition to that, it also enables:
I hope this helps to understand why one might choose to use a synchronous SQLite3 library in Node.js. |
|
wow I didn't expect such a detailed respond! Thanks! I am pushing hard to let my team switch from node-sqlite3 to better-sqlite 👍 |
This issue should be pinned. Its very helpful and explains a lot of things about the design decisions |
Leaving this issue open as it's a common question people have. |
So does that mean I shouldn't use I'm building a desktop application to be consumed by the average user, and very likely, not everyone who will install my software would be owning a decent hard disk drive, let alone, an SSD. |
@jasonsparc Although desktop apps can't control the environment that they run in, they only need to serve one user, instead of thousands or millions. So you'll be fine. Many people/organizations use |
@JoshuaWise I really want to use To describe what I'm trying to accomplish: I was considering a synchronous alternative for If it were compiled instead with I also think that But, I'm also looking forward to the resolution for #237. Hoping that you would eventually decide to use
|
@jasonsparc See my comment in #237 (comment). Worker Threads in Node.js are a very new feature. Since |
@JoshuaWise Cool! Thanks for the fast reply. I didn't know about that. I'll be looking forward to that then! :) |
Hi,
Sorry if this question is asked before, I have checked the closed issues but I can only find this one #150
My question is you said
But thru my limited experience with sqlite I found most of time the execution is I/O-bound instead of CPU-bound, correct me if I was wrong. If it is I/O-bound isn't that what asynchronous APIs is used for?
The text was updated successfully, but these errors were encountered: