• Stars
    star
    152
  • Rank 237,583 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created over 1 year ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A disk based KV store (based on Bitcask implementation)

logo

barreldb

A disk based key-value store based on Bitcask.

Go Reference Go Report Card GitHub Actions


BarrelDB is a Golang implementation of Bitcask by Riak paper and aims to closely follow the spec.

Bitcask is based on a log-structured hash table to store key-value data on disk. It opens a "datafile" (term used for a Bitcask DB file) in an append-only mode and all the writes are sequentially written to this file. Additionally, it also updates an in-memory hash table which maps the key with the offset of the record in the file. This clever yet simple design decision makes it possible to retrieve records from the disk using a single disk seek.

Benefits of this approach

  • Low Latency: Write queries are handled with a single O(1) disk seek. Keys lookup happen in memory using a hash table lookup. This makes it possible to achieve low latency even with a lot of keys/values in the database. Bitcask also relies on the filesystem read-ahead cache for a faster reads.
  • High Throughput: Since the file is opened in "append only" mode, it can handle large volumes of write operations with ease.
  • Predictable performance: The DB has a consistent performance even with growing number of records. This can be seen in benchmarks as well.
  • Crash friendly: Bitcask commits each record to the disk and also generates a "hints" file which makes it easy to recover in case of a crash.
  • Elegant design: Bitcask achieves a lot just by keeping the architecture simple and relying on filesystem primitives for handling complex scenarios (for eg: backup/recovery, cache etc).
  • Ability to handle datasets larger than RAM.

Limitations

  • The main limitation is that all the keys must fit in RAM since they're held inside as an in-memory hash table. A potential workaround for this could be to shard the keys in multiple buckets. Incoming records can be hashed into different buckets based on the key. A shard based approach allows each bucket to have limited RAM usage.

Internals

You can refer to Writing a disk-based key-value store in Golang blog post to read about the internals of Bitcask which also explains how BarrelDB works.

Usage

Library

import (
	"github.com/mr-karan/barreldb"
)

barrel, _ := barrel.Init(barrel.WithDir("data/"))

// Set a key.
barrel.Put("hello", []byte("world"))

// Fetch the key.
v, _ := barrel.Get("hello")

// Delete a key.
barrel.Delete("hello")

// Set with expiry.
barrel.PutEx("hello", []byte("world"), time.Second * 3)

For a complete example, visit examples.

Redis Client

barreldb implements the API over a simple Redis-compatible server (barreldb):

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> set goodbye world 10s
OK
127.0.0.1:6379> get goodbye
"world"
127.0.0.1:6379> get goodbye
ERR: invalid key: key is already expired

API

Method Description
Init(...cfg) *Barrel Returns a new instance of Barrel. Additional options can be passed like WithDir, WithReadOnly etc.
Put(string, []byte) error Store a key and value in the datastore.
PutEx(string, []byte, time.Duration) error Store a key and value with expiry in the datastore.
Get(string) []byte,error Retrieve a value by key from the datastore.
Delete(string) error Delete a key from the datastore.
Keys() []string List all keys in the datastore.
Len() int Return the total count of keys in the datastore.
Fold(func(string) error) error Fold over all K/V pairs in a Bitcask datastore. Calls a function on each key inside the datastore.
Sync() error Force any writes to sync to disk.
Shutdown() error Close a data store and flush all pending writes. Removes any lock on the data directory as well.

Benchmarks

Using make bench:

go test -bench=. -benchmem ./...
HELLO
goos: linux
goarch: amd64
pkg: github.com/mr-karan/barreldb
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkPut/DisableSync-8                385432              3712 ns/op        1103.48 MB/s          88 B/op          4 allocs/op
BenchmarkPut/AlwaysSync-8                    222           5510563 ns/op           0.74 MB/s         115 B/op          4 allocs/op
BenchmarkGet-8                            840627              1304 ns/op        3142.20 MB/s        4976 B/op          5 allocs/op
PASS
ok      github.com/mr-karan/barreldb 10.751s

Using redis-benchmark:

$ redis-benchmark -p 6379 -t set -n 10000 -r 100000000
Summary:
  throughput summary: 140845.06 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.196     0.016     0.175     0.255     1.031     2.455

$ redis-benchmark -p 6379 -t set -n 200000 -r 100000000
Summary:
  throughput summary: 143678.17 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.184     0.016     0.183     0.223     0.455     2.183

$ redis-benchmark -p 6379 -t get -n 100000 -r 100000000
Summary:
  throughput summary: 170068.03 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.153     0.040     0.143     0.199     0.367     1.447

References

More Repositories

1

awesome-investing

💸💸 Curated list of investment & finance related resources
1,800
star
2

doggo

🐶 Command-line DNS Client for Humans. Written in Golang
Go
1,544
star
3

swiggy-analytics

Analyse your swiggy orders 🍔
Python
265
star
4

homelab

Infra-as-code for my personal home server setup
HTML
241
star
5

kubekutr

Cookie cutter templating tool for scaffolding K8s manifests
Go
158
star
6

calert

🔔 Send alert notifications to Google Chat via Prometheus Alertmanager
Go
137
star
7

monkeybeat

Generate a random stock portfolio and find out if it beats the markets!
Go
59
star
8

nomad-vector-logger

A daemon which continuously watches jobs running in a Nomad cluster and templates out a Vector configuration file which can be used to collect application logs enriched with Nomad metadata.
Go
52
star
9

webkin

💻 CLI tool to send webpages to kindle. 📔
Python
50
star
10

nomad-monitoring

Collection of jobspecs and Grafana dashboards for end to end monitoring of Nomad clusters
HCL
47
star
11

