Liking cljdoc? Tell your friends :D

konserve-rocksdb

A RocksDB backend for konserve, using clj-rocksdb.

Usage

Add to your dependencies:

Clojars Project

Configuration

(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.

Implementation Details

Multi-key Operations

This backend supports multi-key operations (multi-assoc, multi-get, multi-dissoc).

No hard item limits - operations scale with your RocksDB instance capacity.

OperationRocksDB APIAtomicity
multi-assocWriteBatchAtomic
multi-getmultiGetAsListEfficient bulk read
multi-dissocWriteBatchAtomic

Storage Format

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 keyRocksDB value
the store key's UTF-8 byteskonserve's value bytes
the store key + .meta, UTF-8konserve'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.

Stores written by 0.2.26 or earlier: read old, write new

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:

  • Reads look up a key in the current format first and, only if it is not there, under its legacy keys. A store with no legacy entries left is noticed when it opens (one seek), and never probes for them.
  • A key exists only when it has both a value and a .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.
  • Writes always use the current format. The same atomic write deletes the key's legacy entries (value and .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.
  • Listing and paging (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:

  • Released jars declared nippy versions that freeze short strings with type id 105. nippy 3.3.x and 3.9 or newer use id 96 instead (and 99 for small maps); unreleased builds from late 2023 to late 2025 ran 3.3.0, and an application whose dependencies resolved nippy 3.9+ over the declared one wrote id 96 with a released jar. Releases could only find keys in the encoding their running nippy wrote. Both are found now; if a key has a complete pair in both, the one written last (by its metadata's :last-write) is read.
  • A string stored with 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.

License

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 & Judith
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close