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 Deno Node Rust C# Java Ruby Elixir Crystal C
Jump to the output or the recording
$ nbe run jetstream/workqueue-stream/go
View the source code or learn how to run this example yourself


package main

import (


func main() {

Use the env variable if running in the container, otherwise use the default.

	url := os.Getenv("NATS_URL")
	if url == "" {
		url = nats.DefaultURL

Create an unauthenticated connection to NATS.

	nc, _ := nats.Connect(url)
	defer nc.Drain()

Access JetStreamContext to use the JS APIs.

	js, _ := nc.JetStream()

Creating the stream

Define the stream configuration, specifying WorkQueuePolicy for retention, and create the stream.

	cfg := &nats.StreamConfig{
		Name:      "EVENTS",
		Retention: nats.WorkQueuePolicy,
		Subjects:  []string{"events.>"},

	fmt.Println("created the stream")

Queue messages

Publish a few messages.

	js.Publish("", nil)
	js.Publish("", nil)
	js.Publish("", nil)
	fmt.Println("published 3 messages")

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

	fmt.Println("# Stream info without any consumers")
	printStreamState(js, cfg.Name)

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.

	sub1, _ := js.PullSubscribe("", "processor-1", nats.BindStream(cfg.Name))

Fetch and ack the queued messages.

	msgs, _ := sub1.Fetch(3)
	for _, msg := range msgs {

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

	fmt.Println("\n# Stream info with one consumer")
	printStreamState(js, cfg.Name)

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.

	_, err := js.PullSubscribe("", "processor-2", nats.BindStream(cfg.Name))
	fmt.Println("\n# Create an overlapping consumer")

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


	sub2, err := js.PullSubscribe("", "processor-2", nats.BindStream(cfg.Name))
	fmt.Printf("created the new consumer? %v\n", err == nil)

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.

	fmt.Println("\n# Create non-overlapping consumers")
	sub1, _ = js.PullSubscribe(">", "processor-us", nats.BindStream(cfg.Name))
	sub2, _ = js.PullSubscribe(">", "processor-eu", nats.BindStream(cfg.Name))

	js.Publish("", nil)
	js.Publish("", nil)
	js.Publish("", nil)
	js.Publish("", nil)
	fmt.Println("published 4 messages")

	msgs, _ = sub1.Fetch(2)
	for _, msg := range msgs {
		fmt.Printf("us sub got: %s\n", msg.Subject)

	msgs, _ = sub2.Fetch(2)
	for _, msg := range msgs {
		fmt.Printf("eu sub got: %s\n", msg.Subject)

This is just a helper function to print the stream state info 😉.

func printStreamState(js nats.JetStreamContext, name string) {
	info, _ := js.StreamInfo(name)
	b, _ := json.MarshalIndent(info.State, "", " ")


created the stream
published 3 messages
# Stream info without any consumers
 "messages": 3,
 "bytes": 157,
 "first_seq": 1,
 "first_ts": "2023-03-16T13:37:00.414795194Z",
 "last_seq": 3,
 "last_ts": "2023-03-16T13:37:00.415026239Z",
 "consumer_count": 0,
 "deleted": null,
 "num_deleted": 0,
 "num_subjects": 3,
 "subjects": null

# Stream info with one consumer
 "messages": 0,
 "bytes": 0,
 "first_seq": 4,
 "first_ts": "1970-01-01T00:00:00Z",
 "last_seq": 3,
 "last_ts": "2023-03-16T13:37:00.415026239Z",
 "consumer_count": 1,
 "deleted": null,
 "num_deleted": 0,
 "num_subjects": 0,
 "subjects": null

# Create an overlapping consumer
nats: multiple non-filtered consumers not allowed on workqueue stream
created the new consumer? true

# Create non-overlapping consumers
published 4 messages
us sub got:
us sub got:
eu sub got:
eu sub got:


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