db
db copied to clipboard
Can `sparklemotion/sqlite3-ruby` be used with async?
I'm new to ruby async. I like it, but I'm unsure if a gem is compatible with async. Are there any articles or resources I can learn from.
Sorry If this is a dumb issue.
It's a good question. In short, you shouldn't have any major issues unless you start becoming CPU bound. I've been using sqlite3
locally with falcon
and haven't run into any obvious issues. There are theoretical issues around concurrency, but in practice it doesn't seem to be a huge problem (the same problem exists for multi-threaded Ruby too).
In detail, the compatibility of a gem with async
Ruby primarily depends on whether the gem performs blocking I/O operations or not. Async Ruby is designed around non-blocking I/O. This means that operations that would traditionally block the thread (such as disk I/O, network requests, etc.) are instead performed in a way that allows the event loop to continue running other tasks while waiting for the operation to complete. This is crucial for achieving concurrency in Ruby using async
. Since native libraries like sqlite3
don't expose the necessary hooks for an event loop, slow (blocking) operations in sqlite3
will cause the event loop to stall. Stalling isn't necessarily bad, but it will potentially impact latency.
Thanks for your quick reply.
Since native libraries like sqlite3 don't expose the necessary hooks for an event loop, slow (blocking) operations in sqlite3 will cause the event loop to stall.
This means If there is only one event loop(one thread) in my server, my server will stop accept new requests until the stall operation in sqlite3 library passes.
my understanding right?
It's not quite as simple as that, Falcon is a multi-process - multi-event loop sever. So yes, there is the potential for a single slow request to cause problems on a single event loop, but being multi-process mitigates that. In general you should aim for all your requests to return quickly and move CPU bound workloads to a background job. Slow front-end requests are actually pretty pathological for all server architectures, not just Falcon.