A Java-based distributed leader election system using Redisson for Redis operations. This project demonstrates how to implement a robust leader election mechanism in distributed systems using Java 21 virtual threads and Redisson's distributed locks.
- Distributed Leader Election: Multiple application instances compete for leadership using Redis distributed locks
- Task Processing: Leader nodes process tasks from a Redis blocking queue
- Automatic Failover: When the leader fails, other nodes automatically become the new leader after lock expiration
- Graceful Shutdown: Proper resource cleanup and lock release on application shutdown
- Java 21 Virtual Threads: Efficient concurrency handling using virtual threads
- Comprehensive Logging: Detailed logging for monitoring and debugging
+----------------+ +------------------+
| Application | | Redis |
| Instance 1 |<--->| (Redis Server) |
+----------------+ +------------------+
|
| (Distributed Lock)
v
+----------------+ +------------------+
| Application | | Task Queue |
| Instance 2 | | (RBlockingQueue)|
+----------------+ +------------------+
- JDK 21: Modern Java with virtual threads support
- Redisson 3.23+: Redis client library with distributed data structures
- Maven: Build automation tool
- SLF4J + Logback: Logging framework
- JUnit 5 + Mockito: Testing framework
The application uses configuration properties that can be set via:
application.propertiesfile insrc/main/resources/- System properties
- Environment variables
redis.address=localhost:6379
redis.password=
redis.timeout=5000
redis.connectionPoolSize=10
redis.connectionMinimumIdleSize=2leader.lock.key=distributed:leader:lock
leader.lock.lease-time=30000
leader.lock.watchdog-timeout=10000task.queue.name=distributed:task:queue
task.queue.timeout=5000- JDK 21 or higher
- Redis server running (default: localhost:6379)
- Maven 3.6+
# Clone the repository
git clone <repository-url>
cd taskreq-java
# Build the project
mvn clean compile
# Run the application
mvn exec:java -Dexec.mainClass="space.lambdadriver.Main"# Set custom Redis address
mvn exec:java -Dexec.mainClass="space.lambdadriver.Main" \
-Dredis.address="your-redis-host:6379"
# Set custom lock key
mvn exec:java -Dexec.mainClass="space.lambdadriver.Main" \
-Dleader.lock.key="custom:leader:lock"Run unit tests:
mvn testRun specific test classes:
mvn test -Dtest=SampleTaskProcessorTest
mvn test -Dtest=LeaderSelectorTestsrc/
├── main/java/
│ └── space/lambdadriver/
│ ├── config/ # Configuration classes
│ │ └── RedissonConfig.java
│ ├── election/ # Leader election logic
│ │ └── LeaderSelector.java
│ ├── service/ # Task processing interface and implementations
│ │ ├── TaskProcessor.java
│ │ └── SampleTaskProcessor.java
│ └── Main.java # Application entry point
├── main/resources/
│ └── application.properties
└── test/java/
└── space/lambdadriver/
├── election/
│ └── LeaderSelectorTest.java
└── service/
└── SampleTaskProcessorTest.java
Singleton class that provides a configured RedissonClient instance for connecting to Redis.
Core component that handles:
- Distributed lock acquisition using Redisson
- Task processing while holding the lock
- Graceful failure handling and retry logic
- Automatic lock renewal via Redisson's watch dog
Defines the contract for processing tasks from the queue.
Example implementation that logs task details and simulates processing.
- Startup: Each application instance starts and creates a LeaderSelector
- Lock Acquisition: Instances attempt to acquire a distributed lock
- Leadership: The instance that successfully acquires the lock becomes the leader
- Task Processing: Leader processes tasks from the Redis blocking queue
- Lock Renewal: Redisson's watch dog automatically renews the lock during processing
- Failure Detection: If the leader loses the lock (timeout/failure), it stops processing
- Failover: Other instances detect the lost lock and compete for leadership again
The application provides detailed logging at different levels:
- INFO: Startup, leadership changes, task processing
- DEBUG: Detailed operation information
- WARN: Potential issues
- ERROR: Critical failures
Log output includes:
- Leadership status changes
- Task processing details
- Lock acquisition/release events
- Error conditions and stack traces
- Uses Java 21 virtual threads for efficient concurrency
- Configurable connection pooling for Redis
- Non-blocking queue operations
- Automatic lock renewal prevents premature expiration
- Graceful degradation on network issues
- Horizontal scaling by adding more application instances
- Redis cluster support through Redisson configuration
- Configurable lock timeouts and retry intervals
- Independent task processing per leader instance
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.