This project is a Spring Boot application that implements a Redis subscriber using Redis Lettuce. The application listens to messages published to a specific Redis channel and processes them accordingly.
- Redis Pub/Sub Mechanism – Subscribes to Redis channels and listens for incoming messages.
- Spring Boot Integration – Uses spring-boot-starter-data-redis for Redis communication.
- Lettuce Client – Implements Redis connection using Lettuce for efficient handling.
- Asynchronous Processing – Listens and processes messages in real time.
The technology used in this project are:
Spring Boot Starter Web
– Web application support.Redis with Lettuce
– Redis integration with Lettuce Redis client for efficient handling.
The project is organized into the following package structure:
redis-subscriber-lettuce/
│── src/main/java/com/yoanesber/spring/redis_subscriber_lettuce/
│ ├── 📂config/ # Configuration classes for Redis
│ ├── 📂event/ # Redis event listeners
│ ├── 📂service/ # Business logic layer
│ │ ├── 📂impl/ # Implementation of services
Configuration values are stored in .env.development
and referenced in application.properties
.
Example .env.development
file content:
# Application properties
APP_PORT=8081
SPRING_PROFILES_ACTIVE=development
# Redis properties
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_USERNAME=default
REDIS_PASSWORD=your_password
REDIS_TIMEOUT=5
REDIS_CONNECT_TIMEOUT=3
REDIS_LETTUCE_SHUTDOWN_TIMEOUT=10
Example application.properties
file content:
# Application properties
spring.application.name=redis-publisher-lettuce
server.port=${APP_PORT}
spring.profiles.active=${SPRING_PROFILES_ACTIVE}
# Redis properties
spring.data.redis.host=${REDIS_HOST}
spring.data.redis.port=${REDIS_PORT}
spring.data.redis.username=${REDIS_USERNAME}
spring.data.redis.password=${REDIS_PASSWORD}
spring.data.redis.timeout=${REDIS_TIMEOUT}
spring.data.redis.connect-timeout=${REDIS_CONNECT_TIMEOUT}
spring.data.redis.lettuce.shutdown-timeout=${REDIS_LETTUCE_SHUTDOWN_TIMEOUT}
A step by step series of examples that tell you how to get a development env running.
- Clone the repository
git clone https://github.com/yoanesber/Spring-Boot-Redis-Subscriber-Lettuce.git
cd Spring-Boot-Redis-Subscriber-Lettuce
- Ensure Redis is installed and running:
redis-server
- (Optional) If you want to add a specific user with access to a specific channel, you can run the following command in Redis CLI:
ACL SETUSER your_user +CHANNEL~your_channel on >your_password
- Set up Redis user and password in
.env.development
file:
# Redis properties
REDIS_PASSWORD=your_password
- Build and run the application
mvn spring-boot:run
- Verify Redis Subscription Use Redis CLI to publish a message:
redis-cli
PUBLISH PAYMENT_SUCCESS "{\"event\":\"PAYMENT_SUCCESS\",\"message\":{\"id\":\"1\",\"orderId\":\"ORD123456789\",\"amount\":199.99,\"currency\":\"USD\",\"paymentMethod\":\"PAYPAL\",\"paymentStatus\":\"SUCCESS\",\"cardNumber\":null,\"cardExpiry\":null,\"cardCvv\":null,\"paypalEmail\":\"[email protected]\",\"bankAccount\":null,\"bankName\":null,\"transactionId\":\"TXN1742316764298\",\"retryCount\":0,\"createdAt\":\"2025-03-18T16:52:44.298336Z\",\"updatedAt\":\"2025-03-18T16:52:44.298336Z\"}}"
- For the Redis Publisher implementation, check out Spring Boot Redis Publisher with Lettuce.
- For the Redis Stream as Message Producer implementation, check out Order Payment Service with Redis Streams as Reliable Message Producer for PAYMENT_SUCCESS / PAYMENT_FAILED Events.