High-performance, lock-free in-memory database with MVCC (multi-version concurrency control), snapshots, TTL, batches, atomic ops, and metrics — written in Go.
- Lock-free algorithms with snapshot isolation (MVCC)
- Epoch-based garbage collection and safe memory reclamation
- TTL and absolute expiry support
- Atomic ops for numeric types (add/inc/dec/mul/div)
- Batch puts/gets and snapshot iterators
- Metrics and observability hooks
go get github.com/kianostad/lfdb- Requires Go 1.24+ (matches
go.modand CI matrix)
package main
import (
"context"
"fmt"
"time"
"github.com/kianostad/lfdb"
)
func main() {
ctx := context.Background()
db := lfdb.NewStringDB[string]()
defer db.Close(ctx)
// CRUD
db.Put(ctx, "hello", "world")
if v, ok := db.Get(ctx, "hello"); ok { fmt.Println(v) }
// Txn
_ = db.Txn(ctx, func(tx lfdb.StringKeyTxn[string]) error {
tx.Put(ctx, "k1", "v1")
tx.Put(ctx, "k2", "v2")
return nil
})
// Snapshot
snap := db.Snapshot(ctx)
defer snap.Close(ctx)
snap.Iterate(ctx, func(k, v string) bool { fmt.Println(k, v); return true })
// TTL
db.PutWithTTL(ctx, "temp", "value", 5*time.Second)
if ttl, ok := db.GetTTL(ctx, "temp"); ok { fmt.Println("TTL:", ttl) }
// Atomic
cdb := lfdb.NewStringDB[int]()
cdb.Add(ctx, "counter", 5)
cdb.Increment(ctx, "counter")
}For maximum performance and zero-copy key handling:
db := lfdb.New[[]byte, string]()
defer db.Close(context.Background())
db.Put(ctx, []byte("hello"), "world")- CRUD:
Put,Get,Delete - Transactions:
Txn(func(tx ...){ ... })withtx.Put,tx.Get,tx.Deleteand atomic/TTL helpers - Snapshots:
Snapshot(ctx)withGetandIterate - TTL:
PutWithTTL,PutWithExpiry,GetTTL,ExtendTTL,RemoveTTL - Atomic (numeric):
Add,Increment,Decrement,Multiply,Divide - Batch:
NewStringBatch/NewBatch,ExecuteBatch,BatchPut,BatchGet,BatchPutWithTTL
See runnable examples in examples/string_api/main.go and examples/basic_usage.go.
- Access a snapshot of counters and gauges via
db.GetMetrics(ctx). - A demo showing how to record and inspect metrics lives in
examples/enhanced_metrics_demo.go.
- REPL:
go run cmd/repl/main.go - Benchmarks:
go run cmd/bench/main.goormake bench
Use make help for a full list. Common tasks:
make test: Run tests with race detectormake bench: Run benchmarksmake format: Format withgoimportsmake lint: Rungolangci-lintmake lint-all: Lint + security (gosec) + vuln checkmake build: Build all binaries incmd/
Advanced test suites:
make fuzz|make property|make linearizability|make race-detection|make gcmake test-allto run everything;make all-teststo include benches
- Architecture:
docs/ARCHITECTURE.md - Contributing:
docs/CONTRIBUTING.md - Makefile overview:
docs/MAKEFILE.md
- Always
defer db.Close(ctx)to release resources - Use transactions for related writes; snapshots for consistent reads
- Prefer batch ops for large workloads
- Call
ManualGC(ctx)periodically in long-running processes - Keep keys reasonably small; use TTL for temporary data
- Local:
make testorgo test ./... - CI: GitHub Actions with multi-arch, race, coverage, fuzz, property, and security scanning
MIT — see LICENSE.