A high-performance, fault-tolerant in-memory cache system written in Elixir, featuring Redis-compatible protocol support, master-replica replication, and comprehensive caching 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
- 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
- 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
- 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
- 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
- Elixir 1.10 or higher
- Erlang OTP 23 or higher
# 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
# Connect using redis-cli (Redis-compatible)
redis-cli -p 6379
# Basic cache operations
SET greeting "Hello World"
GET greeting
INCR counter
DEL greeting
# Terminal 1: Start Master Cache
mix run -- --port 6379
# Terminal 2: Start Replica Cache
mix run -- --port 6380 --replicaof "localhost 6379"
./run_tests.sh all
# 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
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
# 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
MIX_ENV
: Set totest
for testing,prod
for productionREDIS_PORT
: Default cache server port (6379)REDIS_MAX_CLIENTS
: Maximum concurrent clients (default: 1000)
# Start master and replica caches, then test cache synchronization
./quick_fault_tolerance_demo.sh
# Demonstrate cache server stability during client failures
./blpop_disconnect_demo.sh
- Disconnect replica cache network
- Perform cache operations on master
- Reconnect replica cache
- Verify cache consistency
- Multiple concurrent clients
- High cache operation throughput
- Memory usage monitoring
- Automatic cache resource management
# Run all cache tests
mix test
# Run with coverage
mix test --cover
# Run specific test file
mix test test/basic_operations_test.exs
# Format code
mix format
# Run linter
mix credo
# Type checking (if configured)
mix dialyzer
- Add command handler in
lib/server.ex
- Update cache replication logic if it's a write command
- Add comprehensive tests
- Update documentation
- 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
- 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
# Enable debug logging
Logger.configure(level: :debug)
- Connection count
- Cache command throughput
- Memory usage
- Cache replication status
- Error rates
# Ping the cache server
redis-cli -p 6379 ping
# Get cache server info
redis-cli -p 6379 info
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-cache-feature
) - Commit your changes (
git commit -m 'Add amazing cache feature'
) - Push to the branch (
git push origin feature/amazing-cache-feature
) - Open a Pull Request
- 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