GhastlyDB - a super lightweight vector database in Go
January 8, 2025 ยท View on GitHub
I've built this as an experiment - to truly understand how databases work. This is only possible if I built it from first principles. GhastlyDB is the result of this experiment, and I'm super excited about how it turned out.
Features ๐ช
Embedding Support
- Multiple embedding providers:
- OpenAI (using text-embedding-3-small model)
- NVIDIA (using nv-embedqa-mistral-7b-v2)
- ColBERT (local embedding support)
Storage Engine
- LSM Tree-based storage architecture
- Memory-mapped memtable for fast writes
- SSTable-based persistent storage
- Skip list implementation for efficient data structure
- Thread-safe operations with concurrent access support
Search Capabilities
- Multiple similarity metrics:
- Cosine similarity
- Dot product
- L2 distance
- Efficient vector comparison algorithms
- Sorted search results with similarity scores
Cross-Platform Support
- Linux (amd64, arm64)
- macOS (amd64, arm64)
- Windows (amd64)
Installation ๐พ
Prerequisites
- Go 1.21 or higher
- Make
- pkg-config
Local inference specific dependencies
- ONNX Runtime (for local embedding model inference)
- Make sure
libtokenizers.ais present inside/libs/static/libotkenizers. You can build it from source or find it in the releases page of HuggingFace's tokenizers port for Go. (shoutout @daulet)
Platform-Specific Dependencies
macOS
brew install pkg-config
brew install onnxruntime
Linux
sudo apt-get update
sudo apt-get install build-essential pkg-config
pip install onnxruntime
Windows
pip install onnxruntime
Building From Source
- Clone the repository:
git clone https://github.com/ahhcash/ghastly.git
cd ghastly
- Build for your platform:
make build
This will create a binary in the bin/ directory for your current OS and architecture.
- Build for all platforms:
make build-all
This creates binaries for:
- Linux (amd64, arm64)
- macOS (amd64, arm64)
- Windows (amd64)
Usage ๐งโ๐ป
Building from source / using the docker container is the best way to get started. You can generate gRPC stubs or just use the REST API to perform DB operations!
Configuration
Default configuration:
Config{
Path: "./ghastlydb_data",
MemtableSize: 64 * 1024 * 1024, // 64MB
Metric: "cosine",
EmbeddingModel: "openai",
}
API Usage (Coming soon ๐คซ)
import "github.com/ahhcash/ghastlydb/db"
// Initialize with default config
database, err := db.OpenDB(db.DefaultConfig())
// Store data
err = database.Put("key", "value")
// Retrieve data
value, err := database.Get("key")
// Semantic search
results, err := database.Search("query")
Architecture ๐ ๏ธ
Storage Layer
GhastlyDB uses a Log-Structured Merge Tree (LSM) architecture:
Writes are buffered in an in-memory memtable (implemented as a skip list) When memtable reaches its size limit, it's flushed to disk as an SSTable SSTables are immutable and contain sorted key-value pairs Background processes handle SSTable compaction
Search Engine
The search implementation supports multiple distance metrics:
Cosine similarity for normalized vectors Dot product for raw similarity L2 distance for Euclidean space
Embedding Layer
OpenAI: Cloud-based embeddings using text-embedding-3-small
NVIDIA: Cloud-based embeddings using nv-embedqa-mistral-7b-v2
ColBERT: Local inference using ONNX runtime, libtokenizers on colBERT-ir/v2
Development
Testing
make test # Run tests
make coverage # Generate coverage report
Code Quality
make lint # Run golangci-lint
make fmt # Format code
Directory Structure
Directory structure:
โโโ ahhcash-ghastly/
โโโ README.md
โโโ Dockerfile
โโโ Makefile
โโโ go.mod
โโโ go.sum
โโโ .golangci.yml
โโโ clients/
โ โโโ python/
โ โโโ __init__.py
โ โโโ client.py
โ โโโ setup.py
โ โโโ test_client.py
โโโ cmd/
โ โโโ main.go
โโโ db/
โ โโโ db.go
โ โโโ db_test.go
โโโ embed/
โ โโโ embedder.go
โ โโโ local/
โ โ โโโ colbert/
โ โ โโโ config.go
โ โ โโโ darwin.go
โ โ โโโ embed.go
โ โ โโโ linux.go
โ โ โโโ platform_specific.go
โ โ โโโ windows.go
โ โโโ nvidia/
โ โ โโโ embed.go
โ โ โโโ types.go
โ โโโ openai/
โ โโโ embed.go
โ โโโ types.go
โโโ grpc/
โ โโโ gen/
โ โ โโโ grpc/
โ โ โโโ proto/
โ โ โโโ ghastly.pb.go
โ โ โโโ ghastly_grpc.pb.go
โ โโโ proto/
โ โ โโโ ghastly.proto
โ โโโ server/
โ โโโ server.go
โโโ http/
โ โโโ server/
โ โโโ server.go
โโโ index/
โ โโโ connections.go
โ โโโ hnsw.go
โ โโโ search.go
โโโ libs/
โ โโโ static/
โ โโโ libtokenizers/
โ โโโ .gitkeep
โโโ mocks/
โ โโโ embedder.go
โโโ search/
โ โโโ cosine.go
โ โโโ dot.go
โ โโโ l2.go
โ โโโ metrics_test.go
โโโ storage/
โ โโโ memtable.go
โ โโโ memtable_test.go
โ โโโ skiplist.go
โ โโโ skiplist_test.go
โ โโโ sstable.go
โ โโโ store.go
โ โโโ store_test.go
โโโ .github/
โโโ workflows/
โโโ build_and_deploy.yml
Contributing ๐
I would absolutely love any feedback / contributions! Please open a PR, and I'll gladly take a look :)