Async Rest Services With Messaging

Tharaka Dissanayake
3 min readJul 19, 2022

--

Async mean it does not wait until it finishes. If there are multiple things to process sender does not wait until one process is completed. The sender always provides mails to the receiver.

Most of the time, we use REST API with HTTP protocol for messaging. But HTTP is synchronized, not synchronized. It waits until it processes the message. Therefore avoid it. We have to use Kafka or RabbitMQ.

Rabbit MQ is open-source message-broker software. Also, Kafka is used for the same job. RabbitMQ uses used Advanced Message Queueing Protocol (AMQP). It looks like an Ordinary Queue management System.

Kafka

Kafka is a distributed event store and stream-processing platform. The Apache Software Foundation produced Kafka. It was developed with Java and Scala.

RabbitMQ vs Kafka

RabbitMQ is an ordinary queue management protocol (First In, First Out). But when it comes to Kafka, it’s a stream processing system. It’s capable of processing a stream of messages.

RabbitMQ uses Advanced Message Queueing Protocol(AMPQ). But Kafka uses their clients and APIs (libraries). The main, most significant difference is that RabbitMQ is a Smart Broker, which means in RabbitMQ, we can do all those Routing, Content-based and Header-based routing inside the broker. But when it comes to Kafka, it’s a Dumb broker. You can’t do those advanced things inside the broker, but it has a different architecture to do the same thing.

Example

Let’s assume there is a purchase orders system. Once you make a purchase order, it must go through an approval process. Let’s consider this process takes 2 min. Two minutes waiting for the approved request is a bad thing. Rather than that, we give a button to submit it and send it to a queue in the backend, and the queue will take it and process it.

Again assume this system submits ten orders per minute for approval. We need a queue management solution for this. Regarding RabbitMQ, this queue management solution is better because this is the standard queue requirement (FIFO method).

But Kafka, on the other hand, can do the same thing. However, Kafka is not design something like this. Let’s consider we are processing purchase orders. There are two types of approvals going on.

In AMQP(RabbitMQ), this works using something called an exchange. Publisher publishes the message to Exchange. Exchanger sends the message to a different queue binder for this Exchange. So in Exchange, we have a route key to push the message to queue binders.

In Kafka, you can push it into one of the topics similarly. We can attach the consumers to the same topic, and consumers will get the message. Consumers decide what to do with this message.

Reference: Don’t Do this Mistake in Microservice projects | Kafka or RabbitMQ — YouTube

--

--

Tharaka Dissanayake
Tharaka Dissanayake

No responses yet