# basho/bitcask architecture

> Architecture diagram of the GitHub repository basho/bitcask, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/basho/bitcask
- Repository: https://github.com/basho/bitcask

## Overview

#### 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: [`src/bitcask_merge_worker.erl`](https://github.com/basho/bitcask/blob/develop/src/bitcask_merge_worker.erl) (Merge worker), [`c_src/bitcask_nifs.c`](https://github.com/basho/bitcask/blob/develop/c_src/bitcask_nifs.c) (NIF implementation), [`src/bitcask.erl`](https://github.com/basho/bitcask/blob/develop/src/bitcask.erl) (Bitcask API), [`src/bitcask_file.erl`](https://github.com/basho/bitcask/blob/develop/src/bitcask_file.erl) (Data files)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_otp["OTP runtime"]
  node_api["Bitcask API<br/>Erlang API<br/>[bitcask.erl]"]
  node_app["Application callback<br/>OTP application<br/>[bitcask_app.erl]"]
  node_app_spec["Application spec<br/>OTP metadata<br/>[bitcask.app.src]"]
  node_supervisor["Store supervisor<br/>OTP supervisor<br/>[bitcask_sup.erl]"]
end

subgraph group_storage["Storage engine"]
  node_file[("Data files<br/>log-structured storage<br/>[bitcask_file.erl]")]
  node_io["Filesystem I/O<br/>I/O boundary<br/>[bitcask_io.erl]"]
  node_fileops["File operations<br/>filesystem boundary"]
  node_lockops["Lock coordination<br/>concurrency boundary"]
  node_shared_defs["Shared store definitions<br/>Erlang header<br/>[bitcask.hrl]"]
  node_time["Time &amp; expiry<br/>expiry service<br/>[bitcask_time.erl]"]
end

subgraph group_maintenance["Maintenance"]
  node_merge_worker["Merge worker<br/>background worker"]
  node_merge_delete["Deferred file deletion<br/>cleanup worker"]
  node_bump["State advancement<br/>maintenance state<br/>[bitcask_bump.erl]"]
end

subgraph group_native["Native runtime"]
  node_nif_module{{"Erlang NIF bridge<br/>native interface<br/>[bitcask_nifs.erl]"}}
  node_nif_c{{"NIF implementation<br/>C NIF<br/>[bitcask_nifs.c]"}}
  node_nif_compat["NIF compatibility<br/>C header<br/>[erl_nif_compat.h]"]
  node_nif_util["NIF utilities<br/>C support<br/>[erl_nif_util.c]"]
  node_hash["MurmurHash<br/>C hashing<br/>[murmurhash.c]"]
end

subgraph group_delivery["Build &amp; operations"]
  node_build["Build configuration<br/>rebar project<br/>[rebar.config]"]
  node_makefile["Build entry point<br/>Make build"]
  node_schemas["Operational schemas<br/>runtime schemas<br/>[bitcask.schema]"]
  node_packaging["Distribution packaging<br/>package targets"]
end

node_app_spec -->|"declares"| node_app
node_app -->|"starts"| node_supervisor
node_api -->|"reads and appends records"| node_file
node_api -->|"coordinates access"| node_lockops
node_api -->|"applies expiry"| node_time
node_file -->|"performs I/O"| node_io
node_file -->|"manages files"| node_fileops
node_lockops -->|"uses"| node_shared_defs
node_supervisor -->|"owns"| node_merge_worker
node_supervisor -->|"owns"| node_merge_delete
node_merge_worker -->|"rewrites live records"| node_file
node_merge_worker -->|"coordinates replacement"| node_lockops
node_merge_worker -->|"advances state"| node_bump
node_merge_worker -->|"defers old-file removal"| node_merge_delete
node_merge_delete -->|"deletes obsolete files"| node_fileops
node_nif_module -->|"loads"| node_nif_c
node_nif_c -->|"uses"| node_nif_compat
node_nif_c -->|"uses"| node_nif_util
node_nif_c -->|"uses hashing"| node_hash
node_makefile -->|"invokes"| node_build
node_build -->|"compiles"| node_nif_c
node_build -->|"feeds packages"| node_packaging
node_schemas -.->|"configures operation"| node_api

