A RocksDB backend for konserve, using clj-rocksdb.
Add to your dependencies:
(require '[konserve-rocksdb.core] ;; Registers the :rocksdb backend
'[konserve.core :as k])
(def config
{:backend :rocksdb
:path "./tmp/rocksdb/konserve"
:id #uuid "550e8400-e29b-41d4-a716-446655440000"
;; Optional:
:map-size (* 1024 1024 1024) ;; Default: 1GB
:flags 0})
(def store (k/create-store config {:sync? true}))
For API usage (assoc-in, get-in, delete-store, etc.), see the konserve documentation.
This backend supports multi-key operations (multi-assoc, multi-get, multi-dissoc).
No hard item limits - operations scale with your RocksDB instance capacity.
| Operation | RocksDB API | Atomicity |
|---|---|---|
multi-assoc | WriteBatch | Atomic |
multi-get | multiGetAsList | Efficient bulk read |
multi-dissoc | WriteBatch | Atomic |
Data is stored in two keys per entry (key for value, key.meta for header/metadata). This enables efficient metadata-only reads for garbage collection while using multiGetAsList to fetch both in a single call for full reads.
There is no serializer in this backend. konserve has already turned everything into bytes before the backend sees it (its serializer, compressor and encryptor produce a header, a meta array and a value array), so they are stored as they are:
| RocksDB key | RocksDB value |
|---|---|
| the store key's UTF-8 bytes | konserve's value bytes |
the store key + .meta, UTF-8 | konserve's 20-byte header followed by the meta bytes |
The .meta entry is laid out like the start of a konserve file-store blob and
is split by konserve's fixed header size. Conditional writes compare it
bytewise. Store keys are konserve's (lowercase hex UUIDs with suffixes), so
none starts with NPY; the backend refuses such a key, since that prefix marks
the legacy entries below.
Releases up to 0.2.26 passed every key and value through nippy. Such a store opens and works as it is, with nothing converted up front:
.meta entry from the
same write. Orphans (the legacy .meta that dissoc in releases up to
0.2.24 left behind, or a stray backup value) are absent to reads, exists?,
multi-get and listing, as they were in 0.2.26..meta, in every legacy encoding), so each
key moves to the new format the first time it is written. Deletes remove
both formats. Conditional writes compare against the bytes actually read,
in whichever format, and replace them in one transaction.k/keys, konserve.gc/sweep!) name legacy keys by
their store key, each once.konserve-rocksdb.core/port! optionally rewrites every remaining legacy
entry now, one transaction per key, while the store is in use, deletes
orphans, and compacts the old key range. Nobody has to run it. Orphans
include leftover legacy <key>.backup values: konserve never reads them,
and they are only the pre-write copy an old write that crashed may have
left. A conditional write in flight while it runs still succeeds: a key
rewritten with the same bytes passes its fence, and a commit that loses its
conflict history to a flush is retried. Reads, writes and deletes run
safely alongside it. A key listing taken while it runs may miss keys or
repeat them, since keys move between the old and the new key range;
konserve's GC is unaffected (an unseen key is not swept, and deleting twice
is harmless).Legacy entries are read by a small decoder in konserve-rocksdb.legacy, not by
nippy: it understands exactly what konserve-rocksdb ever stored (strings,
keywords, byte arrays, nil and the {:header :meta} map, in nippy's
uncompressed, LZ4 and zstd envelopes) and refuses any other type by its id,
so reading a store never looks up or instantiates a class. Every length is
checked against the entry before anything is allocated. An entry it cannot
read fails reads of that key, with :konserve-rocksdb/unreadable-legacy-entry
(and so a multi-get that includes the key). A plain k/assoc or k/dissoc
replaces it; update-in and conditional writes, which need the old value or
metadata, fail on it. nippy is not a dependency.
Two details:
:last-write) is read.bassoc by 0.2.22 or earlier reads back as its UTF-8
bytes, which is what bassoc of a string stores today.Downgrading does not work. Older releases decode every RocksDB key with
nippy when they list keys, and nippy throws on a key in the new format. So
after the first write by this release, k/keys and konserve.gc/sweep! FAIL
under an older release, and that release cannot find the rewritten keys
either. Upgrade every process that opens a store before writing to it with
this release.
Copyright © 2021-2026 Christian Weilbach, Judith Massa
Licensed under Eclipse Public License (see LICENSE).
Can you improve this documentation? These fine people already did:
Christian Weilbach & JudithEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |