Architecture Overview
This section explains how LatticeDB's storage engine works, from the ground up. Each chapter builds on the previous, showing how simple primitives combine to create a durable, transactional graph database with vector search, full-text search, and local event streams.
LatticeDB is an embedded, single-file property-graph database with vector and BM25 full-text indexes, durable named streams, and a built-in graph changefeed. These chapters explain that substrate: local storage, graph layout, streams, vector search, full-text search, and the query layer over all of them.
The Stack
The Database (src/storage/database.zig) is the orchestrator. It owns every
component below it, stages each transaction's writes in an in-memory overlay, and at
commit time logs that overlay, has the transaction manager write and sync the commit
record, and only then applies the overlay to the stores. The transaction manager
only writes log records; it does not touch the B+Trees or the checkpointer. Dirty
pages reach the database file only in whole flushes that go through the journal.
Chapters
Storage Engine
- Virtual File System - Abstracting file I/O for portability and testing
- Portable Databases - Serializing to bytes, and running with no files at all
- Page Manager - Fixed-size pages, allocation, and checksums
- Buffer Pool - Caching pages in memory with eviction
- B+Tree - Ordered key-value storage with efficient lookups
Durability & Transactions
- Write-Ahead Log - Durability through logging committed changes
- Transaction Manager - Transactions with begin/commit/abort
- Checkpointing - Flushing dirty pages and resetting the log
- Recovery - Redo-only crash recovery from the log
Data Model
- Graph Storage - Nodes, edges, labels, and properties
- Durable Streams - Transactional event records and semantic graph changes
Search Indexes
- Vector Search - HNSW approximate nearest neighbor search
- Full-Text Search - BM25-scored inverted index, per-property declarations, and search as an access path
Query System
- Query Execution - Volcano iterator model, operators, and planning
Design Principles
Direct Page Manipulation
We don't deserialize B+Tree pages into node objects. Searches and in-place inserts read and write bytes directly in the page buffer at calculated offsets, with no intermediate representation of the node.
It is not copy-free end to end. get returns an owned copy of the value
(getBorrowed lends it from the pinned leaf instead), and splits, merges and
redistribution rebuild pages from a temporary list of their entries.
Everything in the File is Pages
The database file is built on fixed-size pages. The size is 4 KB by default and is chosen when the database is created (4096 to 65535 bytes); it is recorded in the file header and cannot change afterwards.
- B+Tree nodes are pages
- Large B+Tree values spill into linked overflow pages
- Vector data and HNSW neighbour lists live in their own page types
- The file header is page 0
- Free pages form a linked list of pages
This uniformity keeps the file simple: one page format and one caching layer for
everything in it. Two files sit beside it. The write-ahead log is a separate
<database>-wal file of page-sized frames, written directly through the VFS rather
than through the buffer pool. The flush journal, <database>-journal, holds a copy
of the pages a flush is writing, so a crash part way through can be finished; it is
empty between flushes and deleted at a clean close.
Durability Through WAL
A transaction's changes are held in memory until commit. At commit they are logged, the commit record is forced to disk, and only then are they applied:
- Commit = commit record is on disk (fast, sequential write)
- Data pages are written later, all dirty pages at once, through the journal, so the file moves from one whole image to the next
- Crash recovery re-executes committed log records onto that image
Simple Concurrency Model
A database allows one read-write transaction at a time; a second one is refused rather than queued. The C API also serialises the calls on a database handle behind one mutex. Below that, each buffer-pool frame has a reader-writer latch, which protects a page while it is being read or modified. There are no complex lock hierarchies - simplicity over maximum concurrency.