This guide will walk you through setting up and running the Redis Sentinel POC from scratch, including all dependencies and configurations.
- Java 17+ (JDK 17 or higher)
- Maven 3.6+
- Docker & Docker Compose
- Git (to clone the repository)
# Check Java version
java -version
# Should show: openjdk version "17" or higher
# Check Maven
mvn -version
# Should show: Apache Maven 3.6+
# Check Docker
docker --version
docker-compose --versiongit clone <your-repo-url>
cd redisPocredisPoc/
βββ src/main/java/com/poc/redis/
β βββ config/
β β βββ ApplicationProperties.java # Redis configuration
β β βββ RedisConfig.java # Connection factories & templates
β βββ controller/
β β βββ UserController.java # REST API endpoints
β βββ service/
β β βββ UserService.java # Business logic with cache annotations
β β βββ RedisService.java # Direct Redis operations
β βββ model/
β β βββ User.java # Entity model
β βββ redisCacheManagement/
β βββ FallbackRedisCacheManager.java # Master-replica cache manager
β βββ FallbackRedisCache.java # Cache implementation
βββ src/main/resources/
β βββ application.yml # Application configuration
βββ docker-compose.yml # Redis Sentinel cluster setup
βββ sentinel1.conf # Sentinel 1 configuration
βββ sentinel2.conf # Sentinel 2 configuration
βββ sentinel3.conf # Sentinel 3 configuration
βββ pom.xml # Maven dependencies
# Start only Redis master and replica (for initial testing)
docker-compose up -d redis-master redis-replica
# Verify containers are running
docker-compose psExpected Output:
NAME STATUS PORTS
redis-master Up (healthy) 0.0.0.0:6379->6379/tcp
redis-replica Up (healthy) 0.0.0.0:6380->6379/tcp
# Test master connection
docker exec -it redis-master redis-cli ping
# Should return: PONG
# Test replica connection
docker exec -it redis-replica redis-cli ping
# Should return: PONG
# Verify replication
docker exec -it redis-master redis-cli SET test "hello"
docker exec -it redis-replica redis-cli GET test
# Should return: "hello"Edit src/main/resources/application.yml:
spring:
data:
redis:
# Standalone configuration (for initial testing)
host: localhost
port: 6379
# Sentinel configuration (disable for now)
sentinel:
enabled: false # Start with standalone mode
master: mymaster
nodes:
- localhost:26379
- localhost:26380
- localhost:26381# Clean and compile
mvn clean compile
# Run tests (optional)
mvn test
# Package the application (optional)
mvn package# Start Spring Boot application
mvn spring-boot:runExpected Output:
INFO - Master RedisTemplate configured successfully
INFO - Replica RedisTemplate configured successfully
INFO - Creating STANDALONE Master Connection Factory
INFO - Creating STANDALONE Replica Connection Factory
INFO - Started RedisPocApplication in X.XXX seconds
# Check application health
curl http://localhost:9090/actuator/healthExpected Response:
{
"status": "UP",
"components": {
"redis": {
"status": "UP",
"details": {
"master": "UP",
"replica": "UP",
"message": "Redis Sentinel cluster is up and running"
}
}
}
}curl -X POST http://localhost:9090/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","designation":"Developer"}'curl http://localhost:9090/userscurl http://localhost:9090/users/1# Check cache keys in Redis master
docker exec -it redis-master redis-cli KEYS "*"
# Expected output:
# 1) "masterNode-> users::1"
# 2) "masterNode-> users::all-users"
# Check cache keys in Redis replica
docker exec -it redis-replica redis-cli KEYS "*"
# Expected output:
# 1) "replicaNode-> users::1"
# 2) "replicaNode-> users::all-users"# Stop current containers
docker-compose down
# Start complete setup with Sentinels
docker-compose up -d
# Verify all services are running
docker-compose psExpected Output:
NAME STATUS PORTS
redis-master Up (healthy) 0.0.0.0:6379->6379/tcp
redis-replica Up (healthy) 0.0.0.0:6380->6379/tcp
redis-sentinel-1 Up 0.0.0.0:26379->26379/tcp
redis-sentinel-2 Up 0.0.0.0:26380->26379/tcp
redis-sentinel-3 Up 0.0.0.0:26381->26379/tcp
Update application.yml:
spring:
data:
redis:
sentinel:
enabled: true # Enable Sentinel mode# Stop the application (Ctrl+C)
# Restart with Sentinel mode
mvn spring-boot:runExpected Output:
INFO - Creating SENTINEL Master Connection Factory
INFO - Creating SENTINEL Replica Connection Factory
INFO - Added Sentinel node: localhost:26379
INFO - Added Sentinel node: localhost:26380
INFO - Added Sentinel node: localhost:26381
# Create some test data
curl -X POST http://localhost:9090/users \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"test@example.com"}'
# Verify data is cached
docker exec -it redis-master redis-cli KEYS "*users*"# In terminal 1 - Monitor Sentinel logs
docker logs -f redis-sentinel-1
# In terminal 2 - Monitor Redis operations
docker exec -it redis-master redis-cli MONITOR# Stop Redis master
docker stop redis-master
# Watch sentinel logs for failover messages
# Should see: "Replica promoted to master"# Application should continue working
curl http://localhost:9090/users/1
# Check new master (should be the former replica)
docker exec -it redis-sentinel-1 redis-cli -p 26379 SENTINEL masters# Restart original master (becomes replica)
docker start redis-master
# It will automatically rejoin as replica# Connect to any Sentinel
docker exec -it redis-sentinel-1 redis-cli -p 26379
# Check master info
SENTINEL masters
# Check replicas
SENTINEL replicas mymaster
# Check other sentinels
SENTINEL sentinels mymaster# On master
docker exec -it redis-master redis-cli INFO replication
# On replica
docker exec -it redis-replica redis-cli INFO replication# Monitor Redis operations
docker exec -it redis-master redis-cli MONITOR
# In another terminal, make API calls
curl http://localhost:9090/users/1# Check cache hit/miss ratios
docker exec -it redis-master redis-cli INFO stats
# Check memory usage
docker exec -it redis-master redis-cli INFO memoryMonitor application logs for:
Writing to master: ...(cache writes)Reading from replica: ...(cache reads)Failed to read from replica, falling back to master(fallback scenarios)
# Check Java version
java -version
# Clean and rebuild
mvn clean compile
# Check Redis connectivity
docker exec -it redis-master redis-cli ping# Check Sentinel logs
docker logs redis-sentinel-1
# Common fix: restart with clean slate
docker-compose down -v
docker-compose up -d# Verify Redis connection
curl http://localhost:9090/actuator/health
# Check Redis keys
docker exec -it redis-master redis-cli KEYS "*"
# Monitor Redis operations
docker exec -it redis-master redis-cli MONITOR# Check Sentinel quorum
docker exec -it redis-sentinel-1 redis-cli -p 26379 SENTINEL ckquorum mymaster
# Verify Sentinel configuration
docker exec -it redis-sentinel-1 cat /etc/redis/sentinel.confβ
Redis Containers Running: Master, replica, and 3 sentinels all UP
β
Application Started: Spring Boot app running on port 9090
β
API Responding: All CRUD operations working
β
Cache Working: Keys visible in both master and replica Redis
β
Health Check Passing: /actuator/health returns UP status
β
Automatic Failover: Master failure promotes replica automatically
β
Zero Downtime: Application continues working during failover