sled for Go

github.com/malcolmston/sled

An embedded, transactional, crash-safe key/value store in pure Go.

 GitHubports spacejam/sled

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

shell
$ go get github.com/malcolmston/sled

Quick start

main.go
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 — Open replays the log, stops at the first torn or CRC-mismatched record, and truncates the partial tail
  • Serializable transactions — DB.Update commits atomically or rolls back on error/panic; DB.View reads a stable snapshot
  • Atomic batches — stage many writes with DB.Batch / NewBatch and land them as one all-or-nothing durable record
  • Ordered range scans — DB.Scan over a Range (Lower/Upper/Prefix) yields keys in ascending order via Iterator
  • Immutable persistent-treap index published with atomic.Pointer, so DB.Get / DB.Has take 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.Compact rewrites the log to the live key set and installs it atomically with a rename
  • Tunable durability — fsync-per-commit by default, or WithSyncWrites(false) / WithFileMode at Open
  • Zero dependencies — pure Go standard library, no cgo, nothing to audit but the toolchain
sled