How basho/bitcask works
Purpose and entry points
Bitcask is an Erlang log-structured hash-table storage engine for fast key/value access. Its public API and OTP application boundary are src/bitcask.erl, src/bitcask_app.erl, and src/bitcask.app.src. Consumers open/use stores through the Erlang API; the application starts its supervision tree.
OTP lifecycle and process ownership
The application callback starts src/bitcask_sup.erl, which owns long-lived service processes. Background maintenance is represented by src/bitcask_merge_worker.erl; merge deletion work is separated in src/bitcask_merge_delete.erl. This establishes fault isolation between foreground store operations and compaction cleanup.
Append-oriented storage layer
Persistent data is organized around file-backed, log-structured writes. src/bitcask_file.erl models/manages Bitcask data files, while src/bitcask_fileops.erl and src/bitcask_io.erl provide filesystem and I/O boundaries. Reads resolve current records through the in-memory key directory implied by the hash-table design; writes append records to active files rather than updating records in place.
Locking and concurrent access
Store/file coordination is isolated behind src/bitcask_lockops.erl, with shared definitions in include/bitcask.hrl. This is the principal boundary for protecting mutable store state and preventing conflicting access during normal operations, rotation, or maintenance.
Merge and compaction flow
Background merge logic rewrites live records to reclaim obsolete log entries. src/bitcask_merge_worker.erl drives merge execution, src/bitcask_merge_delete.erl handles deferred old-file removal, and src/bitcask_bump.erl supports state/version advancement associated with maintenance. The merge path must coordinate with file and lock layers so replacement files become visible before obsolete files are deleted.
Native acceleration boundary
Erlang calls into a native implementation through src/bitcask_nifs.erl. The NIF boundary is implemented in c_src/bitcask_nifs.c, with compatibility/utilities in c_src/erl_nif_compat.h and c_src/erl_nif_util.c. Hashing support includes c_src/murmurhash.c. Native code is a material runtime boundary: failures and ABI compatibility affect the Erlang VM process.
Time, expiry, and operational schemas
Time handling is centralized in src/bitcask_time.erl; expiry behavior is therefore separate from raw record I/O. Operational configuration/schema artifacts are under priv/bitcask.schema, priv/bitcask_multi.schema, and priv/scenario/, including multi-backend and scenario-driven operation support.
Build, packaging, and runtime environment
The repository is an Erlang/rebar project, with top-level build entry point Makefile and dependency/build configuration in rebar.config. It requires Erlang/OTP R14B04 or later and compiles both Erlang and C NIF code. Distribution packaging targets Debian, RPM, and Solaris through package/deb/, package/rpm/SPECS/bitcask.spec, and package/solaris/.
Main entry points: bitcask_merge_worker.erl Merge worker · bitcask_nifs.c NIF implementation · bitcask.erl Bitcask API · bitcask_file.erl Data files
Components 22
OTP runtime
- Bitcask API · Consumer entry point for opening and using stores.
src/bitcask.erl - Application callback
src/bitcask_app.erl - Application spec
src/bitcask.app.src - Store supervisor · Owns long-lived service processes.
src/bitcask_sup.erl
Storage engine
- Data files · Active files receive append-only records.
src/bitcask_file.erl - Filesystem I/O
src/bitcask_io.erl - File operations
src/bitcask_fileops.erl - Lock coordination · Protects mutable store state during access and maintenance.
src/bitcask_lockops.erl - Shared store definitions
include/bitcask.hrl - Time & expiry
src/bitcask_time.erl
Maintenance
- Merge worker · Rewrites live records into replacement files.
src/bitcask_merge_worker.erl - Deferred file deletion · Removes obsolete files after replacement visibility.
src/bitcask_merge_delete.erl - State advancement
src/bitcask_bump.erl
Native runtime
- Erlang NIF bridge
src/bitcask_nifs.erl - NIF implementation · ABI-sensitive code executing inside the Erlang VM.
c_src/bitcask_nifs.c - NIF compatibility
c_src/erl_nif_compat.h - NIF utilities
c_src/erl_nif_util.c - MurmurHash
c_src/murmurhash.c
Build & operations
- Build configuration · Builds Erlang code and C NIF code.
rebar.config - Build entry point
Makefile - Operational schemas · Includes multi-backend and scenario-driven operation artifacts.
priv/bitcask.schema - Distribution packaging · Debian, RPM, and Solaris packaging.
Connections 23
- Application spec → Application callback: declares
- Application callback → Store supervisor: starts
- Bitcask API → Data files: reads and appends records
- Bitcask API → Lock coordination: coordinates access
- Bitcask API → Time & expiry: applies expiry
- Data files → Filesystem I/O: performs I/O
- Data files → File operations: manages files
- Lock coordination → Shared store definitions: uses
- Store supervisor → Merge worker: owns
- Store supervisor → Deferred file deletion: owns
- Merge worker → Data files: rewrites live records
- Merge worker → Lock coordination: coordinates replacement
- Merge worker → State advancement: advances state
- Merge worker → Deferred file deletion: defers old-file removal
- Deferred file deletion → File operations: deletes obsolete files
- Erlang NIF bridge → NIF implementation: loads
- NIF implementation → NIF compatibility: uses
- NIF implementation → NIF utilities: uses
- NIF implementation → MurmurHash: uses hashing
- Build entry point → Build configuration: invokes
- Build configuration → NIF implementation: compiles
- Build configuration → Distribution packaging: feeds packages
- Operational schemas → Bitcask API: configures operation