Spring boot: Scaling with Redis Pub/Sub

Jhamukul
3 min readDec 15, 2022

There are many famous messaging queues.
like Kafka, RabbitMQ etc

Apache Kafka :

Scale: Apache Kafka can scale up to send a million messages every second.

Persistency: It supports data persistence.

Message Scheduling: None

RabbitMQ:

Scale: RabbitMQ can scale up to around 50,000 messages per second.
Persistency: RabbitMQ supports both, persistent as well as transient messages.

Message Scheduling: You can schedule messages in RabbitMQ by using rabbitmq_delayed_message_exchange

https://blog.rabbitmq.com/posts/2015/04/scheduling-messages-with-rabbitmq

Redis
Scale: Redis can send up to 1 million messages per second.

Persistency: It does not support data persistency, it is an in-memory data store.

Message Scheduling: None

Advantage:

  1. Redis is faster than Apache Kafka and RabbitMQ
  2. You don’t need to create a channel, push data into the channel, and the channel automatically gets created.
  3. A configuration is very less and time-saving. You can use the Redis cache cluster for Redis pub/sub.

If you don’t need data persistence, then Redis is good for your requirements.
I have used Redis pub/sub in socket communication.

Reason to choose Redis over Apache Kafka.
1. Latency should be low in real-time applications like online classes, and meetings.

2. Mostly we broadcast socket events over Redis and the Redis cluster is always available so data persistency doesn't matter here.

3. If we lose any event that’s okay. we persist important events such as chats into DB/cache.

I hope you understand the reason.

I have used Apache Kafka, and RabbitMQ in the same project to serve different purposes.
I will write a blog on Apache Kafka and RabbitMQ with the proper explanation.

Let’s Configure Redis pub/sub with the Spring boot application.
Dependencies required

Gradle:

implementation 'org.springframework.data:spring-data-redis'
implementation 'redis.clients:jedis'

Maven:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

Configuration :

You can use the same configuration for the Redis cache as well.

Producer Configuration:
Publishing messages on channel testing-chat

Configuration done.

We have configured Redis with host, port, and assigned channel. Creating producer.

Producer implementation class

Now you can publish data to channel testing-chat.

Verify published data in redis-cli by using these commands.

UE-M-NOTE-0180:~ mukul.jha$ redis-cli
127.0.0.1:6379> subscribe testing-chat

Look it is effortless to subscribe to a channel using redis-cli. Now you can see all messages published on the subscribed channel.

I have published a message from the backend producer and it is visible on redis-cli.

Let’s configure the consumer in the Spring Boot application.

You need a message listener in consumer configuration. Let’s create it first.

Now add consumer configuration.

Now you are ready to produce messages and consume those messages using redis producer subscriber.

redis-cli

Source codes are available on GitHub. 😁

Happy Learning !!! I hope you like it !! 😊

--

--