A blazing-fast, document-oriented NoSQL database engine built from scratch in Rust.
MongoDB-like flexibility Β· Next-gen performance Β· 100% open source
- Document-Based Data Model β Rich BSON-like value types (strings, numbers, arrays, embedded documents, dates, binary, regex, and more)
- Schema Validation β Optional JSON-Schema-like validation with type checks, range/length constraints, regex patterns, enum values, and nested schemas
- Write-Ahead Logging (WAL) β Segmented WAL with CRC32 checksums per entry, LSN tracking, and automatic segment rolling for crash-proof durability
- Buffer Pool β LRU-based page cache with pin/unpin eviction and performance counters
- Page-Based Storage β Fixed-size pages with integrity verification and overflow support
- MongoDB-Style Queries β Full support for
$eq,$ne,$gt,$gte,$lt,$lte,$in,$nin,$exists,$regex,$type,$elemMatch - Logical Operators β
$and,$or,$not,$norfor complex filter compositions - Query Planner β Automatic strategy selection: ID lookup β Index scan β Collection scan
- Projections β Return only the fields you need
- Sort, Skip & Limit β Full pagination support
- Update Operators β
$set,$unset,$inc,$push,$pull,$rename - Explain Plans β Debug and optimize your queries
- 12 Pipeline Stages β
$match,$project,$group,$sort,$limit,$skip,$unwind,$count,$addFields,$replaceRoot,$lookup,$out - 9 Accumulators β
$sum,$avg,$min,$max,$count,$push,$addToSet,$first,$last - JSON Pipeline Parser β Build pipelines from JSON, just like MongoDB
- B-Tree Indexes β Ordered indexes for range queries with composite key support
- Hash Indexes β O(1) exact-match lookups
- Unique Constraints β Enforce uniqueness across indexed fields
- Sparse Indexes β Skip documents missing the indexed field
- Index Manager β Create, drop, and auto-maintain indexes on insert/update/delete
- Role-Based Access Control (RBAC) β 5 built-in roles (
read,readWrite,dbAdmin,userAdmin,root) + custom roles with 11 action types - Argon2 Password Hashing β Industry-standard password security
- AES-256-GCM Encryption at Rest β Transparent data encryption with random nonce generation
- User Management β Create, authenticate, grant/revoke roles, enable/disable accounts
- ACID Transactions β Begin/commit/abort with automatic conflict detection
- Multiple Isolation Levels β Read Uncommitted, Read Committed, Repeatable Read, Snapshot, Serializable
- MVCC β Multi-Version Concurrency Control with snapshot reads and garbage collection
- Timeout Enforcement β Automatic transaction abort on timeout
- Async TCP Server β Built on Tokio with per-connection task spawning
- JSON Wire Protocol β 25+ command types covering all database operations
- Connection Pool β Configurable max connections with tracking and lifecycle management
- Interactive CLI β MongoDB-like shell with commands:
use,insert,find,count,delete, etc.
- Oplog-Based Replication β Capped operations log with timestamp-based sync queries
- Replica Sets β Primary/Secondary/Arbiter roles with heartbeat monitoring
- Consistent Hashing β Shard router with virtual nodes for even data distribution
- Key Range Sharding β Automatic routing based on shard key values
- 17 Atomic Metrics β Queries, inserts, updates, deletes, connections, bytes I/O, WAL writes, buffer pool hits/misses, index lookups, collection scans, transactions
- Server Status β Real-time statistics via the wire protocol
GraniteDB/
βββ Cargo.toml # Dependencies & build config
βββ src/
β βββ lib.rs # Library root (17 module re-exports)
β βββ main.rs # Server entry point (CLI + boot)
β β
β βββ config/ # βοΈ Configuration
β β βββ mod.rs
β β βββ settings.rs # Server, storage, auth, replication settings
β β
β βββ error/ # β Error Types
β β βββ mod.rs # 30+ typed error variants
β β
β βββ document/ # π Document Model
β β βββ mod.rs
β β βββ bson.rs # BSON-like value types with comparisons
β β βββ document.rs # Document struct with metadata & TTL
β β βββ validation.rs # Schema validation engine
β β
β βββ storage/ # πΎ Storage Engine
β β βββ mod.rs
β β βββ engine.rs # Unified storage (WAL + memory + disk)
β β βββ wal.rs # Write-Ahead Log (segmented, checksummed)
β β βββ page.rs # Fixed-size page management
β β βββ buffer_pool.rs # LRU page cache
β β βββ disk.rs # Low-level disk I/O
β β
β βββ collection/ # π¦ Collections
β β βββ mod.rs
β β βββ collection.rs # CRUD + operator queries + capped collections
β β
β βββ database/ # ποΈ Database Management
β β βββ mod.rs
β β βββ database.rs # Multi-collection container + stats
β β
β βββ query/ # π Query Engine
β β βββ mod.rs
β β βββ parser.rs # JSON β FilterExpr + UpdateOperation
β β βββ filter.rs # Filter expression tree
β β βββ planner.rs # Strategy selection + explain
β β βββ executor.rs # Execution with sort/skip/limit/projection
β β
β βββ index/ # ποΈ Indexing
β β βββ mod.rs
β β βββ btree.rs # B-Tree index (range + composite)
β β βββ hash_index.rs # Hash index (O(1) lookups)
β β βββ manager.rs # Index lifecycle management
β β
β βββ aggregation/ # π Aggregation
β β βββ mod.rs
β β βββ stages.rs # Stage & accumulator definitions
β β βββ pipeline.rs # Pipeline executor + JSON parser
β β
β βββ transaction/ # π Transactions
β β βββ mod.rs
β β βββ manager.rs # Begin/commit/abort + conflict detection
β β βββ mvcc.rs # Multi-Version Concurrency Control
β β
β βββ auth/ # π Authentication & Security
β β βββ mod.rs
β β βββ user.rs # User management (Argon2 hashing)
β β βββ rbac.rs # Role-Based Access Control
β β βββ encryption.rs # AES-256-GCM encryption at rest
β β
β βββ network/ # π Networking
β β βββ mod.rs
β β βββ server.rs # Async TCP server (Tokio)
β β βββ protocol.rs # JSON wire protocol (25+ commands)
β β βββ handler.rs # Request routing & execution
β β βββ connection.rs # Connection tracking & pool
β β
β βββ replication/ # π‘ Replication
β β βββ mod.rs
β β βββ oplog.rs # Capped operations log
β β βββ replica.rs # Replica set management
β β
β βββ sharding/ # π Sharding
β β βββ mod.rs
β β βββ shard.rs # Shard definition + key ranges
β β βββ router.rs # Consistent hashing router
β β
β βββ cursor/ # π Cursors
β β βββ mod.rs
β β βββ cursor.rs # Batch iteration over results
β β
β βββ metrics/ # π Monitoring
β β βββ mod.rs
β β βββ collector.rs # 17 atomic performance counters
β β
β βββ utils/ # π§ Utilities
β β βββ mod.rs
β β βββ helpers.rs # ID gen, hashing, formatting
β β
β βββ cli/ # π» CLI Client
β βββ main.rs # Interactive shell client
- Rust β₯ 1.85 (2024 edition)
# Clone
git clone https://github.com/kritarth1107/GraniteDB.git
cd GraniteDB
# Build
cargo build --release
# Start the server
cargo run --release -- --port 6380 --data-dir ./data
# In another terminal, connect with the CLI
cargo run --release --bin granite-cligranite:default> use mydb
Switched to database: mydb
granite:mydb> createcol users
granite:mydb> insert users {"name": "John", "age": 30, "email": "john@example.com"}
granite:mydb> find users {"age": {"$gt": 25}}
granite:mydb> count users
granite:mydb> delete users {"name": "John"}
Connect via TCP and send JSON commands:
{
"request_id": "abc-123",
"command": {
"type": "insert_one",
"database": "mydb",
"collection": "users",
"document": {"name": "Alice", "age": 28}
}
}Create a granitedb.json file:
{
"server": {
"host": "0.0.0.0",
"port": 6380,
"max_connections": 10000
},
"storage": {
"data_dir": "./data/granite",
"page_size": 16384,
"buffer_pool_pages": 4096,
"wal_fsync": true
},
"auth": {
"enabled": false
},
"logging": {
"level": "info"
}
} ββββββββββββββββ
β CLI Client β
ββββββββ¬ββββββββ
β TCP/JSON
ββββββββΌββββββββ
β TCP Server β β Tokio async, connection pool
ββββββββ¬ββββββββ
β
ββββββββΌββββββββ
β Handler β β Request routing, auth checks
ββββββββ¬ββββββββ
β
ββββββββββββββββΌβββββββββββββββ
β β β
ββββββββΌβββββββ ββββββΌβββββ βββββββββΌβββββββ
β Database β β Query β β Aggregation β
β Manager β β Engine β β Pipeline β
ββββββββ¬βββββββ ββββββ¬βββββ βββββββββ¬βββββββ
β β β
ββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββ
β Collection Manager β
β (CRUD + Schema Validation) β
βββββββββββββββββββ¬ββββββββββββββββββββββββββ
β
βββββββββββββββΌββββββββββββββ
β β β
ββββββββΌβββββββ βββββΌβββββ ββββββββΌβββββββ
β Storage β β Index β β Transaction β
β Engine β βManager β β Manager β
ββββββββ¬βββββββ ββββββββββ βββββββββββββββ
β
ββββββΌβββββ
β β β
βββΌββββΌβββββΌβββββββββ
βWALββBuf ββ Disk β
β ββPoolββ Manager β
ββββββββββββββββββββββ
We welcome contributions! GraniteDB is 100% open source under the Apache 2.0 license.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the Apache License 2.0 β see the LICENSE file for details.
Built with β€οΈ and Rust π¦
GraniteDB β The database that's solid as granite.