Skip to content

scalarian/lattice.db

LATTICE.DB

Go Version License Go Report Release

A local-first embedded database with secure end-to-end encrypted multi-device sync.

Features

  • Local-first data storage with full offline functionality
  • Secure sync across devices with end-to-end encryption
  • ACID transactions with serializable isolation
  • CRDT-based conflict resolution
  • WAL-based durability with automatic checkpointing
  • Production-grade reliability with checksums, verification, and repair tools

Documentation

Installation

Go Module

go get github.com/lattice-db/lattice@latest

Docker

docker pull ghcr.io/lattice-db/lattice:latest

Binaries

Download pre-built binaries from the Releases page.

Quick Start

package main

import (
    "fmt"
    "log"

    "github.com/lattice-db/lattice/pkg/lattice"
)

func main() {
    // Open a database
    db, err := lattice.Open("./data", nil)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Start a writable transaction
    tx, err := db.Begin(true)
    if err != nil {
        log.Fatal(err)
    }

    // Create a collection
    err = tx.CreateCollection("users", &lattice.Schema{
        PrimaryKey: "id",
        Fields: map[string]lattice.FieldDef{
            "id":    {Type: lattice.TypeString, Required: true},
            "name":  {Type: lattice.TypeString, Required: true},
            "email": {Type: lattice.TypeString},
        },
    })
    if err != nil {
        log.Fatal(err)
    }

    // Insert a document
    doc := lattice.NewDocument()
    doc.Set("id", "user-1")
    doc.Set("name", "Alice")
    doc.Set("email", "alice@example.com")
    err = tx.Insert("users", doc)
    if err != nil {
        log.Fatal(err)
    }

    // Commit transaction
    err = tx.Commit()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Database initialized successfully")
}

CLI Usage

# Initialize a database
lattice init ./data

# Store a value
lattice put user:1 '{"name":"Alice"}'

# Retrieve a value
lattice get user:1

# Verify database integrity
lattice verify

# Start sync server
lattice-server --addr=:8080

Architecture

Storage Engine

  • WAL (Write-Ahead Log) for crash recovery
  • Segmented storage with automatic compaction
  • Page-based structure with checksums on every page

Transactions

  • MVCC (Multi-Version Concurrency Control) for consistent reads
  • Serializable isolation with conflict detection

CRDT Layer

  • LWW Register for last-writer-wins values
  • OR Set for add-wins sets
  • Map of CRDT values for nested structures
  • Counter with increment and decrement support

Security

  • ECDH for key exchange
  • AES-256-GCM for encryption
  • Ed25519 for signatures

Repository Layout

lattice.db/
├── cmd/                    # CLI tools
├── pkg/                    # Public APIs
├── internal/               # Private implementation
├── docs/                   # Documentation
├── spec/                   # Formal specifications
├── rfcs/                   # Design proposals
└── testdata/               # Test fixtures

Links

License

Copyright (c) 2025 LATTICE.DB contributors. See LICENSE for details.