[Bug] Consumer receives duplicate messages even after acknowledging them.
Author: shibdCreated Oct 11, 2024Updated Oct 1, 2026
Labelstype/bugtriage/lhotari/important
Search before asking
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
Version
All version
Minimal reproduce step
- Start standalone pulsar
- produce some message(enable batch)
// Create a client
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://localhost:6650")
.build();
// Create a producer
ProducerBuilder<byte[]> producerBuilder = client.newProducer(Schema.BYTES)
.topic("persistent://public/default/my-topic")
.sendTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
.enableBatching(true)
.batchingMaxMessages(60)
.blockIfQueueFull(true);
Producer<byte[]> producer = producerBuilder.create();
// Send messages
for (int i = 0; i < 60; i++) {
String msg = "my-message-" + i;
producer.send(msg.getBytes());
System.out.println("Sent message: " + i);
}
producer.flush();
producer.close();
client.close();- Consume these message
public static void main(String[] args) throws Exception {
PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar://localhost:6650")
.build();
Consumer<byte[]> consumer = client.newConsumer()
.topic("persistent://public/default/my-topic")
.subscriptionName("test-sub")
.subscriptionType(SubscriptionType.Shared)
.subscribe();
// Set to store processed messageIds
Set<MessageId> processedMessageIds = new HashSet<>();
while (true) {
System.out.println("Start call batch receive");
Message<byte[]> message = consumer.receive();
MessageId messageId = message.getMessageId();
String messageData = new String(message.getData());
System.out.printf("Received message: %s: %s%n", messageId.toString(), messageData);
if ("my-message-59".equals(messageData)) {
consumer.negativeAcknowledge(message);
System.out.printf("Nack acknowledge %s messages%n", messageId.toString());
} else {
consumer.acknowledge(message);
System.out.printf("Acknowledge %s messages%n", messageId.toString());
}
if (processedMessageIds.contains(messageId)) {
System.out.printf("Received duplicate messageId: %s%n", messageId.toString());
} else {
processedMessageIds.add(messageId);
}
Thread.sleep(10);
}
}What did you expect to see?
Don't print messages 1 to 58, only print message 59.
What did you see instead?
All messages are being received repeatedly.
Anything else?
- I know enabling batchIndexAck can resolve this issue.
- However, it's confusing for users because they acknowledge some messages, yet they still receive duplicates.
- We have deduplication logic: https://github.com/apache/pulsar/pull/21116, but it's not utilized if the client doesn't enable
batchIndexAck. - Even if the client enables batchIndexAck, if the broker doesn't, duplicate messages will still be received. This is because when the client sends the ack to the broker, the broker ignores it, causing the client's bitset to be cleared, resulting in duplicate messages.
Are you willing to submit a PR?
- I'm willing to submit a PR!
Source: apache/pulsar