nomad-events-sink

An events collection agent which processes Nomad Events and dumps to external sink providers like HTTP
Go
47
star
12

flexit

Responsive grid based on CSS Flexbox
HTML
45
star
13

nomctx

Faster way to switch between clusters and namespaces in nomad
Go
43
star
14

notes

Collection of my byte sized notes on programming and other random topics.
Makefile
35
star
15

nomad-external-dns

Set external DNS records for Nomad services
Go
33
star
16

cloak

Securely share sensitive text with others
Go
31
star
17

store-exporter

Utility to extract metrics from arbitary data stores in Prometheus format
Go
30
star
18

clickhouse-keeper-example

Setup ClickHouse cluster with replication using `clickhouse-keeper`
Makefile
29
star
19

fate

Browse FontAawesome icons from your shell
Python
29
star
20

website

Personal site, made using Zola
HTML
29
star
21

coredns-nomad

Go
27
star
22

terraform-provider-kite

Terraform provider for managing long term portfolio with Zerodha Kite
Go
24
star
23

Insta-notFollow

A Flask web app to find out people whom you follow on Instagram but they don't follow you back 😈
HTML
22
star
24

balance

Minimal Golang library for implemeting weighted round robin load balancing.
Go
21
star
25

haraka-plugin-outbound-logger

Haraka SMTP plugin for logging outbound traffic. Useful for storing audit information of delivered/bounced emails.
JavaScript
14
star
26

kiteHistory

🤑 Kite History API wrapper
Python
14
star
27

1brc-go

1️⃣🐝🏎️ The One Billion Row Challenge -- A fun exploration of how quickly 1B rows from a text file can be aggregated with Golang
Go
14
star
28

vscode-kite

Visual Studio Code extension for KiteConnect API
JavaScript
13
star
29

kite-yoda

Ignorance is bliss.
JavaScript
12
star
30

nomadev

Docker based development workflow with Nomad and Consul
Dockerfile
12
star
31

clx

Generate CLI commands using AI for common ops
Go
12
star
32

simplehealth

Tiny lib for exposing health-check endpoints as Prometheus/JSON format
Go
11
star
33

k8s-deployment-book

⭐ Kubernetes - Production Deployments for Developers (Book) ⭐
HTML
11
star
34

localhashi

Vagrant based setup for local Nomad/Consul clusters. Helpful for experimenting with Nomad job specs in an environment similar to production.
Jinja
10
star
35

ebs-snapshot-exporter

Export AWS EBS Snapshots data as Prometheus metrics
Go
8
star
36

kong-service-exporter

Utility to create a export a service registry Markdown file from Kong's config
Python
8
star
37

calwarrior

Calendar web view for Takwarrior
Vue
7
star
38

ansible-server-logs-monitoring

Ansible playbook to install ELK stack for log analysis and Prometheus/AlertManager/Node Exporter/Grafana for Server monitoring. Or as they say, One 💍 to rule them all!
7
star
39

NoiseInspector

📢 Arduino + Python + ESP8266 Wifi = Noise Level Monitor
Arduino
6
star
40

eks-gitops

Set of utilities to do deployments in an EKS cluster
Dockerfile
6
star
41

alertmatter

Alertmanager - Mattermost Webhook Receiver
Go
5
star
42

caddy-plugins-docker

Docker image for Caddy with custom plugins baked into the image.
Dockerfile
5
star
43

haraka-docker

Docker image for Haraka SMTP server
Dockerfile
5
star
44

listmonk-infra

Source code for deploying listmonk on Kubernetes
Makefile
4
star
45

mfp-calorie-extract

Copy macronutrients information from MFP and paste them in Google Sheets for calorie tracking
JavaScript
4
star
46

cgroup-stats

Tiny Go library designed for retrieving CPU and Memory quota information from Linux control groups (cgroups).
Go
4
star
47

pinkFloyd-Lyrics

Analyzing Pink Floyd lyrics
HTML
4
star
48

SNUMessApp

Android App for SNU Weekly Mess Menu
Jupyter Notebook
3
star
49

notion-weightbot

A Telegram bot which tracks bodyweight and stores the records in a Notion Database and CSV
Go
3
star
50

k8s-pruner

Cleanup unused configmaps in a Kubernetes namespace
Shell
2
star
51

fritter

Know if it's a good time to call your posh NRI fraands
Go
2
star
52

talks

HTML
2
star
53

NetNeutralityBadge

Add SaveTheInternet Badge to your Facebook DP to show support for Net Neutrality
HTML
2
star
54

vscode-nomfmt

Visual Studio Code extension for on-the-fly formatting of Nomad job specs in HCL using `nomad fmt`.
TypeScript
1
star
55

Algorithm-Implementations--Python

Some Implementations of Algorithms in Python
Python
1
star
56

kong-ansible

Ansible Playbook for setting up Kong node and using Cassandra as datastore
1
star
57

aws-dc-exporter

Prometheus metrics exporter for AWS Direct Connect
Go
1
star
58

koanf-test

Go
1
star
59

rubberduck

Toolbox of useful utilities for debugging inside Containers
Go
1
star
60

Python-Scripts

Writing Python Scripts to do boring monotonous work the fun way :D
Python
1
star
61

telegramBot-YtDownload

Telegram Bot which downloads Youtube videos
Python
1
star
62

newsletter

HTML
1
star
63

weather-Twitter-Bot

Made a Weather Twitter Bot using Python
Python
1
star
64

HackerRank-Problems

My codes for some problems on HackerRank : https://www.hackerrank.com/domains/miscellaneous/python-tutorials
Python
1
star
65

khoj

CSE SNU Project Final Year
Python
1
star