A fast, reliable, and thread-safe key-value database written in Rust with HTTP API and interactive CLI.
kline/
├── storage/ # Core database engine with WAL
├── http/ # HTTP API with JSON responses
├── cli/ # Interactive REPL interface
├── config/ # Configuration management
└── error/ # Custom error types
git clone <repository>
cd kline
cargo build --release# Generate default config
cargo run config-init
# Start server
cargo run
# Or start with custom settings
cargo run -- --port 8080 --data-dir /var/lib/klineKline uses a hierarchical configuration system with the following priority order:
- CLI Arguments (highest priority)
- Environment Variables
- Config File (
kline.conf) - Built-in Defaults (lowest priority)
cargo run config-init --output kline.conf[server]
port = 3000
bind_address = "127.0.0.1"
max_connections = 1000
[storage]
data_dir = "./data"
compaction_interval_secs = 60
max_log_size_mb = 100
[limits]
max_key_size = 1024 # 1KB
max_value_size = 10485760 # 10MB
max_keys = 1000000 # 1M keys
[ttl]
cleanup_interval_secs = 30
default_ttl_secs = 3600 # 1 hour
max_ttl_secs = 31536000 # 1 year maxexport KLINE_PORT=8080
export KLINE_DATA_DIR=/var/lib/kline
export KLINE_BIND_ADDRESS=0.0.0.0# Override config file settings
cargo run -- --port 9000 --data-dir /tmp/kline
# Use custom config file
cargo run -- --config /etc/kline/production.conf| Method | Endpoint | Description | Example |
|---|---|---|---|
PUT |
/key/{key} |
Store a key-value pair | PUT /key/user:123 |
GET |
/key/{key} |
Retrieve a value | GET /key/user:123 |
DELETE |
/key/{key} |
Delete a key | DELETE /key/user:123 |
GET |
/keys |
List all keys | GET /keys |
# Store data
curl -X PUT http://localhost:3000/key/user:123 \
-H "Content-Type: application/stream" \
-d "john_doe"
# Retrieve data
curl http://localhost:3000/key/user:123
# Delete data
curl -X DELETE http://localhost:3000/key/user:123
# List all keys
curl http://localhost:3000/keys{
"key": "user:123",
"value": "john_doe",
"found": true
}{
"key": "user:123",
"value": null,
"found": false
}{
"status": "OK"
}{
"keys": ["user:123", "session:abc"],
"count": 2
}Start the interactive shell:
cargo runkline> put user:123 john_doe
kline> get user:123
john_doe
kline> delete user:123
kline> keys
user:456
session:abc
kline> help
kline> exituse kline::{Kline, KlineError, Result};
let db = Kline::open("my.db")?;
match db.put(key, value) {
Ok(_) => println!("Stored successfully"),
Err(KlineError::KeyTooLarge { size, max }) => {
println!("Key too large: {} bytes (max: {})", size, max);
}
Err(KlineError::DatabaseFull { current, max }) => {
println!("Database full: {}/{} keys", current, max);
}
Err(err) => println!("Error: {}", err),
}Kline enforces configurable limits to prevent resource exhaustion:
- Max key size: Default 1KB
- Max value size: Default 10MB
- Max keys: Default 1M keys
- Database size: Controlled by max keys × avg value size
- Write-Ahead Log: All operations logged before execution
- Crash Recovery: Database state rebuilt from log on startup
- Auto-Compaction: Periodic cleanup of obsolete log entries
- Atomic Operations: Each operation is atomic and durable
Kline is fully thread-safe:
- Concurrent Reads: Multiple readers can access data simultaneously
- Exclusive Writes: Write operations are serialized for consistency
- HTTP + CLI: Both interfaces can be used concurrently
- Background Tasks: Auto-compaction runs safely in background
- Fork the repository
- Create a 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 MIT License - see the LICENSE file for details.