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.
Installation avec revue préalable
Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.
npx -y tokrepo@latest install 6336afae-3920-11f1-9bc6-00163e2b0d79 --target codexDry-run d'abord, confirmez les écritures, puis lancez cette commande.
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.
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Fil de discussion
Actifs similaires
RocksDB — Facebook's Embeddable Persistent Key-Value Store
A C++ LSM-tree storage engine from Meta powering MyRocks, CockroachDB, TiKV, Kafka Streams and more. Optimized for fast SSDs and workloads with high write throughput and low read latency.
imgproxy — Fast Secure Image Processing Server in Go
Resize, crop, and convert images on-the-fly with imgproxy. A blazing-fast Go server powered by libvips for production-grade image transformation at scale.
fzf — Blazing Fast Command-Line Fuzzy Finder
fzf is a general-purpose command-line fuzzy finder written in Go. Blazing fast, portable, and composable with any list-producing command. Interactive picker for files, commands, history, git branches, processes, and more.
Zola — Fast Static Site Generator in a Single Rust Binary
Zola is a fast static site generator built as a single Rust binary with everything built in: Sass compilation, syntax highlighting, table of contents, search index, image processing, and shortcodes. No external dependencies or plugins needed.