click node_api "https://github.com/basho/bitcask/blob/develop/src/bitcask.erl"
click node_app "https://github.com/basho/bitcask/blob/develop/src/bitcask_app.erl"
click node_app_spec "https://github.com/basho/bitcask/blob/develop/src/bitcask.app.src"
click node_supervisor "https://github.com/basho/bitcask/blob/develop/src/bitcask_sup.erl"
click node_file "https://github.com/basho/bitcask/blob/develop/src/bitcask_file.erl"
click node_io "https://github.com/basho/bitcask/blob/develop/src/bitcask_io.erl"
click node_fileops "https://github.com/basho/bitcask/blob/develop/src/bitcask_fileops.erl"
click node_lockops "https://github.com/basho/bitcask/blob/develop/src/bitcask_lockops.erl"
click node_shared_defs "https://github.com/basho/bitcask/blob/develop/include/bitcask.hrl"
click node_time "https://github.com/basho/bitcask/blob/develop/src/bitcask_time.erl"
click node_merge_worker "https://github.com/basho/bitcask/blob/develop/src/bitcask_merge_worker.erl"
click node_merge_delete "https://github.com/basho/bitcask/blob/develop/src/bitcask_merge_delete.erl"
click node_bump "https://github.com/basho/bitcask/blob/develop/src/bitcask_bump.erl"
click node_nif_module "https://github.com/basho/bitcask/blob/develop/src/bitcask_nifs.erl"
click node_nif_c "https://github.com/basho/bitcask/blob/develop/c_src/bitcask_nifs.c"
click node_nif_compat "https://github.com/basho/bitcask/blob/develop/c_src/erl_nif_compat.h"
click node_nif_util "https://github.com/basho/bitcask/blob/develop/c_src/erl_nif_util.c"
click node_hash "https://github.com/basho/bitcask/blob/develop/c_src/murmurhash.c"
click node_build "https://github.com/basho/bitcask/blob/develop/rebar.config"
click node_makefile "https://github.com/basho/bitcask/blob/develop/Makefile"
click node_schemas "https://github.com/basho/bitcask/blob/develop/priv/bitcask.schema"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_app,node_app_spec,node_supervisor toneBlue
class node_file,node_io,node_fileops,node_lockops,node_shared_defs,node_time toneAmber
class node_merge_worker,node_merge_delete,node_bump toneMint
class node_nif_module,node_nif_c,node_nif_compat,node_nif_util,node_hash toneRose
class node_build,node_makefile,node_schemas,node_packaging toneIndigo
```

## Components

### OTP runtime

- [Bitcask API](https://github.com/basho/bitcask/blob/develop/src/bitcask.erl): Erlang API · Consumer entry point for opening and using stores. · `src/bitcask.erl`
- [Application callback](https://github.com/basho/bitcask/blob/develop/src/bitcask_app.erl): OTP application · `src/bitcask_app.erl`
- [Application spec](https://github.com/basho/bitcask/blob/develop/src/bitcask.app.src): OTP metadata · `src/bitcask.app.src`
- [Store supervisor](https://github.com/basho/bitcask/blob/develop/src/bitcask_sup.erl): OTP supervisor · Owns long-lived service processes. · `src/bitcask_sup.erl`

### Storage engine

- [Data files](https://github.com/basho/bitcask/blob/develop/src/bitcask_file.erl): log-structured storage · Active files receive append-only records. · `src/bitcask_file.erl`
- [Filesystem I/O](https://github.com/basho/bitcask/blob/develop/src/bitcask_io.erl): I/O boundary · `src/bitcask_io.erl`
- [File operations](https://github.com/basho/bitcask/blob/develop/src/bitcask_fileops.erl): filesystem boundary · `src/bitcask_fileops.erl`
- [Lock coordination](https://github.com/basho/bitcask/blob/develop/src/bitcask_lockops.erl): concurrency boundary · Protects mutable store state during access and maintenance. · `src/bitcask_lockops.erl`
- [Shared store definitions](https://github.com/basho/bitcask/blob/develop/include/bitcask.hrl): Erlang header · `include/bitcask.hrl`
- [Time & expiry](https://github.com/basho/bitcask/blob/develop/src/bitcask_time.erl): expiry service · `src/bitcask_time.erl`

### Maintenance

- [Merge worker](https://github.com/basho/bitcask/blob/develop/src/bitcask_merge_worker.erl): background worker · Rewrites live records into replacement files. · `src/bitcask_merge_worker.erl`
- [Deferred file deletion](https://github.com/basho/bitcask/blob/develop/src/bitcask_merge_delete.erl): cleanup worker · Removes obsolete files after replacement visibility. · `src/bitcask_merge_delete.erl`
- [State advancement](https://github.com/basho/bitcask/blob/develop/src/bitcask_bump.erl): maintenance state · `src/bitcask_bump.erl`

### Native runtime

- [Erlang NIF bridge](https://github.com/basho/bitcask/blob/develop/src/bitcask_nifs.erl): native interface · `src/bitcask_nifs.erl`
- [NIF implementation](https://github.com/basho/bitcask/blob/develop/c_src/bitcask_nifs.c): C NIF · ABI-sensitive code executing inside the Erlang VM. · `c_src/bitcask_nifs.c`
- [NIF compatibility](https://github.com/basho/bitcask/blob/develop/c_src/erl_nif_compat.h): C header · `c_src/erl_nif_compat.h`
- [NIF utilities](https://github.com/basho/bitcask/blob/develop/c_src/erl_nif_util.c): C support · `c_src/erl_nif_util.c`
- [MurmurHash](https://github.com/basho/bitcask/blob/develop/c_src/murmurhash.c): C hashing · `c_src/murmurhash.c`

### Build & operations

- [Build configuration](https://github.com/basho/bitcask/blob/develop/rebar.config): rebar project · Builds Erlang code and C NIF code. · `rebar.config`
- [Build entry point](https://github.com/basho/bitcask/blob/develop/Makefile): Make build · `Makefile`
- [Operational schemas](https://github.com/basho/bitcask/blob/develop/priv/bitcask.schema): runtime schemas · Includes multi-backend and scenario-driven operation artifacts. · `priv/bitcask.schema`
- **Distribution packaging**: package targets · Debian, RPM, and Solaris packaging.

## Connections

- 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
