Skip to content

ProgMastermind/ElixirCache

Repository files navigation

ElixirCache πŸš€

A high-performance, fault-tolerant in-memory cache system written in Elixir, featuring Redis-compatible protocol support, master-replica replication, and comprehensive caching operations.

Tests Elixir License

✨ Features

Redis-Compatible Cache Operations

  • Basic Operations: SET, GET, DEL, EXISTS, TYPE, KEYS, INCR, DECR
  • List Operations: RPUSH, LPUSH, LPOP, RPOP, LLEN, LRANGE, LINDEX
  • Sorted Sets: ZADD, ZRANK, ZSCORE, ZREM, ZCARD
  • Streams: XADD, XRANGE, XREAD, XLEN
  • Pub/Sub: PUBLISH, SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE
  • Transactions: MULTI, EXEC, DISCARD, WATCH
  • Blocking Operations: BLPOP with client disconnection handling

Cache-Specific Features

  • In-Memory Storage: High-performance ETS-based key-value storage
  • Cache Eviction: Automatic cleanup and memory management
  • TTL Support: Time-to-live functionality for cache entries
  • Atomic Operations: Thread-safe operations across all data types

Advanced Features

  • Master-Replica Replication: Redis-compatible replication with PSYNC for cache synchronization
  • Fault Tolerance: Graceful handling of client disconnections and cache stability
  • Write Protection: Replica caches automatically reject write operations
  • Command Buffering: Efficient command queuing and transmission to replica caches
  • RESP Protocol: Full Redis Serialization Protocol for compatibility
  • Concurrent Clients: Handle multiple client connections simultaneously
  • Error Handling: Comprehensive error handling with proper Redis-compatible responses

πŸ—οΈ Architecture

Distributed Cache Architecture

  • Master-Replica Synchronization: Redis-compatible replication protocol for cache consistency
  • Command Propagation: All cache write operations automatically synchronized to replicas
  • Data Consistency: Guaranteed consistency across master and replica cache instances
  • Automatic Recovery: Replica caches automatically reconnect after network interruptions

Fault Tolerance

  • Client Disconnection Handling: Cache server remains stable during client failures
  • Blocking Operation Recovery: BLPOP operations gracefully handle client disconnections
  • Network Resilience: Automatic recovery from network partitions
  • Process Isolation: Isolated processes prevent cascading cache failures

πŸš€ Quick Start

Prerequisites

  • Elixir 1.10 or higher
  • Erlang OTP 23 or higher

Installation

# Clone the repository
git clone https://github.com/your-username/elixir-cache.git
cd elixir-cache

# Install dependencies
mix deps.get

# Start the cache server
mix run -- --port 6379

Basic Cache Usage

# Connect using redis-cli (Redis-compatible)
redis-cli -p 6379

# Basic cache operations
SET greeting "Hello World"
GET greeting
INCR counter
DEL greeting

Distributed Cache Setup

# Terminal 1: Start Master Cache
mix run -- --port 6379

# Terminal 2: Start Replica Cache
mix run -- --port 6380 --replicaof "localhost 6379"

πŸ§ͺ Testing

Run All Tests

./run_tests.sh all

Run Specific Test Suites

# Basic cache operations tests
./run_tests.sh basic

# List operations tests
./run_tests.sh lists

# Cache replication tests
./run_tests.sh replication

# Cache fault tolerance tests
./run_tests.sh fault-tolerance

πŸ“ Project Structure

