Hacker News new | ask | show | jobs
by anacrolix 11 days ago
I spent years trying to squeeze this use case out of sqlite. All roads lead to tears, it is not made for this use case.

I wrote https://github.com/anacrolix/possum for this exact use case. It has a few rough edges but it absolutely hauls ass for caching on disk. Incidentally it uses sqlite for transaction/TTL metadata which is more suited to sqlite but it's still the bottleneck.

sqlite transaction commit overhead is too high and uncompetitive without batching but it's a design flaw specific to being multiprocess.

3 comments

Yeah, I'm not sure how well this will work. I've always dealt with data blobs with data on the filesystem (or s3), metadata in the database. Anything else starts to get a bit too much like a filesystem in a database, which has lead many smart people to tears.

Or is there a better way?

Metadata in a DB or index of some kind and the actual data in a key-val object store is the most versatile configuration I have used. This lets you use as many different metadata indexes as you need.
> the actual data in a key-val object store

So, on a filesystem?

Filesystems typically support mutable values, while it's beneficial in many ways to support only immutable values in distributed systems. A filesystem is a fine stand-in for object storage, though, at low scale/volume.
yes my favorite key is a blake3 hash of the object.
At the end of the day durable transaction commit in any OLTP database will always be slow because fsync is an expensive operation (~1000 IOPS on a decent SSD), so as you mentioned batching is the only way to make significant increases to throughput. That said, based on assorted benchmarks I've seen online, sqlite doesn't even get close to max fsync IOPS because it has a ton of overhead. Other embedded systems like rocksdb or lmdb are going to give better performance.
Or you can do like GP with that possum and not fsync at all. It even sets synchronous=OFF, which will corrupt SQLite databases. Then it is fast because it only writes to memory. YOLO!
The usual answer here is a WAL + batching multiple transactions.
Here are some often forgotten performance settings:

  pragma journal_mode = WAL;
  pragma synchronous = normal;
  pragma temp_store = memory;
  pragma mmap_size = 30000000000;
  pragma page_size = 32768;
  pragma cache_size = -1048576;
"Rule 2. Measure."
> Incidentally it uses sqlite for transaction/TTL metadata which is more suited to sqlite but it's still the bottleneck.

Why sqlite instead of RocksDB ?

What you need at the end of the day for a blob storage is a transactional key value store.

Which RocksDB will give you with much better throughput (and latency) and solves your transaction problem.