BadgerDB — Fast Embeddable Key-Value Store in Pure Go
A pure-Go LSM-tree database built for SSDs, the storage engine behind Dgraph. No CGO, ACID transactions, encryption at rest, tiny binary — ideal for Go services that need embedded persistence.
What it is
BadgerDB is a high-performance embeddable key-value database written in pure Go. It uses an LSM-tree (Log-Structured Merge-tree) architecture optimized for SSDs, separating keys from values to reduce write amplification. BadgerDB powers the Dgraph graph database as its storage engine.
The target audience includes Go developers who need an embedded database without CGO dependencies, systems engineers building local storage layers, and projects that require ACID transactions with encryption at rest in a single binary.
How it saves time or tokens
BadgerDB eliminates the need for external database processes. Your Go application embeds the database directly, removing network overhead, connection pooling, and deployment complexity. A key-value store that would require deploying and managing Redis or RocksDB becomes a single go get and a few lines of code.
The pure-Go implementation means cross-compilation works out of the box. No CGO, no system library dependencies, no build flags -- just go build.
How to use
- Install BadgerDB:
go get github.com/dgraph-io/badger/v4
- Open a database and perform basic operations:
package main
import (
"fmt"
"log"
badger "github.com/dgraph-io/badger/v4"
)
func main() {
opts := badger.DefaultOptions("/tmp/badger")
db, err := badger.Open(opts)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Write
err = db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte("hello"), []byte("world"))
})
// Read
db.View(func(txn *badger.Txn) error {
item, _ := txn.Get([]byte("hello"))
item.Value(func(val []byte) error {
fmt.Printf("Value: %s\n", val)
return nil
})
return nil
})
}
- Run the application:
go run main.go
Example
Batch writes with a transaction:
wb := db.NewWriteBatch()
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("key-%d", i)
val := fmt.Sprintf("value-%d", i)
wb.Set([]byte(key), []byte(val))
}
wb.Flush()
Related on TokRepo
- AI tools for database -- Database tools and embedded storage options
- AI tools for coding -- Go developer libraries and frameworks
Common pitfalls
- BadgerDB stores data in a directory on disk. Not setting the directory path or using a tmpfs path in production causes data loss on restart.
- The value log requires periodic garbage collection. Call
db.RunValueLogGC(0.5)in a background goroutine to reclaim space from deleted keys. - Large values (over 1MB) should use the
WithValueThresholdoption to control whether values are stored inline or in the value log. - BadgerDB is designed for SSDs. HDD performance degrades significantly due to random read patterns during compaction.
- In-memory mode (
WithInMemory(true)) is useful for testing but loses all data when the process exits.
Frequently Asked Questions
BoltDB uses a B+ tree and is optimized for read-heavy workloads. BadgerDB uses an LSM tree and is optimized for write-heavy workloads on SSDs. BadgerDB supports concurrent reads and writes, while BoltDB allows only one writer at a time. Choose based on your read/write ratio.
Yes. BadgerDB supports encryption at rest using AES. Enable it by setting the encryption key in the options: opts.WithEncryptionKey(key). The key must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256 respectively.
BadgerDB can handle datasets larger than RAM by keeping keys in memory and values on disk. The key-value separation design means memory usage scales with the number of keys, not the total data size. For datasets with billions of keys, monitor memory usage.
Yes. BadgerDB is designed for concurrent access from multiple goroutines. Transactions provide isolation. Multiple read transactions can run concurrently, and write transactions are serialized internally.
Yes. Set a time-to-live on entries using txn.SetEntry(badger.NewEntry(key, value).WithTTL(time.Hour)). Expired entries are cleaned up during garbage collection.
Citations (3)
- BadgerDB GitHub— BadgerDB is a pure-Go LSM-tree key-value store
- Dgraph Official— BadgerDB powers the Dgraph graph database
- BadgerDB Documentation— LSM-tree architecture separates keys from values for SSD optimization
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.