elixir-cache/
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ server.ex                 # Main cache server logic and command handling
β”‚   └── server/
β”‚       β”œβ”€β”€ acknowledge.ex        # ACK handling for cache replication
β”‚       β”œβ”€β”€ bytes.ex              # Byte manipulation utilities
β”‚       β”œβ”€β”€ clientbuffer.ex       # Client connection management
β”‚       β”œβ”€β”€ clientstate.ex        # Client state tracking
β”‚       β”œβ”€β”€ commandbuffer.ex      # Command buffering for cache replication
β”‚       β”œβ”€β”€ config.ex             # Cache server configuration
β”‚       β”œβ”€β”€ error.ex              # Error handling
β”‚       β”œβ”€β”€ listblock.ex          # Blocking list operations
β”‚       β”œβ”€β”€ listcoord.ex          # List coordination
β”‚       β”œβ”€β”€ liststore.ex          # List data storage
β”‚       β”œβ”€β”€ pendingwrites.ex      # Pending write operations
β”‚       β”œβ”€β”€ protocol.ex           # RESP protocol implementation
β”‚       β”œβ”€β”€ pubsub.ex             # Pub/Sub functionality
β”‚       β”œβ”€β”€ replicationstate.ex   # Cache replication state management
β”‚       β”œβ”€β”€ sortedsetstore.ex     # Sorted set storage
β”‚       β”œβ”€β”€ store.ex              # Key-value cache storage
β”‚       β”œβ”€β”€ streamstore.ex        # Stream data storage
β”‚       └── transactionstate.ex   # Transaction management
β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ basic_operations_test.exs    # Basic cache commands
β”‚   β”œβ”€β”€ integration_test.exs         # Integration tests
β”‚   β”œβ”€β”€ list_operations_test.exs     # List operations
β”‚   β”œβ”€β”€ pubsub_test.exs             # Pub/Sub tests
β”‚   β”œβ”€β”€ sorted_set_test.exs         # Sorted set tests
β”‚   β”œβ”€β”€ stream_test.exs             # Stream tests
β”‚   β”œβ”€β”€ transaction_test.exs        # Transaction tests
β”‚   β”œβ”€β”€ replication_test.exs        # Cache replication functionality
β”‚   β”œβ”€β”€ fault_tolerance_tests.exs   # Cache fault tolerance tests
β”‚   └── test_helper.exs             # Test utilities
β”œβ”€β”€ run_tests.sh                   # Test runner script
β”œβ”€β”€ spawn_redis_server.sh         # Cache server startup script
β”œβ”€β”€ quick_fault_tolerance_demo.sh  # Cache replication demo
β”œβ”€β”€ blpop_disconnect_demo.sh       # Client disconnection demo
└── README.md                      # This file

πŸ”§ Configuration Options

Cache Server Options

# Basic cache server
mix run -- --port 6379

# Replica cache mode
mix run -- --port 6380 --replicaof "localhost 6379"

# Custom cache configuration
mix run -- --port 6379 --max-clients 1000 --timeout 300

Environment Variables

  • MIX_ENV: Set to test for testing, prod for production
  • REDIS_PORT: Default cache server port (6379)
  • REDIS_MAX_CLIENTS: Maximum concurrent clients (default: 1000)

🎯 Fault Tolerance Demonstrations

1. Master-Replica Cache Synchronization

# Start master and replica caches, then test cache synchronization
./quick_fault_tolerance_demo.sh

2. Client Disconnection During BLPOP

# Demonstrate cache server stability during client failures
./blpop_disconnect_demo.sh

3. Network Partition Simulation

  • Disconnect replica cache network
  • Perform cache operations on master
  • Reconnect replica cache
  • Verify cache consistency

4. Cache Load Testing

  • Multiple concurrent clients
  • High cache operation throughput
  • Memory usage monitoring
  • Automatic cache resource management

πŸ› οΈ Development

Running Tests

# Run all cache tests
mix test

# Run with coverage
mix test --cover

# Run specific test file
mix test test/basic_operations_test.exs

Code Quality

# Format code
mix format

# Run linter
mix credo

# Type checking (if configured)
mix dialyzer

Adding New Cache Commands

  1. Add command handler in lib/server.ex
  2. Update cache replication logic if it's a write command
  3. Add comprehensive tests
  4. Update documentation

πŸ“Š Performance

Cache Benchmarks

  • Concurrent Clients: Handles 20000+ concurrent connections
  • Throughput: High-performance cache command processing
  • Memory Usage: Efficient ETS-based in-memory storage
  • Replication Lag: Minimal delay in master-replica cache sync

Cache Optimizations

  • ETS Tables: High-performance in-memory cache storage
  • Process Isolation: Fault-tolerant cache process architecture
  • Command Buffering: Efficient cache replication queuing
  • Lazy Evaluation: Optimized cache data structure operations

πŸ” Monitoring & Debugging

Cache Server Logs

# Enable debug logging
Logger.configure(level: :debug)

Key Cache Metrics

  • Connection count
  • Cache command throughput
  • Memory usage
  • Cache replication status
  • Error rates

Cache Health Checks

# Ping the cache server
redis-cli -p 6379 ping

# Get cache server info
redis-cli -p 6379 info

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-cache-feature)
  3. Commit your changes (git commit -m 'Add amazing cache feature')
  4. Push to the branch (git push origin feature/amazing-cache-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Elixir style guide
  • Add comprehensive tests for new cache features
  • Update documentation
  • Ensure all cache tests pass
  • Maintain code coverage standards

Built with ❀️ using Elixir - A Redis-Compatible Cache System

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published