liangyepianzhou commented on code in PR #875: URL: https://github.com/apache/pulsar-site/pull/875#discussion_r1554564350
########## docs/tutorials-redeliver-messages.md: ########## @@ -0,0 +1,236 @@ +--- +Id: tutorials-redeliver-messages +title: Consume best practice +sidebar_label: "Consume best practice" +description: Learn how to consume messages and redeliver unacknowledged messages in Pulsar. +--- + +# Consume Best Practice + +## Background Knowledge + +### Subscription Types + +Pulsar is a distributed message system where messages can be sent to topics by producers and consumed by consumers. +Consumers can subscribe to the topics in four ways (subscription types): + +* **Exclusive** +* **Failover** +* **Shared** +* **Key-shared** + +The messages are consumed in order for a single partition in Exclusive and Failover modes and out of order for Shared and +Key-shared mode. The main difference between Exclusive and Failover is that in Exclusive mode, the consumer is exclusive +to the entire topic, while in Failover mode, the consumer is exclusive to only one partition. Failover mode allows backup +consumer connections that are not consumed. The main difference between Shared and Key-shared is whether their dispatch +strategy is implemented via a key. For more information about subscription type, refer to the [Pulsar website](https://pulsar.apache.org/docs/3.2.x/concepts-messaging/). +![img.png](../static/img/blog-consume-best-practice/subscription-types.png) + +### Acknowledgment + +The messages should be acknowledged after they are fully consumed and processed, and then the messages would not be received +for the same subscription again. Pulsar provides two ways to acknowledge messages: + +* **Cumulative acknowledgment** +* **Individual acknowledgment** + +Cumulative acknowledgment receives a message or a message id as a parameter and marks the messages before the message as +consumed for this subscription. For multiple-partition topics, the cumulative acknowledgment will work for the single Review Comment: The cumulative ack is recommended to be used in the `exclusive` mode and `Failover` mode. For the `Exclusive` mode, the messages from all the partitions are sent to the same consumer. But the cumulative ack only marks the messages as processed for the single partition. For the `Exclusive` mode, if they consume 100 messages in a topic with 5 partitions and cumulative ack the last message that is a message in partition4. That does not mean the 100 messages before the last message are processed, it only means the messages in partition 4 before the last message are marked as processed. So I add a notice here. You can see more information in the following tests. ```java @Test public void test() throws Exception { String subName = "test"; String topicName = TopicName.get(NAMESPACE1 + "/" + "testCreateTransactionSystemTopic").toString(); admin.topics().createPartitionedTopic(topicName, 3); Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create(); Consumer<byte[]> consumer1 = pulsarClient.newConsumer().receiverQueueSize(100).topic(topicName) .subscriptionName(subName) .subscriptionType(SubscriptionType.Exclusive).subscribe(); for (int i = 0; i < 100; i++) { producer.newMessage().send(); } Message lastMessage = null; for (int i = 0; i < 100; i++) { lastMessage = consumer1.receive(); } log.info("ack message {}", lastMessage.getMessageId()); consumer1.acknowledgeCumulative(lastMessage); consumer1.redeliverUnacknowledgedMessages(); for (int i = 0; i < 30; i++) { lastMessage = consumer1.receive(); log.info("receive message {}", lastMessage.getMessageId()); } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pulsar.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org