Chapter 17. Flow Control

Flow control is used to limit the flow of data between a client and server, or a server and a server in order to prevent the client or server being overwhelmed with data.

17.1. Consumer Flow Control

This controls the flow of data between the server and the client as the client consumes messages. For performance reasons clients normally buffer messages before delivering to the consumer via the receive() method or asynchronously via a message listener. If the consumer cannot process messages as fast as they are being delivered and stored in the internal buffer, then you could end up with a situation where messages just keep building up and are not processed for a long time.

17.1.1. Window-Based Flow Control

By default, JBoss Messaging consumers buffer messages from the server in a client side buffer before the client consumes them. This improves performance: otherwise every time the client consumes a message, JBoss Messaging would have to go the server to request the next message. In turn, this message would then get sent to the client side, if one was available.

A network round trip would be involved for every message and considerably reduce performance.

To prevent this, JBoss Messaging pre-fetches messages into a buffer on each consumer. The total maximum size of messages (in bytes) that will be buffered on each consumer is determined by the consumer-window-size parameter.

By default, the consumer-window-size is set to 1 MiB (1024 * 1024 bytes).

The value can be:

Setting the consumer window size can considerably improve performance depending on the messaging use case. As an example, let's consider the two extremes:

Fast consumers

Fast consumers can process messages as fast as they consume them (or even faster)

To allow fast consumers, set the consumer-window-size to -1. This will allow unbounded message buffering on the client side.

Use this setting with caution: it can overflow the client memory if the consumer is not able to process messages as fast as it receives them.

Slow consumers

Slow consumers takes significant time to process each message and it is desirable to prevent buffering messages on the client side so that they can be delivered to another consumer instead.

Consider a situation where a queue has 2 consumers 1 of which is very slow. Messages are delivered in a round robin fashion to both consumers, the fast consumer processes all of its messages very quickly until its buffer is empty. At this point there are still messages awaiting to be processed by the slow consumer which could be being consumed by the other consumer.

To allow slow consumers, set the consumer-window-size to 0 (for no buffer at all). This will prevent the slow consumer from buffering any messages on the client side. Messages will remain on the server side ready to be consumed by other consumers.

Most of the consumers cannot be clearly identified as fast or slow consumers but are in-between. In that case, setting the value of consumer-window-size to optimize performance depends on the messaging use case and requires benchmarks to find the optimal value, but a value of 1MiB is fine in most cases.

17.1.1.1. Using Core API

If JBoss Messaging Core API is used, the consumer window size is specified by ClientSessionFactory.setConsumerWindowSize() method and some of the ClientSession.createConsumer() methods.

17.1.1.2. Using JMS

if JNDI is used to look up the connection factory, the consumer window size is configured in jbm-jms.xml:

<connection-factory name="ConnectionFactory">
   <connector-ref connector-name="netty-connector"/>
   <entries>
      <entry name="ConnectionFactory"/>       
   </entries>
      
   <!-- Set the consumer window size to 0 to have *no* buffer on the client side -->
   <consumer-window-size>0</consumer-window-size>
</connection-factory>
            

If the connection factory is directly instantiated, the consumer window size is specified by JBossConnectionFactory.setConsumerWindowSize() method.

Please see Section 9.1.33, “No Consumer Buffering” for an example which shows how to configure JBoss Messaging to prevent consumer buffering when dealing with slow consumers.

17.1.2. Rate limited flow control

It is also possible to control the rate at which a consumer can consumer messages. This is a form of throttling and can be used to make sure that a consumer never consumes messages at a rate faster than the rate specified.

The rate must be a positive integer to enable and is the maximum desired message consumption rate specified in units of messages per second. Setting this to -1 disables rate limited flow control. The default value is -1.

Please see Section 9.1.26, “Message Consumer Rate Limiting” for a working example of limiting consumer rate.

17.1.2.1. Using Core API

If the JBoss Messaging core API is being used the rate can be set via the ClientSessionFactory.setConsumerMaxRate(int consumerMaxRate) method or alternatively via some of the ClientSession.createConsumer() methods.

17.1.2.2. Using JMS

If JNDI is used to look up the connection factory, the max rate can be configured in jbm-jms.xml:

<connection-factory name="ConnectionFactory">
      <connector-ref connector-name="netty-connector"/>
      <entries>
         <entry name="ConnectionFactory"/>       
      </entries>
      <!-- We limit consumers created on this connection factory to consume messages
             at a maximum rate
      of 10 messages per sec -->
      <consumer-max-rate>10</consumer-max-rate>
 </connection-factory>

If the connection factory is directly instantiated, the max rate size can be set via the JBossConnectionFactory.setConsumerMaxRate(int consumerMaxRate) method.

Note

Rate limited flow control can be used in conjunction with window based flow control. Rate limited flow control only effects how many messages a client can consume in a second and not how many messages are in its buffer. So if you had a slow rate limit and a high window based limit the clients internal buffer would soon fill up with messages.

Please see Section 9.1.26, “Message Consumer Rate Limiting” for an example which shows how to configure JBoss Messaging to prevent consumer buffering when dealing with slow consumers.

17.2. Producer flow control

JBoss Messaging also can limit the amount of data sent from a client to a server to prevent the server being overwhelmed.

17.2.1. Window based flow control

JBoss Messaging clients maintain a buffer of commands that have been sent to the server, thus provides a form of flow control. Please see Chapter 18, Command Buffering for more information on this.

17.2.2. Rate limited flow control

JBoss Messaging also allows the rate a producer can emit message to be limited, in units of messages per second. By specifying such a rate, JBoss Messaging will ensure that producer never produces messages at a rate higher than that specified.

The rate must be a positive integer to enable and is the maximum desired message consumption rate specified in units of messages per second. Setting this to -1 disables rate limited flow control. The default value is -1.

Please see the Section 9.1.30, “Message Producer Rate Limiting” for a working example of limiting producer rate.

17.2.2.1. Using Core API

If the JBoss Messaging core API is being used the rate can be set via the ClientSessionFactory.setProducerMaxRate(int consumerMaxRate) method or alternatively via some of the ClientSession.createProducer() methods.

17.2.2.2. Using JMS

If JNDI is used to look up the connection factory, the max rate can be configured in jbm-jms.xml:

<connection-factory name="ConnectionFactory">
      <connector-ref connector-name="netty-connector"/>
      <entries>
         <entry name="ConnectionFactory"/>       
      </entries>
      <!-- We limit producers created on this connection factory to produce messages 
                at a maximum rate
      of 10 messages per sec -->
      <producer-max-rate>10</producer-max-rate>
 </connection-factory>

If the connection factory is directly instantiated, the max rate size can be set via the JBossConnectionFactory.setProducerMaxRate(int consumerMaxRate) method.