

Sending message: I want a pizza 61 to redis pubsub. Sending message: I want a pizza 60 to redis pubsub. Sending message: I want a pizza 59 to redis pubsub.Īpp2 handle: pizza 59 # app2 is turned on again Sending message: I want a pizza 58 to redis pubsub. Sending message: I want a pizza 57 to redis pubsub. Sending message: I want a pizza 56 to redis pubsub. Sending message: I want a pizza 55 to redis pubsub. Sending message: I want a pizza 54 to redis pubsub. Sending message: I want a pizza 53 to redis pubsub. Sending message: I want a pizza 52 to redis pubsub.Īpp3 handle: pizza 52 # app2 is turned off Sending message: I want a pizza 51 to redis pubsub. If we run all of the apps and monitor the app1 log, we will see something like this:

Whenever app2 and app3 got the message, they will do a post request to app1 to tell app1 that they got the message. In this demo, app1 is the publisher that will publish a message to Redis Pub/Sub each second, while app2 and app3 subscribe to Redis to get the message from app1. You can look at the README.md file to set up the application. This demo is using Nodejs Language and Redis as Pub/Sub and Message Queue systems. You can clone the repo here: pubsub vs queue.
#PUB SUB MESSAGE QUEUE CODE#
Let’s get to the demo, I already prepare the source code if you want to try and see it for yourself. In this communication, A doesn’t know who got the message, but it is guaranteed that the message will be sent to other services. If no one request the message, then the X will keep the message until someone requests it. In this case, B send a request to X to see if there is a message. In Message Queue, A produce a message to X, and then X will wait for another service to request and consume the message. So, how is that different from HTTP? Well, HTTP can send messages directly from point A to B, while Pub/Sub and Message Queue need some intermediary. What I know is Pub/Sub and Message Queue is some kind of method to send a message/event asynchronously from 1 service to another service. Well, I don’t know about you, but the definition does not matter that much to me as a developer.

I hope you guys will understand better the difference between Pub/Sub and Message Queue. So here I made a small project using NodeJs and Redis to see the difference, and I will share it with all of you. When I first learned about Pub/Sub and Message Queues, I was very confused about the difference between the two. Pub/Sub and Message Queue are often used for alternative methods of communication between microservices other than HTTP. If you are learning about microservices, you must have heard the term Pub/Sub or Message Queue.
