Skip to main content

Redis publish subscribe to node.js

Disclaimer: This is no way a comparison between RabbitMQ and Redis. RabbitMQ is a great product and we use it in our startup as a queuing system. I just wanted to try out redis in my pet project so I did it.

I am intrigued by the hype around node.js so am planning of writing a node.js to push real time events from server and do push notifications in browser from server.

Now mostly I saw that people use a myriad of technologies from websocket, nginx, nodejs, redis/rabbitmq and in backend produce the message from Java or any other app.  Basically node.js is only used for long polling and decoupled from the main app using a message queue as a broker. So my first goal was to push message from Java to message queue and I have already used RabbitMQ in past so I thought let me this time try redis and boy it is much much easy then RabbitMQ (however at this moment I trust rabbitmq more than redis for production as I have much more experience scaling rabbitmq as a queuing system,  I just wanted to try out a different queuing system).

To connect to redis from java there are multiple implementations like JRedis and Jedis, in past in my startup we had used JRedis and then switched to Jedis but I am not happy with that code as we did all code ourselves so thought to check if spring has something because I am a big time fan of spring-jdbc and luckily I found spring has a similar abstraction to hide all this details using its spring-data-redis templates.  I used spring-data-redis-1.1.0.RELEASE and jedis-2.1.0.jar and I ran into issues with spring-3.X so I upgraded to spring-tx-4.0.1.RELEASE.jar

all I needed to do was to add this in spring config

 <bean id="jedisConnectionFactory" class=""
        p:host-name="" p:port="6379"/>
      <bean id="stringRedisTemplate" class=""
      <bean id="messagePublisher" class="">
        <property name="stringRedisTemplate" ref="stringRedisTemplate" />

and then

public class MessagePublisher {
    private static final AppLogger logger = AppLogger.getLogger(MessagePublisher.class);
    private StringRedisTemplate stringRedisTemplate;

    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;

    public void publishMessage(String channel, String message) {
        logger.debug("Publishing {} to channel {}", message, channel);
        stringRedisTemplate.convertAndSend(channel, message);

    public void publishJsonMessage(String channel, Object msgObject) {
        publishMessage(channel, JsonUtil.generateJSON(msgObject));

Now can you beat this.  2 lines of code to publish a message to message queue.  Totally impressed and icing on cake is that if you have to switch the library from jedis to Jredis then its just a config change and no code change is required and spring is handing all connection open/close.


  1. Rabbitmq is a queuing system. You can compare it with ExecutorService or a queue (push/pop) but the comparison with redis is inappropriate. The other point is you can use any technology the way you want but you need to look at the pros and cons too. For eg:- In redis, you may need to delete the message manually once processed if it has to behave like Rabbitmq. You do not have "ack" too.

  2. I think you are taking the post in a wrong spirit. RabbitMQ is a great product, Infact for the startup we use RabbitMQ and it has rarely given any issues. We had pushed hundreds of millions of messages via RabbitMQ and it didn’t sweat. I will put a disclaimer at top of the blog that this is not a comparison.


Post a Comment

Popular posts from this blog

RabbitMQ java clients for beginners

Here is a sample of a consumer and producer example for RabbitMQ. The steps are
Download ErlangDownload Rabbit MQ ServerDownload Rabbit MQ Java client jarsCompile and run the below two class and you are done.
This sample create a Durable Exchange, Queue and a Message. You will have to start the consumer first before you start the for the first time.

For more information on AMQP, Exchanges, Queues, read this excellent tutorial
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; import com.rabbitmq.client.*; public class RabbitMQProducer { public static void main(String []args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("guest"); factory.setPassword("guest"); factory.setVirtualHost("/"); factory.setHost(""); factory.setPort(5672); Conne…

What a rocky start to labor day weekend

Woke up by earthquake at 7:00 AM in morning and then couldn't get to sleep. I took a bath, made my tea and started checking emails and saw that after last night deployment three storage node out of 100s of nodes were running into Full GC. What was special about the 3 nodes was that each one was in a different Data centre but it was named same app02.  This got me curious I asked the node to be taken out of rotation and take a heap dump.  Yesterday night a new release has happened and I had upgraded spymemcached library version as new relic now natively supports instrumentation on it so it was a suspect. And the hunch was a bullseye, the heap dump clearly showed it taking 1.3G and full GCs were taking 6 sec but not claiming anything.

I have a quartz job in each jvm that takes a thread dump every 5 minutes and saves last 300 of them, checking few of them quickly showed a common thread among all 3 data centres. It seems there was a long running job that was trying to replicate pending…

Email slavery

It seems I have become an EmailSlave. The first half of the day is spent in just answering to emails. There are so many emails where I am copied but I need not be. There are many emails  where its a 1-2 page email and somewhere down someone says @KP please answer this.  So it seems daily my work schedule is:
Signin to newrelic and check anomalies for 15 min. Check emails related production exception report and yes there are a ton of these report daily. Need a better tool here as this model is not scalable. I need to reduce the incoming data at me to only see relevant data like what newrelic does. May be I need to create a webapp out of these emails.Check emails for next few minutes before team callsDo team callsThen again back to checking emails until a I have taken a best shot at answering everyone waiting for my reply.Attend team meetings on Tue/Thu
Being an architect and coder at heart I don't feel satisfied at end of the day if there is nothing tangible getting done at the end.…