NATS Logo by Example

Work-queue Stream in JetStream

A work-queue retention policy satisfies a very common use case of queuing up messages that are intended to be processed once and only once.

This retention policy supports queuing up messages from publishers independent of consummption. Since each message is intended to be processed only once, this retention type allows for a set of consumers that have non-overlapping interest on subjects. In other words, if multiple consumers are bound to a work-queue stream, they must have disjoint filter subjects. This is in constrast to a standard limits-based or interest-based stream which supports multiple consumers with overlapping interest.

Like the interest policy this retention policy is additive to any limits set on the stream. As a contrived example, if max-msgs is set to one with old messages being discarded, every new message that is received by the stream will result in the prior message being deleted regardless if any subscriptions were available to process the message.

In this example, we will walk through the work-queue retention setup and behavior. If you are new to streams, it is recommended to read the limits-based stream example prior to reading this one.

CLI Go Python JavaScript Rust C# C#2 Java Ruby Elixir Crystal C
Jump to the output or the recording
$ nbe run jetstream/workqueue-stream/crystal
View the source code or learn how to run this example yourself


require "nats"
require "nats/jetstream"

Get the NATS_URL from the environment or fallback to the default. This can be a comma-separated string. We convert it to an Array(URI) to pass to the NATS client.

servers = ENV.fetch("NATS_URL", "nats://localhost:4222")
  .map { |url| URI.parse(url) }

Create a client connection to an available NATS server.

nats =

When the program exits, we close the NATS client which waits for any pending messages (published or in a subscription) to be flushed.

at_exit { nats.close }

js = nats.jetstream

Create the stream

Define the stream configuration, specifying :workqueue for the retention policy.

stream =
  name: "EVENTS",
  retention: :workqueue,
  subjects: %w[events.>],
  storage: :file,
puts "Created the stream"

Queue messages

Publish a few messages.

js.publish "", ""
js.publish "", ""
js.publish "", ""
puts "Published 3 messages"

Checking the stream info, we see three messages have been queued.

puts "Stream info without any consumers:"

Adding a consumer

Now let’s add a consumer and publish a few more messages. It can be a push or pull consumer. For this example, we are defining a pull consumer.

consumer1 = js.consumer.create(
  durable_name: "processor-1",
sub1 = js.pull_subscribe(consumer1)

Fetch and ack the queued messages

msgs = sub1.fetch(3)
msgs.each { |msg| js.ack_sync msg }

Checking the stream info again, we will notice no messages are available.

puts "Stream info with 1 consumer:"

Exclusive non-filtered consumer

As noted in the description above, work-queue streams can only have at most one consumer with interest on a subject at any given time. Since the pull consumer above is not filtered, if we try to create another one, it will fail.

  puts "Creating an overlapping consumer"
  consumer2 = js.consumer.create(
    durable_name: "processor-2",
rescue ex
  puts "** #{ex}"

However, if we delete the first one, we can add the new one.

puts "Deleting first consumer"
js.consumer.delete consumer1

puts "Creating second consumer:"
pp consumer2 = js.consumer.create(
  durable_name: "processor-2",
js.consumer.delete consumer2

Multiple filtered consumers

To create multiple consumers, a subject filter needs to be applied. For this example, we could scope each consumer to the geo that the event was published from, in this case us or eu.

puts "Creating non-overlapping consumers"
consumer1 = js.consumer.create(
  durable_name: "processor-us",
  filter_subject: ">",
consumer2 = js.consumer.create(
  durable_name: "processor-eu",
  filter_subject: ">",
sub1 = js.pull_subscribe(consumer1)
sub2 = js.pull_subscribe(consumer2)

js.publish("", "")
js.publish("", "")
js.publish("", "")
js.publish("", "")
puts "Published 4 messages"

sub1.fetch(2).each do |msg|
  puts "US subscription got: #{msg.subject}"
  js.ack msg
sub2.fetch(2).each do |msg|
  puts "EU subscription got: #{msg.subject}"
  js.ack msg


Created the stream
Published 3 messages

Stream info without any consumers:
 @first_ts=2023-03-16 13:37:24.550721827 UTC,
 @last_ts=2023-03-16 13:37:24.551020697 UTC,
Stream info with 1 consumer:
 @first_ts=1970-01-01 00:00:00.0 UTC,
 @last_ts=2023-03-16 13:37:24.551020697 UTC,

Creating an overlapping consumer
** multiple non-filtered consumers not allowed on workqueue stream (400)

Deleting first consumer
Creating second consumer:
  NATS::JetStream::API::V1::Consumer::Sequence(@consumer_seq=0, @stream_seq=0),
 @created=2023-03-16 13:37:24.555030218 UTC,
  NATS::JetStream::API::V1::Consumer::Sequence(@consumer_seq=0, @stream_seq=3),

Creating non-overlapping consumers
Published 4 messages
US subscription got:
US subscription got:
EU subscription got:
EU subscription got:


Note, playback is half speed to make it a bit easier to follow.