# etcd-io/bbolt architecture

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

- Interactive diagram: https://gitdiagram.com/etcd-io/bbolt
- Repository: https://github.com/etcd-io/bbolt

## Overview

bbolt is an embedded, file-backed Go key/value database, with a companion CLI for inspecting and repairing database files. The main workflow opens a database, runs read or write transactions, and uses buckets to read or modify keys; the engine manages pages and free space. The README also documents backups and statistics, while the CLI tree includes checking, dumping, compaction, inspection, and surgery. Some CLI implementations were not sampled, so their internal connections are omitted where the evidence does not establish them.

Main entry points: [`db.go`](https://github.com/etcd-io/bbolt/blob/main/db.go) (Database), [`cmd/bbolt/command`](https://github.com/etcd-io/bbolt/tree/main/cmd/bbolt/command) (Admin Commands), [`internal/common/page.go`](https://github.com/etcd-io/bbolt/blob/main/internal/common/page.go) (Page Format), [`tx.go`](https://github.com/etcd-io/bbolt/blob/main/tx.go) (Transactions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Database API"]
  node_dbapi["Database<br/>[db.go]"]
  node_txn["Transactions<br/>[tx.go]"]
  node_bucket["Buckets<br/>[bucket.go]"]
  node_cursor["Key Cursor<br/>[cursor.go]"]
end

subgraph group_engine["Storage Engine"]
  node_btree["B-tree Nodes<br/>[node.go]"]
  node_pageformat["Page Format<br/>[page.go]"]
  node_metadata["Metadata<br/>[meta.go]"]
  node_freelist["Free Pages<br/>[shared.go]"]
  node_freelistmap["Free Map<br/>[hashmap.go]"]
end

subgraph group_cli["CLI Operations"]
  node_cli["bbolt CLI<br/>[main.go]"]
  node_cliops["Admin Commands"]
  node_bench["Benchmark<br/>[command_bench.go]"]
  node_surgerycmd["Surgery Commands<br/>[command_surgery.go]"]
  node_compact["Compaction<br/>[compact.go]"]
end

subgraph group_raw["Raw File Tools"]
  node_rawio["Page Access<br/>[guts_cli.go]"]
  node_surgeon["File Surgery<br/>[surgeon.go]"]
  node_xray["Page Tracing<br/>[xray.go]"]
end

node_app(("Application"))
node_dbfile[("Database File")]
node_cliuser(("CLI User"))

node_app -->|"opens"| node_dbapi
node_dbapi -->|"starts"| node_txn
node_txn -->|"operates on"| node_bucket
node_bucket -->|"iterates with"| node_cursor
node_dbapi -->|"maps file"| node_dbfile
node_dbapi -->|"uses"| node_freelist
node_dbapi -->|"uses"| node_pageformat
node_bucket -->|"uses"| node_pageformat
node_txn -->|"uses"| node_pageformat
node_btree -->|"uses"| node_pageformat
node_freelist -.->|"implemented by"| node_freelistmap
node_cliuser -->|"runs"| node_cli
node_cli -->|"dispatches"| node_cliops
node_cliops -.->|"accesses"| node_dbapi
node_bench -->|"benchmarks"| node_dbapi
node_surgerycmd -->|"invokes"| node_surgeon
node_surgeon -->|"reads and writes"| node_rawio
node_xray -->|"reads pages"| node_rawio
node_cliops -.->|"inspects pages"| node_rawio
node_cliops -.->|"compacts"| node_compact

click node_dbapi "https://github.com/etcd-io/bbolt/blob/main/db.go"
click node_txn "https://github.com/etcd-io/bbolt/blob/main/tx.go"
click node_bucket "https://github.com/etcd-io/bbolt/blob/main/bucket.go"
click node_cursor "https://github.com/etcd-io/bbolt/blob/main/cursor.go"
click node_btree "https://github.com/etcd-io/bbolt/blob/main/node.go"
click node_pageformat "https://github.com/etcd-io/bbolt/blob/main/internal/common/page.go"
click node_metadata "https://github.com/etcd-io/bbolt/blob/main/internal/common/meta.go"
click node_freelist "https://github.com/etcd-io/bbolt/blob/main/internal/freelist/shared.go"
click node_freelistmap "https://github.com/etcd-io/bbolt/blob/main/internal/freelist/hashmap.go"
click node_cli "https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/main.go"
click node_cliops "https://github.com/etcd-io/bbolt/tree/main/cmd/bbolt/command"
click node_bench "https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/command/command_bench.go"
click node_surgerycmd "https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/command/command_surgery.go"
click node_rawio "https://github.com/etcd-io/bbolt/blob/main/internal/guts_cli/guts_cli.go"
click node_surgeon "https://github.com/etcd-io/bbolt/blob/main/internal/surgeon/surgeon.go"
click node_xray "https://github.com/etcd-io/bbolt/blob/main/internal/surgeon/xray.go"
click node_compact "https://github.com/etcd-io/bbolt/blob/main/compact.go"

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_dbapi,node_txn,node_bucket,node_cursor,node_cliuser toneBlue
class node_btree,node_pageformat,node_metadata,node_freelist,node_freelistmap,node_dbfile toneAmber
class node_cli,node_cliops,node_bench,node_surgerycmd,node_compact toneMint
class node_rawio,node_surgeon,node_xray toneRose
class node_app toneIndigo
```

## Components

### Database API

- [Database](https://github.com/etcd-io/bbolt/blob/main/db.go): `db.go`
- [Transactions](https://github.com/etcd-io/bbolt/blob/main/tx.go): `tx.go`
- [Buckets](https://github.com/etcd-io/bbolt/blob/main/bucket.go): `bucket.go`
- [Key Cursor](https://github.com/etcd-io/bbolt/blob/main/cursor.go): `cursor.go`

### Storage Engine

- [B-tree Nodes](https://github.com/etcd-io/bbolt/blob/main/node.go): `node.go`
- [Page Format](https://github.com/etcd-io/bbolt/blob/main/internal/common/page.go): `internal/common/page.go`
- [Metadata](https://github.com/etcd-io/bbolt/blob/main/internal/common/meta.go): `internal/common/meta.go`
- [Free Pages](https://github.com/etcd-io/bbolt/blob/main/internal/freelist/shared.go): `internal/freelist/shared.go`
- [Free Map](https://github.com/etcd-io/bbolt/blob/main/internal/freelist/hashmap.go): `internal/freelist/hashmap.go`

### CLI Operations

- [bbolt CLI](https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/main.go): `cmd/bbolt/main.go`
- [Admin Commands](https://github.com/etcd-io/bbolt/tree/main/cmd/bbolt/command): `cmd/bbolt/command`
- [Benchmark](https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/command/command_bench.go): `cmd/bbolt/command/command_bench.go`
- [Surgery Commands](https://github.com/etcd-io/bbolt/blob/main/cmd/bbolt/command/command_surgery.go): `cmd/bbolt/command/command_surgery.go`
- [Compaction](https://github.com/etcd-io/bbolt/blob/main/compact.go): `compact.go`

### Raw File Tools

- [Page Access](https://github.com/etcd-io/bbolt/blob/main/internal/guts_cli/guts_cli.go): `internal/guts_cli/guts_cli.go`
- [File Surgery](https://github.com/etcd-io/bbolt/blob/main/internal/surgeon/surgeon.go): `internal/surgeon/surgeon.go`
- [Page Tracing](https://github.com/etcd-io/bbolt/blob/main/internal/surgeon/xray.go): `internal/surgeon/xray.go`

### Other components

- **Application**
- **Database File**
- **CLI User**

## Connections

- Application → Database: opens
- Database → Transactions: starts
- Transactions → Buckets: operates on
- Buckets → Key Cursor: iterates with
- Database → Database File: maps file (evidence: `db.go`)
- Database → Free Pages: uses (evidence: `db.go`)
- Database → Page Format: uses (evidence: `db.go`)
- Buckets → Page Format: uses (evidence: `bucket.go`)
- Transactions → Page Format: uses (evidence: `tx.go`)
- B-tree Nodes → Page Format: uses (evidence: `node.go`)
- Free Pages → Free Map: implemented by
- CLI User → bbolt CLI: runs (evidence: `cmd/bbolt/main.go`)
- bbolt CLI → Admin Commands: dispatches (evidence: `cmd/bbolt/main.go`)
- Admin Commands → Database: accesses
- Benchmark → Database: benchmarks (evidence: `cmd/bbolt/command/command_bench.go`)
- Surgery Commands → File Surgery: invokes (evidence: `cmd/bbolt/command/command_surgery.go`)
- File Surgery → Page Access: reads and writes (evidence: `internal/surgeon/surgeon.go`)
- Page Tracing → Page Access: reads pages (evidence: `internal/surgeon/xray.go`)
- Admin Commands → Page Access: inspects pages
- Admin Commands → Compaction: compacts
