How does RabbitMQ send messages to consumers?
Asked Answered
S

4

13

I am a newbie to RabbitMQ, hence need guidance on a basic question:

Does RabbitMQ send messages to consumer as they arrive?
OR
Does RabbitMQ send messages to consumer as they become available?

  • At message consumption endpoint, I am using com.rabbitmq.client.QueueingConsumer.
  • Looking at the sprint client source code, I could figure out that
    • QueueingConsumer keeps listening on socket for any messages the broker sends to it
    • Any message that is received is parsed and stored as Delivery in a LinkedBlockingQueue encapsulated inside the QueueingConsumer.
  • This implies that even if the message processing endpoint is busy, messages will be pushed to QueueingConsumer

Is this understanding right?

Sabbat answered 19/6, 2014 at 14:35 Comment(2)
Is it more like 'pull queue or push queue' question?Constellation
Does RabbitMQ send messages to consumer as they arrive? OR Does RabbitMQ send messages to consumer as they become available?Sabbat
E
11

TLDR: you poll messages from RabbitMQ till the prefetch count is exceeded in which case you will block and only receive heart beat frames till the fetch messages are ACKed. So you can poll but you will only get new messages if the number of non-acked messages is less than the prefetch count. New messages are put on the QueueingConsumer and in theory you should never really have much more than the prefetch count in that QueueingConsumer internal queue.

Details: Low level wise for (I'm probably going to get some of this wrong) RabbitMQ itself doesn't actually push messages. The client has to continuously read the connections for Frames based on the AMQP protocol. Its hard to classify this as push or pull but just know the client has to continuously read the connection and because the Java client is sadly BIO it is a blocking/polling operation. The blocking/polling is based on the AMQP heartbeat frames and regular frames and socket timeout configuration.

What happens in the Java RabbitMQ client is that there is thread for each channel (or maybe its connection) and that thread loops gathering frames from RabbitMQ which eventually become commands that are put in a blocking queue (I believe its like a SynchronousQueue aka handoff queue but Rabbit has its own special one).

The QueueingConsumer is a higher level API and will pull commands off of that handoff queue mentioned early because if commands are left on the handoff queue it will block the channel frame gathering loop. This is can be bad because timeout the connection. Also the QueueingConsumer allows work to be done on a separate thread instead of being in the same thread as the looping frame thread mentioned earlier.

Now if you look at most Consumer implementations you will probably notice that they are almost always unbounded blocking queues. I'm not entirely sure why the bounding of these queues can't be a multiplier of the prefetch but if they are less than the prefetch it will certainly cause problems with the connection timing out.

Esperance answered 19/6, 2014 at 17:43 Comment(0)
D
5

I think best answer is product's own answer. As RMQ has both push + pull mechanism defined as part of the protocol. Have a look : https://www.rabbitmq.com/tutorials/amqp-concepts.html

Dornick answered 20/8, 2015 at 9:55 Comment(0)
J
0

Rabbitmq mainly uses Push mechanism. Poll will consume bandwidth of the server. Poll also has time gaps between each poll. It will not able to achieve low latency. Rabbitmq will push the message to client once there are consumers available for the queue. So the connection is long running. ReadFrame in rabbitmq is basically waiting for incoming frames

Judd answered 17/8, 2020 at 6:31 Comment(0)
S
0

Consumers can use get or consume
get: gets a single message. control is with consumer. it can call get whenever it wants as per buss logic.
consume: consumer registers callback. RabbitMQ sends message when it becomes available. Connection is kept alive through heartbeat. Another feature of consume is that sending messages can be done in batches as specified by consumer.
By default, message stays on queue until consumer acknowledges. Consumer can auto ack, or acknowledge programatically. Till ack is not received, message stays in un-ack state.

Septima answered 27/5, 2023 at 15:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.