# goleveldb — Pure Go Implementation of LevelDB Key-Value Store > Embed a fast, sorted key-value storage engine in your Go application without any CGo dependencies. ## Install Save in your project root: # goleveldb — Pure Go Implementation of LevelDB Key-Value Store ## Quick Use ```go package main import ( "github.com/syndtr/goleveldb/leveldb" "log" ) func main() { db, err := leveldb.OpenFile("./mydb", nil) if err != nil { log.Fatal(err) } defer db.Close() db.Put([]byte("hello"), []byte("world"), nil) val, _ := db.Get([]byte("hello"), nil) log.Println(string(val)) // world } ``` ## Introduction goleveldb is a pure Go implementation of Google's LevelDB key-value store. It provides an embedded, persistent, sorted key-value database that compiles and runs anywhere Go does, with no CGo or external C library required. ## What goleveldb Does - Stores key-value pairs sorted by key in a persistent on-disk database - Supports Get, Put, Delete, and ordered iteration over key ranges - Provides atomic batch writes for grouping multiple operations - Implements LSM-tree (Log-Structured Merge-tree) storage for write-optimized workloads - Offers Bloom filters, LRU caches, and compression to tune read/write performance ## Architecture Overview goleveldb follows the classic LSM-tree design. Writes go to an in-memory table (memtable) backed by a write-ahead log for durability. When the memtable fills, it is flushed to a sorted SSTable file on disk. Background compaction merges SSTables across levels to reclaim space and maintain read performance. The implementation mirrors the original LevelDB design but is written entirely in Go. ## Self-Hosting & Configuration - Add to your project: `go get github.com/syndtr/goleveldb/leveldb` - Open a database with `leveldb.OpenFile(path, opts)` — the directory is created automatically - Tune write buffer size, compaction thresholds, and block cache size via `opt.Options` - Enable Bloom filters to reduce disk reads on key lookups - Use snapshots for consistent point-in-time reads during iteration ## Key Features - Pure Go with zero CGo dependencies, cross-compiles to any Go target - Sorted key ordering enables efficient range scans and prefix iteration - Batch API for atomic multi-key writes with a single fsync - Built-in Snappy compression reduces on-disk storage size - Thread-safe: concurrent reads and writes from multiple goroutines ## Comparison with Similar Tools - **BadgerDB** — Go-native KV store with value separation for large values; goleveldb stores everything in SSTables - **BoltDB / bbolt** — B+ tree design optimized for reads; goleveldb's LSM-tree favors write-heavy workloads - **Pebble** — CockroachDB's LevelDB-inspired store with advanced compaction; heavier but more feature-rich - **Original LevelDB (C++)** — the reference implementation; goleveldb avoids CGo at the cost of some performance ## FAQ **Q:** Is goleveldb production-ready? A: Yes. It has been used in production by many Go projects, including early versions of go-ethereum. **Q:** How does performance compare to the original C++ LevelDB? A: Throughput is somewhat lower due to Go runtime overhead, but the difference is negligible for most workloads. The main advantage is CGo-free compilation. **Q:** Can multiple processes access the same database? A: No. LevelDB uses file-level locking for single-process access only. Use a server wrapper for multi-process scenarios. **Q:** What license does goleveldb use? A: BSD 2-Clause license. ## Sources - https://github.com/syndtr/goleveldb - https://pkg.go.dev/github.com/syndtr/goleveldb/leveldb --- Source: https://tokrepo.com/en/workflows/asset-07308605 Author: AI Open Source