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 engine stack from top to bottom: the application calls the Database, which holds the graph, vector, full-text, stream and query layers, the per-transaction overlays and the single-writer rule. The Database drives the B+Trees, the transaction manager and the checkpointer, and reads vector and posting pages from the buffer pool directly. The transaction manager writes through the write-ahead log, which writes its own file through the virtual file system. The B+Trees go through the buffer pool; the checkpointer uses the buffer pool, the page manager and the log. The buffer pool sits on the page manager, and writes every flush through the journal, its own -journal file, before the pages go in place; both reach the virtual file system, and finally the operating system

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

  1. Virtual File System - Abstracting file I/O for portability and testing
  2. Portable Databases - Serializing to bytes, and running with no files at all
  3. Page Manager - Fixed-size pages, allocation, and checksums
  4. Buffer Pool - Caching pages in memory with eviction
  5. B+Tree - Ordered key-value storage with efficient lookups

Durability & Transactions

  1. Write-Ahead Log - Durability through logging committed changes
  2. Transaction Manager - Transactions with begin/commit/abort
  3. Checkpointing - Flushing dirty pages and resetting the log
  4. Recovery - Redo-only crash recovery from the log

Data Model

  1. Graph Storage - Nodes, edges, labels, and properties
  2. Durable Streams - Transactional event records and semantic graph changes

Search Indexes

  1. Vector Search - HNSW approximate nearest neighbor search
  2. Full-Text Search - BM25-scored inverted index, per-property declarations, and search as an access path

Query System

  1. 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.

Traditional access deserialises page bytes into an in-memory object, modifies it, and serialises back. LatticeDB searches, inserts and deletes leaf entries in the page bytes in place through calculated offsets, with no node object; copies still happen for values returned by get and when splits, merges and redistribution rebuild a page

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.