4/30/2026 at 5:21:49 PM
"Idle cost is that one lightweight SELECT per millisecond per database — no page-cache pressure, no writer-lock contention, no kernel file watcher in the mix."I think (respectfully) the LLM that probably wrote this overshot the mark here because busy-polling a select does not actually sound better to me than a "kernel file watcher".
by tptacek
4/30/2026 at 5:41:36 PM
"one lightweight SELECT per millisecond"This reminds me of the teenager who told her dad that she was just a tiny little bit pregnant.
by felooboolooomba
4/30/2026 at 10:05:19 PM
One cannot be a little bit pregnant. But a DB can be only a little bit in the RAM, and specifically in the page cache. SQLite can act exactly like that, and it's damn fast as long as it does not need to durably write a transaction. Polling once a millisecond could spend a few microseconds.I wonder if using a tiny Redis instance, or even something like LevelDB would be even more efficient.
by nine_k
5/1/2026 at 8:28:58 AM
With the file-watch APIs is that you don't need to poll at all - free is better than cheap.by Retr0id
4/30/2026 at 9:14:29 PM
Thing of the battery!(read that in the way of "think of the children!")
by sroussey
4/30/2026 at 6:09:31 PM
[flagged]by giraffe_lady
4/30/2026 at 6:48:17 PM
Hold on -- if it really is "one lightweight SELECT per millisecond", and you're saying a select is "a couple hundred microseconds", say generously 200us?, then you're spending 200us out of every 1000us just selecting. That's a lot of polling!by rv64imafdc
4/30/2026 at 7:24:32 PM
I mean only in the same sense that you spend 1 second per second doing something. Time is probably not the best way to evaluate the resources this consumes and I doubt it takes much of anything else either.It does seem weird though even for sqlite. I wonder how oban does it. I also wonder if OP knows oban can run on sqlite.
by giraffe_lady
4/30/2026 at 6:15:00 PM
Yeah, again, to be clear: I get how SQLite works and I'm not dunking on the design, I'm just saying the comparison set up on this page snags. It's a classic LLM negated triptych, but "one of these things is not like the other": cache pressure: bad, writer contention: bad, kernel file watcher: ... good, actually? Intuitively seems better than this design?by tptacek
4/30/2026 at 8:41:27 PM
to me it sounds like they asked it to not make a kernel file watcher, and now it writes that into every comment everywhere, despite not even being in the implementationby 8note
4/30/2026 at 9:47:58 PM
Yupby russellthehippo
4/30/2026 at 9:47:29 PM
Respectfully (thanks haha) - yeah probably right. Original intent was to use inotify type thing but i avoided per-platform differences at the outset. this was definitely a for fun project that blew up unintentionally and am working to harden/improve.Love Fly.
by russellthehippo
5/1/2026 at 11:39:18 AM
One of the things people seem to forget is that SQLite itself polls every millisecond or so to grab a lock.So yes, don't use this in a mobile device, or a server if you want to let the CPU enter a low power state.
Otherwise, a single thread doing this in an otherwise idle server, doesn't seem that terrible. And if it's not idle, inotify won't help you (need to query what changed afterwards).
by ncruces
5/1/2026 at 8:49:35 PM
Appreciated your input on the original thread as well. Maybe I should note this recommendation in the docs or something.by russellthehippo
4/30/2026 at 5:52:21 PM
If you're not making any changes to the database, does the SELECT "kill" you?And if you are making changes, don't you have to poll regardless after the file watcher wakes you?
For WAL mode, SQLite can probably satisfy this query just by inspecting some shared memory. But it is busy waiting, sure.
by ncruces
4/30/2026 at 8:42:30 PM
SQLite has a wal hook which calls you back every time a transaction is committed to the WAL. https://www.sqlite.org/c3ref/wal_hook.htmlby billywhizz
4/30/2026 at 8:50:04 PM
That only catches changes made by the database connection being "hooked."This has a thread running in the background trying to catch changes made by other connections, potentially (I'm not sure here, but I suspect as much) in different processes that are modifying the same database.
by ncruces
4/30/2026 at 9:09:06 PM
good point. but ime and as seems to be widely understood writing from multiple connections is a bit of a minefield in SQLite. and afaik it still would be possible to have a hook on all connections you expect to be writing?by billywhizz
5/1/2026 at 1:32:55 AM
That wouldn't work across processes. And if you only care about in-process queuing then you might find it easier/faster to use another kind of storage or roll your own WAL.by duped
4/30/2026 at 11:09:00 PM
i did a quick benchmark on this with a single db connection updating user_version in a tight loop with the wal_hook callback enabled.on my crappy old i5 with the db file on /dev/shm it can do ~150k writes a second with the wal_hook callback called on every write. and this is using JS bindings to C++ so has some unnecessary overhead.
by billywhizz
4/30/2026 at 7:37:48 PM
[dead]by redsocksfan45
5/1/2026 at 6:43:28 AM
A prepared `PRAGMA data_version` is likely quite cheap to run because it hits the same page every time……but some other push-based IPC mechanism would be a lot more battery friendly
by conradev
4/30/2026 at 8:48:11 PM
> one lightweight SELECT per millisecondFor the low, low cost of $1 per minute, you can also lease a supercar.
by paulddraper
4/30/2026 at 5:49:14 PM
Yeah, I had the same instinct - this feels very much like a "nice idea" but the execution falls short. I mean - busily banging on sqlite like this? Shit at that point just use Redis.by d1l
4/30/2026 at 6:17:14 PM
For what it's worth, Kine (software that k3s uses to replace etcd with SQL databases) implements etcd watches on SQLite through polling[1]. The reason being that SQLite does not offer NOTIFY/LISTEN like MySQL and Postgres do. Ironically, Honkey attempts implementing NOTIFY/LISTEN through polling.k3s has been running on my home server for about three years now (using the default SQLite backend), and there doesn't seem to be excessive CPU usage despite dozens of watches existing in the simulated etcd. Of course, this doesn't say much about Honker, but it's nonetheless worth pointing out that sometimes the choice of database forces one towards a certain design.
[1] https://github.com/k3s-io/kine/blob/648a2daa/pkg/logstructur...
by koito17
4/30/2026 at 7:20:15 PM
With SQLite, you're basically funneled towards a single-writer / single-process design anyway ... in which case why not use a more traditional condvar + mutex rather than polling?by jallmann
4/30/2026 at 9:15:45 PM
Are you trying to avoid sleep?by sroussey
4/30/2026 at 6:01:57 PM
I'm not even saying it's unworkable, just, my intuition is not that the "lightweight per-millisecond select" is an optimal design.by tptacek
4/30/2026 at 6:13:29 PM
Really might be in sqlite. I've learned to never trust my intuition about performance with that thing. So many times I've gone to "optimize" something and discovered that the naive hack way I had been doing it was faster anyway. It's built for this sort of bullshit.by giraffe_lady
4/30/2026 at 6:15:27 PM
Maybe, I'm really writing about the language on this page, not about the design (I responded about this upthread).by tptacek
4/30/2026 at 6:22:18 PM
Oh, yes, I see what you mean now.by giraffe_lady
4/30/2026 at 6:00:55 PM
What's the CPU usage? Like 2%?I had a manual fs polling thing a while back. It was ugly (low time budget, didn't wanna mess with the native watchers), just scanned the whole thing once per second. It averaged out to like 0.3% CPU.
Not elegant, but acceptable for my purposes! (Small-ish directory, and "ping me within a second or two" was realtime enough for this use case.)
by andai
5/1/2026 at 3:49:46 AM
If this stops the core being able to drop to a lower power state it can be whole multiples of power use on some devices.Wake ups are death for mobile form factors, even if not really doing much work.
by kimixa
5/1/2026 at 5:13:15 PM
This is a pretty good argument against the way we do operating systems now, right?by andai
5/1/2026 at 9:15:04 PM
Why? Most modern OSs are "tickless" - where there's no regular scheduling tick and it can sleep pretty much indefinitely if there's no work.by kimixa
4/30/2026 at 8:37:34 PM
i mean, technically this is once per millisecond, so this would happen 1000x more. In your case due to the kernel overhead you would likely not even be able to do it (300% CPU?).Either way this does seem like a very large overhead due to the fact that there's just no other way to do it without a deeper kernel integration which might be outside the scope of what sqlite is trying to do.
by booi
4/30/2026 at 9:57:12 PM
If the fs tree scanned once per second had 1000 files, it would be once per millisecond for a file.by nine_k
4/30/2026 at 5:46:41 PM
[flagged]by djdillon