A high-performance key-value storage engine based on the Bitcask paper, written in Go.
Bitcask uses a log-structured storage model:
- Write: Append-only writes to the active data file, one sequential disk IO
- Read: In-memory index maps keys to file positions, one random disk IO
- Merge: Background compaction reclaims space from deleted/overwritten entries
Core
- High-performance read/write with O(1) complexity
- Multiple index types: B-tree / Skip List
- Atomic batch writes with WriteBatch
- CRC32 checksum for data integrity
- Configurable merge/compaction strategies
Protocol
- HTTP RESTful API
- Redis RESP protocol (compatible with redis-cli)
- Redis data structures: String / Hash / List / Set / ZSet
Optimization
- LRU cache for hot data
- Bloom filter to reduce disk IO
- TTL support with background cleanup
- Memory-mapped file IO (MMap)
opts := bitcask.DefaultOptions
opts.DirPath = "/tmp/bitcask"
db, _ := bitcask.Open(opts)
defer db.Close()
// Basic operations
db.Put([]byte("name"), []byte("bitcask"))
val, _ := db.Get([]byte("name"))
db.Delete([]byte("name"))
// Batch operations
wb := db.NewWriteBatch(bitcask.DefaultWriteBatchOptions)
wb.Put([]byte("k1"), []byte("v1"))
wb.Put([]byte("k2"), []byte("v2"))
wb.Commit()
// Iterator
iter := db.NewIterator(bitcask.DefaultIteratorOptions)
for iter.Rewind(); iter.Valid(); iter.Next() {
fmt.Printf("%s = %s\n", iter.Key(), iter.Value())
}
iter.Close()# Start server
go run ./cmd/bitcask-server -dir ./data -http :8080
# Usage
curl -X PUT localhost:8080/api/v1/kv/name -d '{"value":"bitcask"}'
curl localhost:8080/api/v1/kv/name
curl -X DELETE localhost:8080/api/v1/kv/name# Start server
go run ./cmd/redis-server -dir ./data -addr :6379
# Use redis-cli
redis-cli -p 6379
> SET name bitcask
> GET name
> HSET user:1 name alice age 30
> LPUSH queue task1 task2
> ZADD scores 100 alice 90 bobMIT