BroadwayRabbitMQ v0.4.0 BroadwayRabbitMQ.Producer View Source

A RabbitMQ producer for Broadway.

Features

  • Automatically acknowledges/rejects messages.
  • Handles connection outages using backoff for retries.

Options

  • :queue - Required. The name of the queue. If "", then the queue name will be autogenerated by the server but for this to work you have to declare the queue through the :declare option.
  • :connection - Optional. Defines an AMQP URI or a set of options used by the RabbitMQ client to open the connection with the RabbitMQ broker. See AMQP.Connection.open/1 for the full list of options.
  • :qos - Optional. Defines a set of prefetch options used by the RabbitMQ client. See AMQP.Basic.qos/2 for the full list of options. Pay attention that the :global option is not supported by Broadway since each producer holds only one channel per connection.
  • :buffer_size - Optional, but required if :prefetch_count under :qos is set to 0. Defines the size of the buffer to store events without demand. Can be :infinity to signal no limit on the buffer size. This is used to configure the GenStage producer, see the GenStage docs for more details. Defaults to :prefetch_count * 5.
  • :buffer_keep - Optional. Used in the GenStage producer configuration. Defines whether the :first or :last entries should be kept on the buffer in case the buffer size is exceeded. Defaults to :last.
  • :requeue - Optional. Defines a strategy for requeuing failed messages. Possible values are: :always - always requeue, :never - never requeue, :once - requeue it once when the message was first delivered. Reject it without requeueing, if it's been redelivered. Default is :always.
  • :backoff_min - The minimum backoff interval (default: 1_000)
  • :backoff_max - The maximum backoff interval (default: 30_000)
  • :backoff_type - The backoff strategy, :stop for no backoff and to stop, :exp for exponential, :rand for random and :rand_exp for random exponential (default: :rand_exp)
  • :metadata - The list of AMQP metadata fields to copy (default: [])
  • :declare - Optional. A list of options used to declare the :queue. The queue is only declared (and possibly created if not already there) if this option is present and not nil. Note that if you use "" as the queue name (which means that the queue name will be autogenerated on the server), then every producer stage will declare a different queue. If you want all producer stages to consume from the same queue, use a specific queue name. You can still declare the same queue as many times as you want because queue creation is idempotent (as long as you don't use the passive: true option). For the available options, see AMQP.Queue.declare/3.
  • :bindings - Optional. a list of bindings for the :queue. This option allows you to bind the queue to one or more exchanges. Each binding is a tuple {exchange_name, binding_options} where so that the queue will be bound to exchange_name through AMQP.Queue.bind/4 using binding_options as the options. Bindings are idempotent so you can bind the same queue to the same exchange multiple times.

Note: choose the requeue strategy carefully. If you set the value to :never or :once, make sure you handle failed messages properly, either by logging them somewhere or redirecting them to a dead-letter queue for future inspection. By sticking with :always, pay attention that requeued messages by default will be instantly redelivered, this may result in very high unnecessary workload. One way to handle this is by using Dead Letter Exchanges and TTL and Expiration.

Example

Broadway.start_link(MyBroadway,
  name: MyBroadway,
  producers: [
    default: [
      module:
        {BroadwayRabbitMQ.Producer,
        queue: "my_queue",
        requeue: :once,
        connection: [
          username: "user",
          password: "password",
          host: "192.168.0.10"
        ],
        qos: [
          prefetch_count: 50
        ]},
      stages: 5
    ]
  ],
  processors: [
    default: []
  ]
)

Back-pressure and :prefetch_count

Unlike the RabbitMQ client that has a default :prefetch_count = 0, which disables back-pressure, BroadwayRabbitMQ overwrite the default value to 50 enabling the back-pressure mechanism. You can still define it as 0, however, if you do this, make sure the machine has enough resources to handle the number of messages coming from the broker, and set :buffer_size to an appropriate value.

This is important because the BroadwayRabbitMQ producer does not work as a poller like BroadwaySQS. Instead, it maintains an active connection with a subscribed consumer that receives messages continuously as they arrive in the queue. This is more efficient than using the basic.get method, however, it removes the ability of the GenStage producer to control the demand. Therefore we need to use the :prefetch_count option to impose back-pressure at the channel level.

Connection loss and backoff

In case the connection cannot be opened or if a stablished connection is lost, the producer will try to reconnect using an exponential random backoff strategy. The strategy can be configured using the :backoff_type option.

Unsupported options

Currently, Broadway does not accept options for Basic.consume/4 which is called internally by the producer with default values. That means options like :no_ack are not supported. If you have a scenario where you need to customize those options, please open an issue, so we can consider adding this feature.

Declaring queues and binding them to exchanges

In RabbitMQ, it's common for consumers to declare the queue they're going to consume from and bind it to the appropriate exchange when they start up. You can do these steps (either or both) when setting up your Broadway pipeline through the :declare and :bindings options.

Broadway.start_link(MyBroadway,
  name: MyBroadway,
  producers: [
    default: [
      module:
        {BroadwayRabbitMQ.Producer,
        queue: "my_queue",
        declare: [],
        bindings: [{"my-exchange", []}]},
      stages: 5
    ]
  ],
  processors: [
    default: []
  ]
)