Modern applications often need
real-time communication between services. Redis provides a simple and fast messaging system called
Publish/Subscribe (Pub/Sub).
With Pub/Sub, one component publishes messages to a channel, and other components subscribe to that channel to receive the messages instantly.
This article shows how to implement Redis Pub/Sub messaging in a Spring Boot application.
1. How Redis Pub/Sub Works
Redis Pub/Sub has three main parts:
- Publisher – sends messages to a channel
- Channel – the message topic
- Subscriber – listens for messages on the channel
Example flow:
Publisher → Redis Channel → Subscriber
Whenever a message is published, all subscribers receive it immediately.
2. Redis Configuration in Spring Boot
Add Redis connection settings.
spring.redis.host=localhost
spring.redis.port=6379
Spring Boot automatically configures Redis connectivity.
3. Creating a Message Publisher
The publisher sends messages to a Redis channel.
@Service
public class RedisPublisher {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void publish(String channel, String message) {
redisTemplate.convertAndSend(channel, message);
}
}
Example usage:
channel: notifications
message: New order created
4. Creating a Message Subscriber
Subscribers listen to Redis channels and handle incoming messages.
@Component
public class RedisSubscriber {
public void handleMessage(String message) {
System.out.println("Received message: " + message);
}
}
Whenever a message arrives, the subscriber processes it immediately.
5. Configuring the Redis Message Listener
Spring Boot needs a listener container to subscribe to channels.
@Configuration
public class RedisConfig {
@Bean
public RedisMessageListenerContainer container(
RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container =
new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter,
new PatternTopic("notifications"));
return container;
}
@Bean
public MessageListenerAdapter listenerAdapter(RedisSubscriber subscriber) {
return new MessageListenerAdapter(subscriber, "handleMessage");
}
}
Now the application subscribes to the
notifications channel.
6. Sending Messages via REST API
Create an API endpoint to publish messages.
@RestController
@RequestMapping("/messages")
public class MessageController {
@Autowired
private RedisPublisher publisher;
@PostMapping
public String sendMessage(@RequestParam String message) {
publisher.publish("notifications", message);
return "Message sent";
}
}
Test example:
POST /messages?message=Hello
Subscribers instantly receive the message.
7. Real Use Cases
Redis Pub/Sub is useful for:
- Real-time notifications
- Event-driven microservices
- Chat applications
- Cache invalidation events
- System monitoring alerts
8. Redis Pub/Sub vs Message Brokers
Redis Pub/Sub is lightweight compared to systems like Kafka or RabbitMQ.
Advantages:
- Very fast messaging
- Simple implementation
- Low latency
However, Redis Pub/Sub does
not persist messages, meaning if a subscriber is offline, messages are lost.
image quote pre code