Failover Subscription

Continuing to use our sample code from the StreamNative UI, let’s test out the Failover Subscription type. We will start multiple consumers using the same subscription on the same topic. We observe that multiple consumers are allowed to connect to the topic but only one will receive messages. If the first consumer is stopped, the second consumer will begin consuming messages. We will revisit the Failover Subscription later when using a topic with more than one partition since the behavior changes.

The Failover Subscription is specified in the consumer configuration:

Consumer consumer = client.newConsumer()
.topic("persistent://public/default/mynewtopic")
.subscriptionName("mysubscription")
.subscriptionType(SubscriptionType.Failover)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.subscribe();

Notice that we have changed the subscription type on the topic. This is possible if all consumers disconnect from the topic and a consumer reconnects using a different subscription type. We can see the updated subscription type in the StreamNative UI.

Note that message ordering is retained with a Failover Subscription. We will revisit this when we increase the number of partitions of our topic.