Skip to content

goshops-com/kv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TieredKV

A high-performance, tiered key-value store written in Rust with automatic data lifecycle management across memory, disk, and object storage.

Features

  • Three-Tier Storage Architecture

    • L1 Memory: LRU cache for hot data with configurable size limits
    • L2 Disk: Persistent LSM-tree storage using sled
    • L3 Object Storage: S3-compatible cold storage for archived data
  • Automatic Data Tiering

    • Data automatically migrates from disk to object storage based on age or disk pressure
    • Hot data is promoted back to cache on access
  • Horizontal Scalability

    • Consistent hashing with virtual nodes for even key distribution
    • Easy to add/remove shards with minimal data movement
  • High Availability

    • Async replication with configurable consistency
    • Kubernetes-native deployment with StatefulSets
  • Simple HTTP API

    • RESTful interface for all operations
    • Health checks and metrics endpoints

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         HTTP API Layer                           β”‚
β”‚            GET/PUT/DELETE /kv/:key  β€’  /health  β€’  /stats        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                        Tiered Engine                             β”‚
β”‚                                                                  β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚   β”‚   Memory    β”‚    β”‚    Disk     β”‚    β”‚ Object Storage  β”‚     β”‚
β”‚   β”‚   (L1)      │───▢│    (L2)     │───▢│     (L3)        β”‚     β”‚
β”‚   β”‚  LRU Cache  β”‚    β”‚  LSM Tree   β”‚    β”‚   S3/MinIO      β”‚     β”‚
β”‚   β”‚  ~256MB     β”‚    β”‚   ~50GB     β”‚    β”‚   Unlimited     β”‚     β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β”‚                                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     Sharding (Consistent Hash)    β”‚    Replication (Async)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Quick Start

Run Locally

# Clone the repository
git clone https://github.com/goshops-com/kv.git
cd kv

# Build and run
cargo run --release

# The server starts on http://localhost:8080

Basic Usage

# Health check
curl http://localhost:8080/health

# Store a value
curl -X PUT http://localhost:8080/kv/mykey \
  -H "Content-Type: application/json" \
  -d '{"value": "Hello, World!"}'

# Retrieve a value
curl http://localhost:8080/kv/mykey

# Delete a value
curl -X DELETE http://localhost:8080/kv/mykey

# Check if key exists
curl -I http://localhost:8080/kv/mykey

# Get statistics
curl http://localhost:8080/stats

Configuration

Configure via environment variables:

Variable Default Description
HTTP_PORT 8080 HTTP server port
DATA_DIR ./data Disk storage directory
MEMORY_MAX_SIZE_MB 256 Max memory cache size
MEMORY_MAX_ENTRIES 100000 Max cached entries
DISK_MAX_SIZE_GB 50 Max disk storage size
DISK_MIGRATION_AGE_HOURS 24 Age before migrating to S3
S3_BUCKET tieredkv-data S3 bucket name
S3_REGION us-east-1 S3 region
S3_ENDPOINT - Custom S3 endpoint (for MinIO)
RUST_LOG info Log level

Kubernetes Deployment

Deploy with Kustomize

# Deploy 3 shards
kubectl apply -k k8s/

# Check status
kubectl get pods -l app=tieredkv

# Scale to 5 shards
kubectl scale statefulset tieredkv --replicas=5

Architecture in Kubernetes

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   Load Balancer  β”‚
                    β”‚   (Service)      β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚                    β”‚                    β”‚
        β–Ό                    β–Ό                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  tieredkv-0   β”‚  β”‚  tieredkv-1   β”‚  β”‚  tieredkv-2   β”‚
β”‚   (Shard 0)   β”‚  β”‚   (Shard 1)   β”‚  β”‚   (Shard 2)   β”‚
β”‚               β”‚  β”‚               β”‚  β”‚               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   PVC   β”‚  β”‚  β”‚  β”‚   PVC   β”‚  β”‚  β”‚  β”‚   PVC   β”‚  β”‚
β”‚  β”‚  100Gi  β”‚  β”‚  β”‚  β”‚  100Gi  β”‚  β”‚  β”‚  β”‚  100Gi  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                    β”‚                    β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                             β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   S3 / MinIO     β”‚
                    β”‚  (Cold Storage)  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

API Reference

Key-Value Operations

Method Endpoint Description
GET /kv/:key Get value by key
PUT /kv/:key Store a value
DELETE /kv/:key Delete a value
HEAD /kv/:key Check if key exists

Administrative

Method Endpoint Description
GET /health Health check
GET /stats Engine statistics
POST /admin/migrate Trigger migration
POST /admin/flush Flush to disk

Response Examples

GET /kv/:key

{
  "key": "mykey",
  "value": "Hello, World!",
  "tier": "memory"
}

GET /stats

{
  "memory_entries": 1523,
  "memory_size_bytes": 2456789,
  "memory_hit_rate": 0.847,
  "disk_entries": 45230,
  "disk_size_bytes": 1234567890,
  "disk_usage_percent": 12.5,
  "migrations_completed": 156
}

Data Flow

Write Path

Client Request
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Write to   │◄── Durability guarantee
β”‚    Disk     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Update     │◄── Fast subsequent reads
β”‚   Cache     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
   Response

Read Path

Client Request
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Check     │──── Hit ───▢ Return
β”‚   Cache     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚ Miss
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Check     │──── Hit ───▢ Promote to Cache ──▢ Return
β”‚    Disk     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚ Miss
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Check     │──── Hit ───▢ Promote to Cache ──▢ Return
β”‚     S3      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚ Miss
      β–Ό
   Not Found

Development

Prerequisites

  • Rust 1.75+
  • Docker (optional, for containerized deployment)

Build

# Debug build
cargo build

# Release build
cargo build --release

# Run tests
cargo test

# Run with logging
RUST_LOG=debug cargo run

Project Structure

tieredkv/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ memory/       # L1 LRU cache
β”‚   β”œβ”€β”€ disk/         # L2 sled-based storage
β”‚   β”œβ”€β”€ object/       # L3 S3-compatible storage
β”‚   β”œβ”€β”€ engine/       # Tiered engine coordinator
β”‚   β”œβ”€β”€ api/          # HTTP handlers and router
β”‚   β”œβ”€β”€ cluster/      # Sharding and replication
β”‚   β”œβ”€β”€ lib.rs
β”‚   └── main.rs
β”œβ”€β”€ k8s/              # Kubernetes manifests
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ Dockerfile
└── README.md

Running Tests

# Run all tests
cargo test

# Run specific module tests
cargo test memory
cargo test disk
cargo test engine

# Run with output
cargo test -- --nocapture

Performance Considerations

  • Memory Cache: Sub-microsecond reads for cached data
  • Disk Storage: Millisecond reads with LSM-tree optimization
  • Object Storage: Higher latency, used for cold/archived data
  • Consistent Hashing: O(log n) shard lookup with 150 virtual nodes per shard

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors