Skip to content

A lightweight, high-performance in-memory key-value store inspired by Redis, built entirely in Rust.

License

Notifications You must be signed in to change notification settings

Legacy-Engineers/medusa

Repository files navigation

Medusa

███╗   ███╗███████╗██████╗ ██╗   ██╗███████╗ █████╗
████╗ ████║██╔════╝██╔══██╗██║   ██║██╔════╝██╔══██╗
██╔████╔██║█████╗  ██║  ██║██║   ██║███████╗███████║
██║╚██╔╝██║██╔══╝  ██║  ██║██║   ██║╚════██║██╔══██║
██║ ╚═╝ ██║███████╗██████╔╝╚██████╔╝███████║██║  ██║
╚═╝     ╚═╝╚══════╝╚═════╝  ╚═════╝ ⚠══════╝╚═╝  ╚═╝

A lightning-fast in-memory key-value store built with Rust

Rust License Status Version


About

Medusa is a lightweight, high-performance in-memory key-value store inspired by Redis, built entirely in Rust. This project serves as a learning experiment to explore systems programming concepts and database internals.

What You'll Learn

  • Low-level data storage and memory management
  • TCP networking and concurrent client handling
  • Command parsing and response serialization
  • Systems programming with Rust's safety guarantees
  • Database internals and performance optimization
  • TTL (Time To Live) and expiration handling
  • Pattern matching and advanced queries
  • Benchmarking and performance testing

⚡ Why "Medusa"?

Just as the mythical Medusa possessed incredible power, this project channels that energy into a compact, efficient data store. While it won't turn you to stone, it might transform your understanding of how databases work under the hood!

Project Goals

Learning Performance Hands-on
Explore database internals Fast in-memory operations Build from scratch
Master Rust systems programming Efficient TCP handling Understand every line
Study Redis-like architectures Minimal memory footprint Learn by doing

What This Is NOT

Important: This is not a production-ready Redis clone.

Missing Features:

  • ❌ Clustering and replication
  • ❌ Persistence and snapshots
  • ❌ Advanced data types (sets, sorted sets, etc.)
  • ❌ Pub/sub messaging
  • ❌ Lua scripting

Focus: Understanding core concepts, not feature completeness.

New Features

TTL Support

  • Set expiration times for keys
  • Automatic cleanup of expired keys
  • TTL querying and management

Pattern Matching

  • Wildcard key searches (KEYS user:*)
  • Advanced pattern matching capabilities
  • Efficient key filtering

Hash Data Type

  • Store field-value pairs within a key
  • Operations: HSET, HGET, HGETALL, HDEL, HEXISTS, HLEN
  • Perfect for storing object-like data structures

List Data Type

  • Ordered collections of strings
  • Operations: LPUSH, RPUSH, LPOP, RPOP, LLEN, LRANGE
  • Support for negative indices in range operations
  • Ideal for queues, stacks, and ordered data

Configuration System

  • Environment variable support
  • Configurable timeouts and limits
  • Flexible server settings

Benchmarking Tools

  • Built-in performance testing
  • Multi-threaded benchmarks
  • Stress testing capabilities

Enhanced Error Handling

  • Better connection management
  • Timeout support
  • Graceful error recovery

Tech Stack

Technology Purpose
Rust Systems Programming - Memory safety and performance
TCP Network Layer - Client-server communication
Memory Data Storage - In-memory key-value operations
TTL Time Management - Automatic key expiration

Quick Start

Prerequisites

  • Rust (latest stable)
  • A terminal and some curiosity!

Running Medusa

# Clone and navigate to the project
git clone https://github.com/Legacy-Engineers/medusa.git
cd medusa

# Build the project
cargo build --release

# Run the server
cargo run --bin medusa # to run the server
cargo run --bin medusa-client # to run the client

Using the Client

# Run the interactive client
cargo run --bin medusa-client

# Or use the compiled binary
./target/release/medusa-client

Benchmarking

# Run performance benchmarks
cargo run --bin medusa-benchmark

# Custom benchmark parameters
cargo run --bin medusa-benchmark 127.0.0.1 2312 10000 8

Available Commands

🔧 Basic Operations

SET key value [TTL seconds]  # Store key-value pair with optional TTL
GET key                      # Retrieve value by key
DELETE key                   # Remove key-value pair
EXISTS key                   # Check if key exists

TTL Management

TTL key                      # Get time-to-live for key
EXPIRE key seconds           # Set expiration time for key

Hash Operations

HSET key field value         # Set hash field to value
HGET key field               # Get hash field value
HGETALL key                  # Get all hash fields and values
HDEL key field               # Delete hash field
HEXISTS key field            # Check if hash field exists
HLEN key                     # Get hash length

List Operations

LPUSH key value              # Push value to left of list
RPUSH key value              # Push value to right of list
LPOP key                     # Pop value from left of list
RPOP key                     # Pop value from right of list
LLEN key                     # Get list length
LRANGE key start stop        # Get list range (supports negative indices)

Query Operations

LIST                         # List all keys
KEYS pattern                 # Find keys matching pattern (use * for wildcard)
COUNT                        # Get number of entries

Administrative

CLEAR/FLUSHALL              # Remove all entries
INFO                         # Get server statistics
PING                         # Server health check
QUIT/EXIT                    # Disconnect

Examples

SET user:1 "John Doe" 3600    # Set with 1 hour TTL
EXPIRE user:1 7200            # Set 2 hour expiration
KEYS user:*                   # Find all user keys
TTL user:1                    # Check remaining time
HSET user:1 name "John"       # Set hash field
HGET user:1 name             # Get hash field
LPUSH tasks "task1"          # Push to list
LRANGE tasks 0 -1            # Get all list items

Configuration

Environment Variables

export MEDUSA_HOST="127.0.0.1"
export MEDUSA_PORT="2312"
export MEDUSA_MAX_CONNECTIONS="100"
export MEDUSA_TIMEOUT="30"
export MEDUSA_ENABLE_TIMEOUTS="false"
export MEDUSA_LOG_LEVEL="info"
export MEDUSA_METRICS="false"
export MEDUSA_CLIENT_TIMEOUTS="false"

Server Configuration

The server supports various configuration options:

  • Host/Port: Customize server binding
  • Max Connections: Limit concurrent clients
  • Timeouts: Configure connection timeouts (disabled by default)
  • Logging: Adjust verbosity levels

Performance

Benchmark Results

Typical performance on modern hardware:

Benchmark Results: Single-threaded SET
  Operations: 10000
  Duration: 1.2s
  Ops/sec: 8333.33
  Avg Latency: 0.12ms

Benchmark Results: Multi-threaded (4 threads)
  Total Operations: 40000
  Total Ops/sec: 25000.00

Performance Features

  • Lock-free operations where possible
  • Efficient memory management
  • Optimized TCP handling
  • Automatic expired key cleanup

Testing

# Run all tests
cargo test

# Run specific test modules
cargo test store
cargo test server

# Run benchmarks
cargo run --bin medusa-benchmark

Learning Resources

Resource Description
Redis Source Code The Original - Study the master
Build Your Own Redis Step-by-step - C implementation guide
The Rust Book Language Reference - Essential reading
Rust Async Book Async Programming - For advanced features

Contributing

This is a learning project, but contributions that improve code quality, documentation, or educational value are welcome!

Development Guidelines

  • Experiment freely - This is for learning!
  • Document your discoveries - Share what you learn
  • Report issues - Help improve the codebase
  • Suggest improvements - Better ways to teach concepts

Code Style

  • Follow Rust conventions
  • Add tests for new features
  • Update documentation
  • Use meaningful commit messages

License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with ⚡ by curious minds for curious minds

"The best way to learn is to build"

About

A lightweight, high-performance in-memory key-value store inspired by Redis, built entirely in Rust.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •