sled for Go
An embedded, transactional, crash-safe key/value store in pure Go.
A small embedded key/value store written in pure Go with no third-party dependencies and no cgo, inspired by the Rust sled crate. All state lives in one append-only write-ahead log: every durable mutation — a single Set/Delete, a Batch, or a transaction — is encoded as exactly one length-prefixed, CRC-32 checksummed record and appended to the log, so groups of writes are applied all-or-nothing on recovery. The in-memory index is an immutable, ordered persistent treap published through a single atomic.Pointer store, giving lock-free snapshot reads that never race the writer. On Open the log is replayed and stops at the first torn or corrupt record, so a crash mid-write loses only the in-flight record and the partial tail is physically truncated. On top sit atomic Batch commits, serializable Update/View transactions, ordered prefix and bounded Scan, and a rename-atomic Compact.
Install
$ go get github.com/malcolmston/sledQuick start
import "github.com/malcolmston/sled"
db, _ := sled.Open("data.sled")
defer db.Close()
db.Set([]byte("greeting"), []byte("hello"))
v, ok, _ := db.Get([]byte("greeting"))
if ok {
fmt.Printf("%s\n", v) // hello
}
db.Delete([]byte("greeting"))Features
- Durable append-only WAL — every commit is one length-prefixed, CRC-32 checksummed record appended via
DB.Set/DB.Delete - Real crash recovery —
Openreplays the log, stops at the first torn or CRC-mismatched record, and truncates the partial tail - Serializable transactions —
DB.Updatecommits atomically or rolls back on error/panic;DB.Viewreads a stable snapshot - Atomic batches — stage many writes with
DB.Batch/NewBatchand land them as one all-or-nothing durable record - Ordered range scans —
DB.Scanover aRange(Lower/Upper/Prefix) yields keys in ascending order viaIterator - Immutable persistent-treap index published with
atomic.Pointer, soDB.Get/DB.Hastake snapshots with no locks - Lock-free concurrent readers — a single writer is serialized while any number of readers proceed race-free (verified under
-race) - Compaction —
DB.Compactrewrites the log to the live key set and installs it atomically with a rename - Tunable durability — fsync-per-commit by default, or
WithSyncWrites(false)/WithFileModeatOpen - Zero dependencies — pure Go standard library, no cgo, nothing to audit but the toolchain