Using Pulsar as a message queue

Message queues are essential components of many large-scale data architectures. If every single work object that passes through your system absolutely must be processed in spite of the slowness or downright failure of this or that system component, there’s a good chance that you’ll need a message queue to step in and ensure that unprocessed data is retained—-with correct ordering—-until the required actions are taken.

Pulsar is a great choice for a message queue because:

You can use the same Pulsar installation to act as a real-time message bus and as a message queue if you wish (or just one or the other). You can set aside some topics for real-time purposes and other topics for message queue purposes (or use specific namespaces for either purpose if you wish).

Client configuration changes

To use a Pulsar topic as a message queue, you should distribute the receiver load on that topic across several consumers (the optimal number of consumers will depend on the load). Each consumer must:

  • Establish a shared subscription and use the same subscription name as the other consumers (otherwise the subscription is not shared and the consumers can’t act as a processing ensemble)

  • If you’d like to have tight control over message dispatching across consumers, set the consumers’ receiver queue size very low (potentially even to 0 if necessary). Each Pulsar consumer has a receiver queue that determines how many messages the consumer will attempt to fetch at a time. A receiver queue of 1000 (the default), for example, means that the consumer will attempt to process 1000 messages from the topic’s backlog upon connection. Setting the receiver queue to zero essentially means ensuring that each consumer is only doing one thing at a time.

    The downside to restricting the receiver queue size of consumers is that that limits the potential throughput of those consumers and cannot be used with partitioned topics. Whether the performance/control trade-off is worthwhile will depend on your use case.

Java clients

Here’s an example Java consumer configuration that uses a shared subscription:

  1. import org.apache.pulsar.client.api.Consumer;
  2. import org.apache.pulsar.client.api.PulsarClient;
  3. import org.apache.pulsar.client.api.SubscriptionType;
  4. String SERVICE_URL = "pulsar://localhost:6650";
  5. String TOPIC = "persistent://public/default/mq-topic-1";
  6. String subscription = "sub-1";
  7. PulsarClient client = PulsarClient.builder()
  8. .serviceUrl(SERVICE_URL)
  9. .build();
  10. Consumer consumer = client.newConsumer()
  11. .topic(TOPIC)
  12. .subscriptionName(subscription)
  13. .subscriptionType(SubscriptionType.Shared)
  14. // If you'd like to restrict the receiver queue size
  15. .receiverQueueSize(10)
  16. .subscribe();

Python clients

Here’s an example Python consumer configuration that uses a shared subscription:

  1. from pulsar import Client, ConsumerType
  2. SERVICE_URL = "pulsar://localhost:6650"
  3. TOPIC = "persistent://public/default/mq-topic-1"
  4. SUBSCRIPTION = "sub-1"
  5. client = Client(SERVICE_URL)
  6. consumer = client.subscribe(
  7. TOPIC,
  8. SUBSCRIPTION,
  9. # If you'd like to restrict the receiver queue size
  10. receiver_queue_size=10,
  11. consumer_type=ConsumerType.Shared)

C++ clients

Here’s an example C++ consumer configuration that uses a shared subscription:

  1. #include <pulsar/Client.h>
  2. std::string serviceUrl = "pulsar://localhost:6650";
  3. std::string topic = "persistent://public/defaultmq-topic-1";
  4. std::string subscription = "sub-1";
  5. Client client(serviceUrl);
  6. ConsumerConfiguration consumerConfig;
  7. consumerConfig.setConsumerType(ConsumerType.ConsumerShared);
  8. // If you'd like to restrict the receiver queue size
  9. consumerConfig.setReceiverQueueSize(10);
  10. Consumer consumer;
  11. Result result = client.subscribe(topic, subscription, consumerConfig, consumer);

Go clients

Here is an example of a Go consumer configuration that uses a shared subscription:

  1. import "github.com/apache/pulsar-client-go/pulsar"
  2. client, err := pulsar.NewClient(pulsar.ClientOptions{
  3. URL: "pulsar://localhost:6650",
  4. })
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. consumer, err := client.Subscribe(pulsar.ConsumerOptions{
  9. Topic: "persistent://public/default/mq-topic-1",
  10. SubscriptionName: "sub-1",
  11. Type: pulsar.Shared,
  12. ReceiverQueueSize: 10, // If you'd like to restrict the receiver queue size
  13. })
  14. if err != nil {
  15. log.Fatal(err)
  16. }