Skip to content

rahul2251999/Distributed-Cache-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Distributed Cache System

A high-performance distributed cache system built with Spring Boot and Redis, designed for high availability and scalability. This system implements consistent hashing for data distribution, circuit breakers for fault tolerance, and comprehensive monitoring.

πŸš€ Features

  • High Availability: 99.9% uptime with automatic failover
  • Performance: Sub-millisecond latency and 10,000+ RPS support
  • Scalability: Horizontal scaling with consistent hashing
  • Fault Tolerance:
    • Circuit breaker pattern
    • Retry mechanism with exponential backoff
    • Data replication across nodes
  • Monitoring:
    • Real-time metrics via Spring Boot Actuator
    • CloudWatch integration
    • Health check endpoints
  • Batch Operations: Support for bulk cache operations

πŸ—οΈ Architecture

Client Applications
       ↓  ↑
API Gateway Layer
       ↓  ↑
Cache Service Layer (Spring Boot)
       ↓  ↑
Redis Cluster (AWS ElastiCache)

πŸ“‹ Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher
  • Redis Cluster (AWS ElastiCache recommended)
  • AWS account for CloudWatch monitoring

βš™οΈ Configuration

Environment Variables

# Redis Configuration
export REDIS_CLUSTER_NODES=redis-cluster.xxxxx.clustercfg.use1.cache.amazonaws.com:6379
export REDIS_PASSWORD=your_redis_password

# AWS Configuration
export AWS_REGION=us-east-1

application.yml

spring:
  redis:
    cluster:
      nodes: ${REDIS_CLUSTER_NODES:localhost:6379}
    password: ${REDIS_PASSWORD:}
    timeout: 2000
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 2

management:
  endpoints:
    web:
      exposure:
        include: health,metrics,prometheus
  metrics:
    export:
      cloudwatch:
        namespace: CacheService
        enabled: true
        region: ${AWS_REGION:us-east-1}

πŸ› οΈ Building and Running

1. Clone the Repository

git clone https://github.com/your-username/distributed-cache-system.git
cd distributed-cache-system

2. Build the Project

mvn clean package

3. Run the Application

# Using environment variables
export REDIS_CLUSTER_NODES=your_redis_nodes
export REDIS_PASSWORD=your_password
java -jar target/distributed-cache-system-1.0-SNAPSHOT.jar

# Or using command line arguments
java -jar target/distributed-cache-system-1.0-SNAPSHOT.jar \
  --spring.redis.cluster.nodes=your_redis_nodes \
  --spring.redis.password=your_password

πŸ“‘ API Endpoints

Cache Operations

Store a Value

curl -X POST http://localhost:8080/api/cache/my-key \
  -H "Content-Type: text/plain" \
  -d "my-value" \
  -G --data-urlencode "ttlSeconds=3600"

Retrieve a Value

curl http://localhost:8080/api/cache/my-key

Delete a Value

curl -X DELETE http://localhost:8080/api/cache/my-key

Batch Store Values

curl -X POST http://localhost:8080/api/cache/batch \
  -H "Content-Type: application/json" \
  -d '{"key1": "value1", "key2": "value2"}' \
  -G --data-urlencode "ttlSeconds=3600"

Health Check

curl http://localhost:8080/api/health

Response:

{
  "status": "UP",
  "redis": "UP",
  "clusterNodes": ["node1:6379", "node2:6379"],
  "nodeCount": 2
}

πŸ“Š Monitoring

Available Metrics

  • Cache Operations:

    • cache.operations: Count of cache operations
    • cache.operation.latency: Operation latency in milliseconds
    • cache.hits: Number of cache hits
    • cache.misses: Number of cache misses
  • Circuit Breaker:

    • resilience4j.circuitbreaker.state: Current state of the circuit breaker
    • resilience4j.circuitbreaker.calls: Number of calls
  • Redis:

    • redis.connection.pool.active: Active connections
    • redis.connection.pool.idle: Idle connections

Viewing Metrics

  1. Spring Boot Actuator:

    curl http://localhost:8080/actuator/metrics
  2. CloudWatch Dashboard:

    • Navigate to AWS CloudWatch
    • Select "CacheService" namespace
    • View metrics and create dashboards

πŸ”§ Fault Tolerance

The system implements several fault tolerance mechanisms:

  1. Circuit Breaker:

    • Opens when failure rate exceeds 50%
    • Waits 1 second before allowing new requests
    • Requires 2 successful calls to close
  2. Retry Mechanism:

    • Maximum 3 retry attempts
    • 100ms wait between retries
    • Exponential backoff
  3. Data Replication:

    • Data replicated to 2 backup nodes
    • Automatic failover support

πŸš€ Performance Optimization

  • Connection Pooling: Optimized Redis connection pool
  • Batch Operations: Redis pipelining for bulk operations
  • Consistent Hashing: Efficient data distribution
  • Memory Management: Proper TTL handling

πŸ§ͺ Testing

Unit Tests

mvn test

Load Testing

# Using Apache Benchmark
ab -n 10000 -c 100 http://localhost:8080/api/cache/test-key

πŸ”’ Security

  • Redis authentication required
  • SSL/TLS for Redis connections
  • Proper IAM roles for AWS resources
  • VPC and security group configuration

πŸ“ License

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

🀝 Contributing

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

πŸ“ž Support

For support, please open an issue in the GitHub repository or contact the maintainers.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors