r/elixir • • 5h ago

bitcask: More Than a Key-Value Store, An Embedded Data Engine with Full-Text Search, Vector Retrieval, and Graph Database Capabilities

Thumbnail
github.com
5 Upvotes

Bitcask — An Introduction

Bitcask is a high-performance, log-structured hash table for fast key/value storage, written in C++23 with an Erlang NIF interface. It goes well beyond a simple KV store: on top of the classic bitcask design it adds BM25 full-text search, approximate nearest-neighbor vector retrieval, RRF hybrid search, transactions with isolation, and even a graph database layer — making it a versatile embedded data engine for Erlang/OTP applications.

Architecture

The implementation is a single C++23 NIF (apps/bitcask/cpp/) behind a thin Erlang facade (bitcask.erl), organized as a rebar3 umbrella project. The on-disk format uses typed records with per-write ordinal numbers and optional DocValue encoding (text + vector + metadata). A separate application, apps/bitcask_cypher/, provides an openCypher graph query language built purely on the core's public API.

Key Capabilities

  • Core KV storage — binary keys and values with put/get/delete/sync, snapshot-consistent iteration via fold, and merge management for reclaiming space.
  • Ordered range queries (v6.0.0) — lexicographic scans over [Lo, Hi) at O(range) cost, ideal for prefix scans.
  • Atomic batches & multi-key transactions — put_batch_atomic and txn_commit guarantee that after a crash or power loss a batch either fully applies or not at all, with optional preconditions (absent | {ord, N}) for optimistic concurrency control.
  • Isolated transactions (bitcask_txn, v6.5.1) — a pure-OTP layer adding pessimistic two-phase locking, deadlock detection, automatic restart, prefix locks (phantom-free scans), and idempotency keys that make mid-commit crashes safely retryable — a single-node analogue of Mnesia's model.
  • BM25 full-text search — pluggable analyzers (whitespace, CJK n-gram, jieba Chinese segmentation), plus phrase, fuzzy (edit-distance), wildcard, proximity, and field-boosted search, with synonym dictionaries and highlight support.
  • Vector search — three selectable engines: hnsw (in-memory graph, up to a few million vectors), ivfrq (IVF-RaBitQ disk tier, recommended for 10M–100M), and diskann (experimental Vamana graph). Embeddings can be fetched automatically from OpenAI-compatible HTTP endpoints or computed in-process via an optional llama.cpp backend (with CUDA/Vulkan GPU support), including Matryoshka (MRL) dimension truncation.
  • Hybrid search — RRF fusion of BM25 and vector ranking signals in a single query.
  • Graph layer (graphdb) — vertices and packed adjacency lists stored as KVs, with conditional writes so concurrent edge writers lose no edges, plus transactional graph operations, BFS/shortest-path traversal, and analytics (PageRank, connected components, SSSP).
  • Structured query DSL (bitcask_query) — combines key conditions, full-text conditions, and meta filters in one query, with a planner that picks the driving index, plus secondary indexes.

Performance Highlights

Recent releases show aggressive optimization: point-read hits through the NIF dropped from 1.3–2.0 µs to 0.26–0.73 µs; graph one-hop reads went from 110 ms to 0.078 ms; put_edge on a 200k-degree hub improved from 255 µs to 20 µs; and del_vertex on a hub went from O(degree²) to linear (450 s → 2.1 s). The engine also offers a disk-resident keydir option cutting resident memory by ~90% at 100M keys.

Getting Started

The project builds with rebar3 compile (Erlang ≥ 22, plus the ICU development package), and can be used as a git_subdir dependency from any rebar3 project. C++ tests (400+ GoogleTests), sanitizers, and benchmarks are driven by CMake.

License

Apache 2.0.


r/elixir • • 22h ago

Security incident on hex.pm

Thumbnail
hex.pm
113 Upvotes