badger README

The project's README from github.com/dgraph-io/badger. As one markdown file: README.md.

BadgerDB

Go Reference Go Report Card Sourcegraph ci-badger-tests ci-badger-bank-tests ci-badger-bank-tests-nightly

Badger mascot

BadgerDB is an embeddable, persistent and fast key-value (KV) database written in pure Go. It is the underlying database for Dgraph, a fast, distributed graph database. It's meant to be a performant alternative to non-Go-based key-value stores like RocksDB.

Project Status

Badger is stable and is being used to serve data sets worth hundreds of terabytes. Badger supports concurrent ACID transactions with serializable snapshot isolation (SSI) guarantees. A Jepsen-style bank test runs nightly for 8h, with --race flag and ensures the maintenance of transactional guarantees. Badger has also been tested to work with filesystem level anomalies, to ensure persistence and consistency. Badger is being used by a number of projects which includes Dgraph, Jaeger Tracing, UsenetExpress, and many more.

The list of projects using Badger can be found here.

Please consult the Changelog for more detailed information on releases.

Note: Badger is built with go 1.23 and we refrain from bumping this version to minimize downstream effects of those using Badger in applications built with older versions of Go.

Table of Contents

Getting Started

Installing

To start using Badger, install Go 1.23 or above. Badger v3 and above needs go modules. From your project, run the following command

go get github.com/dgraph-io/badger/v4

This will retrieve the library.

Installing Badger Command Line Tool

Badger provides a CLI tool which can perform certain operations like offline backup/restore. To install the Badger CLI, retrieve the repository and checkout the desired version. Then run

cd badger
go install .

This will install the badger command line utility into your $GOBIN path.

Badger Documentation

Badger Documentation is available at https://badger.dgraph.io

Resources

Blog Posts

  1. Introducing Badger: A fast key-value store written natively in Go
  2. Make Badger crash resilient with ALICE
  3. Badger vs LMDB vs BoltDB: Benchmarking key-value databases in Go
  4. Concurrent ACID Transactions in Badger

Design

Badger was written with these design goals in mind:

Badger’s design is based on a paper titled WiscKey: Separating Keys from Values in SSD-conscious Storage.

Comparisons

Feature Badger RocksDB BoltDB
Design LSM tree with value log LSM tree only B+ tree
High Read throughput Yes No Yes
High Write throughput Yes Yes No
Designed for SSDs Yes (with latest research 1) Not specifically 2 No
Embeddable Yes Yes Yes
Sorted KV access Yes Yes Yes
Pure Go (no Cgo) Yes No Yes
Transactions Yes, ACID, concurrent with SSI3 Yes (but non-ACID) Yes, ACID
Snapshots Yes Yes Yes
TTL support Yes Yes No
3D access (key-value-version) Yes4 No No

1 The WISCKEY paper (on which Badger is based) saw big wins with separating values from keys, significantly reducing the write amplification compared to a typical LSM tree.

2 RocksDB is an SSD optimized version of LevelDB, which was designed specifically for rotating disks. As such RocksDB's design isn't aimed at SSDs.

3 SSI: Serializable Snapshot Isolation. For more details, see the blog post Concurrent ACID Transactions in Badger

4 Badger provides direct access to value versions via its Iterator API. Users can also specify how many versions to keep per key via Options.

Benchmarks

We have run comprehensive benchmarks against RocksDB, Bolt and LMDB. The benchmarking code, and the detailed logs for the benchmarks can be found in the badger-bench repo. More explanation, including graphs can be found the blog posts (linked above).

Projects Using Badger

Below is a list of known projects that use Badger:

If you are using Badger in a project please send a pull request to add it to the list.

Platform Compatibility

Badger uses OS-specific implementations for directory locking and fsync operations. On POSIX-compliant systems (Linux, macOS, BSD), these work as expected.

For non-POSIX platforms, be aware of potential limitations:

Platform File Notes
AIX dir_aix.go Directory fsync not supported; durability on crash may be affected
Windows dir_windows.go Uses different locking mechanism
Plan9 dir_plan9.go No file locking support
WASM/JS dir_other.go No file locking support

If you encounter issues on these platforms, review the corresponding dir_*.go source file for implementation details.

Contributing

If you're interested in contributing to Badger see CONTRIBUTING.

Contact

badger (dgraph-io/badger)

Searching Database…

15.8K ·GitHub
这是什么?BadgerDB 是一个用纯 Go 编写的可嵌入、持久化且速度很快的键值数据库。它是 Dgraph 图数据库背后的存储引擎,也是 RocksDB 这类非 Go 存储库的高性能替代品。
它是嵌入到我 Go 应用里的库,还是需要单独运行的数据库服务器?它是一个嵌入到你自己的 Go 进程里的库——不需要安装或运行任何服务器。Dgraph、Jaeger 和 go-ipfs 等项目都把它当作本地存储引擎来用。
怎么把它加到项目里?安装 Go 1.23 或更高版本,然后在你的项目里用 go modules 拉取这个库。你还可以编译出一个 badger 命令行工具,用于离线备份和恢复,它会被放到你的 $GOBIN 目录下。
除了 Go 之外还需要别的东西吗,比如 C 编译器或外部库?不需要——Badger 是纯 Go 实现,不依赖 cgo,只要有一套普通的 Go 工具链就够了。这也是它相对 RocksDB 这类非 Go 存储库的一个官方宣称的优势。
它在各个操作系统上的表现都一样吗?在 POSIX 系统(Linux、macOS、BSD)上,目录锁和 fsync 都能正常工作。Windows 使用的是另一种加锁机制,而 AIX、Plan9 和 WASM/JS 版本则有一些限制,比如缺少文件锁或崩溃时的持久性较弱。
它为什么快?它的设计源自 WiscKey 研究论文:键和值被分开,值存放在单独的值日志(value log)里,相比典型的 LSM 树能显著减少写放大,而且这个引擎是专门针对 SSD 优化的。
它和 RocksDB、BoltDB 相比怎么样?Badger 使用 LSM 树加值日志的结构,而 RocksDB 是纯 LSM 树,BoltDB 用的是 B+ 树。和 RocksDB 不同,它是纯 Go 实现、不依赖 cgo;和 BoltDB 不同,它提供高写入吞吐量,还支持 TTL。
它支持键过期或者一个值的多个版本吗?两者都支持。它支持 TTL,Iterator API 还能直接访问值的各个版本——你可以通过 options 控制每个键保留多少个版本。
我可以放心用它存重要数据吗?README 称 Badger 是稳定的,支撑着数百 TB 量级的数据集。它支持带可串行化快照隔离的并发 ACID 事务,每晚都会运行一个长达 8 小时、类似 Jepsen 的银行转账测试并开启 race 检测器进行验证,另外还针对文件系统层面的异常情况做了测试。
有哪些公司在生产环境中用它?Dgraph、Jaeger Tracing、go-ipfs、Pyroscope 以及 Grab 的 TalariaDB 等许多项目都在用——仅 UsenetExpress 一家就用它承载了超过 300TB 的数据。
What are similar projects?bolt, rocksdb, surrealdb, FerretDB, gun, nedb, leveldb, dicedb
Creator
dgraph-io
Size
57.1 MB
Stars
15.8K
Age
9y
Commits · 90 Days