Ask Your Question

How do Publisher/Subscriber Message Queues Work?

asked 2016-09-15 15:57:04 -0600

aak2166 gravatar image

Hi all,

I can't seem to find any documentation on this, but it I want to understand more deeply how messaging queues work.

My understanding is that each node has its own publisher and subscriber queues, and it is up the ROS backend to pull messages from publisher queues and push them onto subscriber queues.

Let's say I have a publisher that publishes int32s at a fast rate into a queue of size 10. When my node runs the publish() function, it will push values onto the end of the queue, [1, 2, 3, 4, 5, ...]. If the queue grows too large, queue_size will drop the values at the front of the queue leaving me with [2, 3, 4, 5, 6, ...]. Similarly, if ROS sends the first message in the queue over the wire that automatically pops it and leaves us with the same message queue of [2, 3, 4, 5, 6, ...].

Now on the subscriber end it seems like the logic is when the callback is called the first value is popped, similarly to the above, if the subscriber queue_size is exceeded, the backend is the one that pops the first values and will append the new one to the end in a FIFO manner.

Is this somewhat correct? Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-09-16 00:13:40 -0600

alienmon gravatar image

updated 2016-09-16 00:20:28 -0600

Publisher Subscriber Overview gives a good explanation on this:

When you create a publisher

ros::Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false);

The queue_size there is the publisher/outgoing message queue (B) .. If you are publishing faster than roscpp can send the messages over the wire, roscpp will start dropping OLD messages.

When you create a subscriber

ros::Subscriber subscribe(const std::string& topic, uint32_t queue_size, <callback, which may involve multiple arguments>, const ros::TransportHints& transport_hints = ros::TransportHints());

The queue_size there is the incoming message/subscriber queue (C) size roscpp will use for your callback. If messages are arriving too fast and you are unable to keep up (e.g. you do not spin/ the rate of spin is slow ), roscpp will start throwing away OLD messages.


I believe that everything is FIFO. When queue is full, the oldest message will be thrown away.


From reading "publish() behavior and queueing" from the link that I put. What I understand is :

  • publish() is asynchronous.
  • When you publish, messages are pushed into a queue (A) for later processing. This queue is immediately pushed into the outgoing/publisher queue (B) . PS: If no one subscribes to the topic, the end is here.
  • When a subscriber subscribes to that topic. Messages will be sent/pushed from the corresponding outgoing/publisher queue (B) to the incoming/subscriber queue (C).--> this is done by internal thread
  • When you spin/ callback, the messages handled are from the incoming/subscriber queue (C).


That is what I know, feel free to give any correction/ addition.

edit flag offensive delete link more



Does it mean that if you have two subscribers then the queue is duplicated? once per each subscriber?

Thank you, Octavio

opo gravatar imageopo ( 2017-10-18 13:44:36 -0600 )edit

If you have two subscribers, there is still a single queue, but instead of executing one callback on each message in the queue, both callbacks are executed on each message series.

Ed Venator gravatar imageEd Venator ( 2017-11-05 17:16:41 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-09-15 15:57:04 -0600

Seen: 11,369 times

Last updated: Oct 18 '17