Netabase Store
A type-safe, multi-backend key-value storage library for Rust with support for native (Sled, Redb) and WASM (IndexedDB) environments, inspired by native_db.
⚠️ Early Development: This crate is still in early development and will change frequently as it stabilizes. It is not advised to use this in a production environment until it stabilizes.
Installation
Add to your Cargo.toml:
[]
= "my_project"
= "0.1.0"
= "2024"
# Features must be enabled in your crate for macro-generated code
[]
= ["native"]
= ["netabase_store/native"]
[]
= { = "0.0.7", = ["native"] }
# Required dependencies
= { = "2.0", = ["serde"] }
= { = "1.0", = ["derive"] }
= { = "0.27.2", = ["derive"] }
= { = "2.0.1", = ["from", "try_into", "into"] }
= "0.56" # Optional, if you would like to use this as a persistent backend for [`libp2p-kad` RecordStore implementation](https://docs.rs/libp2p/latest/libp2p/kad/index.html)
= "1.0"
Feature Flags
The default feature set includes native which provides both Sled and Redb backends. You can customize features for your specific needs.
Backend Features:
native- (Recommended) Enables both Sled and Redb backends for desktop/serversled- Sled backend only (high-performance embedded database)redb- Redb backend only (memory-efficient, ACID compliant)redb-zerocopy- Zero-copy Redb variant (maximum performance, requiresredb)wasm- IndexedDB backend for browser/WASM applications
Integration Features:
libp2p- Enable libp2p integration for distributed systemsrecord-store- Enable RecordStore trait (requireslibp2p)
Common Configurations:
# For desktop/server applications (recommended):
= { = "0.0.6", = ["native"] }
# For WASM/browser applications:
[]
= { = "0.0.7", = false, = ["wasm"] }
# For specific backend only:
= { = "0.0.7", = ["sled"] }
# For zero-copy redb optimization:
= { = "0.0.7", = ["redb-zerocopy"] }
# For libp2p integration:
= { = "0.0.7", = ["native", "libp2p"] }
Quick Start
1. Define Your Schema
use netabase_definition_module;
use NetabaseModelTrait;
use *;
2. Use with NetabaseStore (Recommended)
The unified NetabaseStore provides a consistent API across all backends:
use NetabaseStore;
3. Direct Backend Usage (Advanced)
You can also use backends directly for backend-specific features:
use SledStore;
use RedbStore;
// Direct Sled usage
let sled_store = temp?;
let user_tree = sled_store.;
// Direct Redb usage
let redb_store = new?;
let user_tree = redb_store.;
// Both have identical APIs via NetabaseTreeSync trait
4. Use with IndexedDB (WASM)
use IndexedDBStore;
use NetabaseTreeAsync;
async
Backend Comparison & Selection Guide
All backends share the same core API through traits, but have different characteristics:
🔹 Sled (Recommended for Most Cases)
- Type: Native, persistent, sync
- Use Cases: General-purpose applications, desktop apps, servers
- Strengths:
- Excellent write performance
- Crash-safe with recovery
- Mature and battle-tested
- Simple file-based storage
- API Example:
let store = sled?; let tree = store.; tree.put?; // Synchronous operations
🔹 Redb (Optimized for Reads)
- Type: Native, persistent, sync
- Use Cases: Read-heavy workloads, embedded databases
- Strengths:
- Zero-copy reads (fastest read performance)
- Memory-efficient
- ACID compliant
- Smaller file size than Sled
- API Example:
let store = redb?; let tree = store.; tree.put?; // Synchronous operations
🔹 RedbZeroCopy (Advanced, Explicit Transactions)
- Type: Native, persistent, explicit transactions
- Use Cases: Maximum performance, explicit transaction control
- Strengths:
- Zero-copy reads
- Explicit transaction API for fine-grained control
- Best performance for bulk operations
- API Difference: Requires explicit transaction management
use RedbStoreZeroCopy; let store = new?; // Must use explicit transactions let mut txn = store.begin_write?; let mut tree = txn.?; tree.put?; drop; txn.commit?; // Must explicitly commit
🔹 IndexedDB (Browser/WASM)
- Type: WASM, persistent, async
- Use Cases: Web applications, browser-based storage
- Strengths:
- Native browser storage
- Persistent across sessions
- Standard web API
- API Difference: All operations are async
let store = new.await?; let tree = store.; tree.put.await?; // Note: async operations let result = tree.get.await?;
Quick Selection Guide
| Backend | Persistence | Async | Best For | Avoid If |
|---|---|---|---|---|
| Sled | ✅ Disk | ❌ Sync | General purpose, high writes | WASM target |
| Redb | ✅ Disk | ❌ Sync | Read-heavy, low memory | Need fastest writes |
| RedbZeroCopy | ✅ Disk | ❌ Sync | Bulk ops, transaction control | Want simple API |
| IndexedDB | ✅ Browser | ✅ Async | Web/WASM apps | Native targets |
Performance Notes:
- All backends support batch operations (10-100x faster for bulk inserts)
- All backends support secondary key queries
- All backends (except RedbZeroCopy) support the transaction API:
store.read()andstore.write() - Sled and Redb have similar performance, with Redb slightly faster for reads
- For testing, use temp() methods for fast, isolated tests
API Compatibility:
- ✅ All sync backends (Sled, Redb) have identical APIs
- ✅ RedbZeroCopy requires explicit transaction management
- ⚠️ IndexedDB uses async (
.await) for all operations - ✅ All backends support the same data models and secondary keys
Advanced Usage
Configuration API
The new unified configuration system provides consistent backend initialization across all database types:
FileConfig - For File-Based Backends
use FileConfig;
use BackendStore;
use SledStore;
// Method 1: Builder pattern (recommended)
let config = builder
.path
.cache_size_mb
.truncate
.build;
let store = new?;
// Method 2: Simple constructor
let config = new;
let store = open?;
// Method 3: Temporary database
let store = temp?;
Switching Backends with Same Config
The power of the configuration API is that you can switch backends without changing your code:
use FileConfig;
use BackendStore;
let config = builder
.path
.cache_size_mb
.build;
// Try different backends - same config!
let store = new?;
let store = new?;
let store = new?;
// All have the same API from this point on!
let user_tree = store.;
Configuration Options Reference
FileConfig (for Sled, Redb, RedbZeroCopy):
path: PathBuf- Database file/directory pathcache_size_mb: usize- Cache size in megabytes (default: 256)create_if_missing: bool- Create if doesn't exist (default: true)truncate: bool- Delete existing data (default: false)read_only: bool- Open read-only (default: false)use_fsync: bool- Fsync for durability (default: true)
IndexedDBConfig (for WASM):
database_name: String- IndexedDB database nameversion: u32- Schema version (default: 1)
Bulk Operations with Transactions
For high-performance bulk operations, use the transaction API (10-100x faster than individual operations):
use NetabaseStore;
let store = sled?;
// Create a write transaction for bulk operations
// NOTE: write() returns TxnGuard directly, not a Result
let mut txn = store.write;
let mut user_tree = txn.;
// Bulk insert - 8-9x faster than individual puts!
let users: =
.map
.collect;
// All inserts in a single transaction
user_tree.put_many?;
// Bulk read within transaction
let keys: = .map.collect;
let users: = user_tree.get_many?;
// Commit all changes atomically
txn.commit?;
Transaction Methods:
put_many(Vec<M>)- Insert multiple models in one transactionget_many(Vec<M::PrimaryKey>)- Read multiple models in one transaction
Backend Support:
- Sled: Full support via transactions and batch API
- Redb: Full support via transactions and batch API
- Both backends provide identical API and performance benefits
For more examples, see examples/batch_operations_all_backends.rs
Or use the batch API for more control:
use Batchable;
// Create a batch
let mut batch = user_tree.create_batch?;
// Add many operations
for i in 0..1000
// Commit atomically - all or nothing
batch.commit?;
The batch API provides fine-grained control and is supported on both sync backends (Sled, Redb).
Transactions (New!)
For maximum performance and atomicity, use the transaction API to reuse a single transaction across multiple operations:
use NetabaseStore;
let store = sled?;
// Read-only transaction - multiple concurrent reads allowed
// NOTE: read() and write() return guards directly, not Results
let txn = store.read;
let user_tree = txn.;
let user = user_tree.get?;
// Transaction auto-closes on drop
// Read-write transaction - exclusive access, atomic commit
let mut txn = store.write;
let mut user_tree = txn.;
// All operations share the same transaction
for i in 0..1000
// Bulk helpers also work within transactions
user_tree.put_many?;
// Commit all changes atomically
txn.commit?;
// Or drop without committing to rollback
Transaction Benefits:
- 10-100x Faster: Single transaction for many operations (eliminates per-operation overhead)
- Type-Safe: Compile-time enforcement of read-only vs read-write access
- Zero-Cost: Phantom types compile away completely
- ACID: Full atomicity for write transactions (Redb)
Compile-Time Safety:
let txn = store.read; // ReadOnly transaction
let tree = txn.;
tree.put?; // ❌ Compile error: put() not available on ReadOnly!
Backend Support Notes:
- Sled, Redb: Full support for
store.read()andstore.write()transaction API - RedbZeroCopy: Uses explicit
store.begin_write()andtxn.commit()pattern (different API) - IndexedDB: Async operations, transactions handled internally by browser
- See
examples/transactions.rsfor detailed examples
Secondary Keys
Secondary keys enable efficient lookups on non-primary fields:
// Query by single secondary key
let tech_articles = article_tree
.get_by_secondary_key?;
// Bulk query multiple secondary keys (2-3x faster!)
let keys = vec!;
let results: = article_tree.get_many_by_secondary_keys?;
// results[0] = tech articles, results[1] = science articles
Multiple Models in One Store
use NetabaseStore;
let store = sled?;
// Different trees for different models
let user_tree = store.;
let post_tree = store.;
// Each tree is independent but shares the same underlying database
user_tree.put?;
post_tree.put?;
Temporary Store for Testing
use NetabaseStore;
// Perfect for unit tests - no I/O, no cleanup needed
let store = temp?;
let user_tree = store.;
user_tree.put?;
Implementing a backend
It is technically possible to implement a backend of your own, but much of this implementation needed to be generated with macros, with a few implementation specific quirks. This makes it a bit hard for me to track exactly how to create reproducable instructions, but you can do one of 2 things:
- Read the
netabase_macrosto see what gents generated and how things string up - Expanding the macro implementations to see how the backend implementations are generated.
But if you are looking to wrap your own backend, or for a simpler model,
kivisornative_modelare probably better suited.
Performance
Netabase Store is designed for high performance while maintaining type safety. The library provides multiple APIs optimized for different use cases, with comprehensive benchmarking and profiling support.
API Options for Performance
The library offers three APIs with different performance characteristics:
- Standard Wrapper API: Simple, ergonomic API with auto-transaction per operation
- Bulk Methods:
put_many(),get_many(),get_many_by_secondary_keys()- single transaction for multiple items - ZeroCopy API: Explicit transaction management for maximum control
Benchmark Results
Comprehensive benchmarks comparing all implementations across multiple dataset sizes (10, 100, 500, 1000, 5000 items):
Insert Performance (1000 items)
| Implementation | Time | vs Raw | Notes |
|---|---|---|---|
| Raw Redb (baseline) | 1.42 ms | 0% | Single transaction, manual index management |
| Wrapper Redb (bulk) | 3.10 ms | +118% | put_many() - single transaction |
| Wrapper Redb (loop) | 27.3 ms | +1,822% | Individual put() calls - creates N transactions |
| ZeroCopy (bulk) | 3.51 ms | +147% | put_many() with explicit transaction |
| ZeroCopy (loop) | 4.34 ms | +206% | Loop with single explicit transaction |
Key Insights:
- Bulk methods provide 8-9x speedup over loop-based insertion (27.3ms → 3.10ms)
- Bulk wrapper API approaches raw performance (118% overhead vs 1,822% for loops)
- Transaction overhead dominates when creating N transactions vs 1 transaction
Read Performance (1000 items)
| Implementation | Time | vs Raw | Notes |
|---|---|---|---|
| Raw Redb (baseline) | 164 µs | 0% | Single transaction |
| Wrapper Redb (bulk) | 382 µs | +133% | get_many() - single transaction |
| Wrapper Redb (loop) | 895 µs | +446% | Individual get() calls - creates N transactions |
| ZeroCopy (single txn) | 692 µs | +322% | Explicit read transaction |
Key Insights:
- Bulk
get_many()provides 2.3x speedup over individual gets (895µs → 382µs) - Transaction reuse is critical for read performance
- Even bulk methods have overhead due to transaction and deserialization costs
Secondary Key Queries (10 queries)
| Implementation | Time | vs Raw | Notes |
|---|---|---|---|
| Raw Redb (baseline) | 291 µs | 0% | 10 transactions, manual index traversal |
| Wrapper Redb (bulk) | 470 µs | +61% | get_many_by_secondary_keys() - single transaction |
| Wrapper Redb (loop) | 1.02 ms | +248% | 10 separate get_by_secondary_key() calls |
| ZeroCopy (single txn) | 5.41 µs | -98% | Single transaction, optimized index access |
Key Insights:
- ZeroCopy API is 54x faster than raw redb for secondary queries (291µs → 5.4µs)
- Bulk secondary query method provides 2.2x speedup over loops
- Single transaction + efficient index access = dramatic performance gains
Performance Optimization Guide
1. Use Bulk Methods for Standard API (8-9x faster)
// ❌ Slow: Creates 1000 transactions
for user in users
// ✅ Fast: Single transaction
tree.put_many?; // 8-9x faster!
Available Bulk Methods:
put_many(Vec<M>)- Bulk insertget_many(Vec<M::Keys>)- Bulk readget_many_by_secondary_keys(Vec<SecondaryKey>)- Bulk secondary queries
2. Use Explicit Transactions for Maximum Control
// For write-heavy workloads
// NOTE: write() returns TxnGuard directly, not a Result
let mut txn = store.write;
let mut tree = txn.;
for user in users
txn.commit?; // Single atomic commit
3. Choose the Right API for Your Use Case
| Use Case | Recommended API | Reason |
|---|---|---|
| Simple CRUD, few operations | Standard wrapper | Simplest API, auto-commit |
| Bulk inserts/reads (100+ items) | Bulk methods | 8-9x faster than loops |
| Complex transactions | Explicit transactions | Full control, atomic commits |
| Read-heavy queries | ZeroCopy API | Up to 54x faster for secondary queries |
Running Benchmarks
# Cross-store comparison (all backends, multiple sizes)
# Generate visualizations
# View results
Backend Comparison
Redb
- Best for: Write-heavy workloads, ACID guarantees
- Wrapper overhead: 118-133% for bulk operations
- Strengths: Excellent write performance, full ACID compliance, efficient storage
- Use when: Data integrity is critical, write performance matters
Sled
- Best for: Read-heavy workloads
- Wrapper overhead: ~20% for read operations
- Strengths: Very low read overhead, battle-tested
- Use when: Read performance is critical, workload is read-heavy
Technical Notes
Type Safety vs Performance
The wrapper APIs prioritize type safety and ergonomics. For applications where the overhead is significant:
- Use bulk methods first - often solves the problem
- Use explicit transactions - full control with same safety
- Profile your workload - measure before optimizing
- Consider ZeroCopy API - for specialized high-performance scenarios
Serialization Overhead
The read-path overhead in Redb comes from type system limitations with Generic Associated Types (GATs). We prioritize safety over unsafe transmutes. For applications where this matters:
- Use bulk methods to amortize overhead
- Use explicit transactions for better performance
- Consider Sled backend for read-heavy workloads
See benchmark results and visualizations in docs/benchmarks/ for detailed performance analysis.
Testing
# Run all tests
# Run native tests only
# Run WASM tests (requires wasm-pack and Firefox)
Architecture
See ARCHITECTURE.md for a deep dive into the library's design.
High-Level Overview
┌───────────────────────────────────────────────────┐
│ Your Application Code │
│ (Type-safe models with macros) │
└───────────────────────────────────────────────────┘
│
↓
┌───────────────────────────────────────────────────┐
│ NetabaseStore<D, Backend> │
│ (Unified API layer - Recommended) │
└───────────────────────────────────────────────────┘
│
┌─────────────┼─────────────┐
↓ ↓ ↓
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ SledStore │ │ RedbStore │ │IndexedDBStore│
│ <D> │ │ <D> │ │ <D> │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
↓ ↓ ↓
┌─────────────────────────────────────────────────┐
│ Trait Layer │
│ (NetabaseTreeSync, NetabaseTreeAsync) │
│ (OpenTree, Batchable, StoreOps) │
└─────────────────────────────────────────────────┘
│ │ │
↓ ↓ ↓
┌─────┐ ┌──────┐ ┌─────────┐
│ Sled│ │ Redb │ │IndexedDB│
└─────┘ └──────┘ └─────────┘
Native Native WASM
Roadmap
For 1.0.0
- Transaction support across multiple operations (COMPLETED)
- Zero-copy reads for redb backend (via
redb-zerocopyfeature) - Phase 1 Complete - Allow modules to define more than one definition for flexible organization
- Migration utilities for schema changes
- Query builder for complex queries
- Range queries on ordered keys
- Compression support
- Encryption at rest
- Improved documentation and examples
Future Plans
- Distributed systems support with automatic sync
- CRDT-based conflict resolution
- WebRTC backend for peer-to-peer storage
- SQL-like query language
- GraphQL integration
Examples
See the test_netabase_store_usage crate for a complete working example.
Core Examples
examples/basic_store.rs- Basic CRUD operations with Sledexamples/unified_api.rs- Working with the NetabaseStore unified APIexamples/config_api_showcase.rs- Configuration system and backend switching
Single-Backend Examples
examples/batch_operations.rs- Batch operations (Sled-focused)examples/transactions.rs- Transaction API (Sled-focused)examples/redb_basic.rs- Redb-specific featuresexamples/redb_zerocopy.rs- RedbZeroCopy explicit transaction APIexamples/subscription_demo.rs- Change notification systemexamples/subscription_streams.rs- Advanced streaming examples
Test Examples
tests/backend_crud_tests.rs- Comprehensive CRUD tests for all backendstests/wasm_tests.rs- WASM/IndexedDB usage patternstests/comprehensive_store_tests.rs- Full test suite
Running Examples
# Run multi-backend examples (automatically includes available backends)
# Run with specific backends
# Run single-backend examples
Note: Examples work with both Sled and Redb backends. Switch backends by changing the initialization method.
Why Netabase Store?
I was mostly trying to abstract a persisten backend for libp2p::kad::store, and got carried away.
Contributing
Contributions are welcome! Feel free to leave a PR.
License
This project is licensed under the GPL-3.0-only License - see the LICENSE file for details.
Links
Acknowledgments
Built with: