Go framework for services on local LMDB state (purego, CGO_ENABLED=0)
  • Go 99.4%
  • Dockerfile 0.6%
Find a file
Be Dangerous bb0c7fe47f Start arete with the lmdb and user packages
lmdb is the purego binding from the lmdb-purego-spike, moved over with its
tests: the single-writer actor, the primary and its guards, ReadPool/View,
Health, Resize and the Watchdog. Two API changes:

- GetNoPin is dropped (it broke the cgo pointer rules).
- Getter is added (Get, GetU64, GetString), satisfied by both *Txn and
  *ReadTxn, so packages can compose reads in one snapshot. GetU64 and
  GetString copy the key into the txn's pinned buffer, so lookups through
  the interface don't allocate.

user stores user records with a unique, case-insensitive email index. Each
operation has a composable form (Get/GetByEmail on a Getter,
CreateTx/UpdateTx on a write txn) and a convenience form. Ids come from a
sequence and are never reused. Changing an email re-indexes it atomically.
OpenReadOnly serves reader processes.

Tests pass on macOS (LMDB 1.0.1, -race) and Alpine amd64 (0.9.33, musl,
non-root).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 23:49:10 -06:00
lmdb Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
user Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
.dockerignore Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
.gitignore Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
Dockerfile Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
go.mod Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
go.sum Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00
README.md Start arete with the lmdb and user packages 2026-11-11 23:49:10 -06:00

arete

A Go framework for building services on one host's local state: LMDB is reached through purego, so binaries build with CGO_ENABLED=0 and ship on Alpine with just apk add lmdb.

Package What it is
lmdb LMDB binding with zero-copy, allocation-free reads. Contains the single-writer actor (Writer), the primary and its guards, the scoped read pool (ReadPool/View), Health, Resize and a Watchdog.
user User records with a unique, case-insensitive email index.

The design and measurements come from the lmdb-purego-spike experiments:

  • purego on musl.
  • LMDB vs SQLite under an IDP load.
  • Scaling on a 40-thread 2-socket box.
  • Truncation guards.
  • Sharing an env across containers.
  • Supervision.

Model

  • One primary process per env holds the writer lease.
    • All writes go through its Writer, one goroutine locked to an OS thread.
    • A write txn must not change threads, and LMDB's writer lock is a robust mutex.
  • Reads go through ReadPool.View.
    • Each View renews a snapshot, runs fn, and resets it, even on panic, so a read txn can't leak and pin a snapshot.
    • Values alias the memory map and are valid only inside fn.
  • Packages compose.
    • Every read takes an lmdb.Getter (a *ReadTxn in a View, or a *Txn in a write).
    • Every write takes a *lmdb.Txn.
    • So a service can read a session and its user in one snapshot, or create a user and its first session atomically.
  • Supervision follows Erlang's shape:
    • Guards prevent what can be prevented: WRITEMAP is refused, there's one writer lease, readers are read-only, and readers must share the primary's PID namespace.
    • A Watchdog catches slow failures: stale reader slots, pinned snapshots, and the map filling up.
    • The process supervisor handles the rest.
    • Suggested tree, rest_for_one: Primary → Writer → Pool → Watchdog.
p, err := lmdb.OpenPrimary(dir, lmdb.Config{MapSize: 1 << 30, MaxDBs: user.DBIs})
users, err := user.Open(p)

u, err := users.Create("ada@example.com", "Ada")
u, err = users.ByEmail("ADA@example.com")

// compose: several lookups in one snapshot, zero-copy until decoded
err = p.Pool.View(func(t *lmdb.ReadTxn) error {
	u, err := users.Get(t, id)
	...
})

user

  • Records: ID (assigned from 1, never reused), Email (trimmed, lower case, unique), Name, Status (active/disabled), Created and Updated.
  • Reads: Get and GetByEmail are composable. ByID, ByEmail and List (id-ordered pages) are convenience wrappers.
  • Writes: CreateTx and UpdateTx are composable. Create, Update and SetStatus are convenience wrappers.
  • Email changes re-index atomically. The old address is freed, and someone else's address is refused with ErrEmailTaken.
  • Reader processes use OpenReadOnly(pool).
  • Out of scope: passwords and credentials.

On an M2 Pro, a lookup on a held handle takes ~450 ns by id and ~750 ns by email. The only allocations are the copies of the returned strings.

Requirements and rules

  • LMDB 0.9.x in production (Alpine lmdb, 0.9.33). Homebrew ships 1.0.x. The file formats are incompatible, so pin one version per deployment.
  • The env must be on a local filesystem. Containers sharing it must share the primary's PID namespace.
  • Back up with mdb_copy. Restore into a new directory and restart. Never copy over a live data.mdb: the process dies with SIGBUS, and no code can catch it.
  • Don't nest Views on one goroutine, and don't call View inside Writer.Update. Either can deadlock against a resize.

Tests

CGO_ENABLED=0 go test -race ./...                     # macOS (-race needs cgo on Linux)
docker build -t arete-test . && docker run --rm arete-test   # Alpine, musl, non-root