pubsub: High amount of redeliveries and modAck failures without clear explanation
Client
PubSub 1.45.3.
Environment
Pods running in GKE, europe-west1. Go 1.22.
Code and Dependencies
It's a bit hard to provide a sample as we use a library to wrap the client, but if what's below isn't enough tell me and I'll work on something :)
Behaviour
We are observing a weird behaviour related to exactly-once delivery that we cannot understand. We spent a lot of time investigating, looking at traces, tweaking parameters and couldn't find a satisfying answer and/or fix. We are thinking it might be a bug, but we (of course) can't be 100% sure.
In short, we see messages being redelivered when they shouldn't be. Here are our parameters:
- 150 pods subscribing (streaming pull) to a single subscription
- Subscription has exactly-once delivery enabled and 600s default deadline
- 10 PubSub client goroutines per pod
- 200 outstanding messages
- 4h max deadline
- 10 min minimum extension period in pods
Our metrics show significant ack/modack failures:
We added a lock-like mechanism using a Redis instance, and we can indeed see lots of messages being redelivered:
At first we thought something was wrong with our deadlines or with the deadline extension mechanism which would cause messages to expire and modAcks from the (unaware) client to fail, so we enabled tracing.
Let's take a single messageId, and look at the logs:
The id shown in the logs is an internal id, but we also attach the PubSub messageID as a tag, and all those logs belong to the same messageID. Notice how the message was first received in a pod at 10:40:28, and received by another one at 10:41:40, less than two minutes after.
We also have the trace of the first pod processing that message:
The message was received at 10:40:26 and modAcked upon reception (receipt modAck), as it should be. If we go and look at the modAck trace itself, it asks for 600s seconds extension, as expected. Message was nacked at +1m14.205s because it detected a loss of ownership.
If we look at the trace of the second pod (re)processing the message:
Same messageId, marked as the second delivery attempt by PubSub. This trace starts at 10:41:40, i.e. in the middle of the previous one. At this point, it is impossible (I think) that the message first delivery has expired, seeing that the default sub deadline is 600s, that the first modAck was sent immediately requesting an extension of 600s, and that (I think) if it had failed the message wouldn't even have been processed because the client would have popped it from the list.
With exactly once delivery enabled, messages shouldn't be redelivered while outstanding. The message wasn't acked/nacked or expired, according to the trace, and yet it was redelivered!
Since modAck status don't appear in the spans it's hard to say if the modAck failures we see in our metrics are related, but that wouldn't change much as the default deadline hasn't passed yet.
We are a bit puzzled, as it looks just as if PubSub doesn't care about the exactly once parameter and redelivers the message anyway... yet acks/modacks on invalid ids are rejected.
Both pods run in the same zone (and our cluster is in a single region anyway).
There is a (not really clear) paragraph PubSub that sounds like it could explain what's going on:
We're not sure what it means though. Since the messageID is the same, what does it mean for PubSub to do "multiple unique publishes"? If it means multiple redelivers can happen anyway, that sounds like it would defeat the purpose of exactly-once delivery...
Would you be able to provide pointers or potential explanations? I'm happy to provide additional data and run tests again if needed :)
I'll try to address your comments in sections, but let me know if I forgot to to address something or need clarification.
Same messageId, marked as the second delivery attempt by PubSub. This trace starts at 10:41:40, i.e. in the middle of the previous one. At this point, it is impossible (I think) that the message first delivery has expired, seeing that the default sub deadline is 600s, that the first modAck was sent immediately requesting an extension of 600s, and that (I think) if it had failed the message wouldn't even have been processed because the client would have popped it from the list.
With exactly once delivery enabled, messages shouldn't be redelivered while outstanding. The message wasn't acked/nacked or expired, according to the trace, and yet it was redelivered!
If I'm reading the trace correctly in the previous screenshot, the message was nacked after 1m14s in the first message delivery. This will cause the second delivery as expected. I think you address this too by saying you nacked the message because of loss of ownership (I'm exactly sure what this means but I don't think is relevant).
Separately, the default sub deadline is unused when it comes to our client libraries. The first receipt modack time determines the initial message deadline, after which modacks will continue to push that deadline further out. In addition, when you issue a nack, it sets the message deadline to "expire now" which causes redeliveries. In fact, if you peek into the implementation of Nack, it's not a separate RPC, but rather just a ModifyAckDeadline request.
We're not sure what it means though. Since the messageID is the same, what does it mean for PubSub to do "multiple unique publishes"? If it means multiple redelivers can happen anyway, that sounds like it would defeat the purpose of exactly-once delivery...
You can ignore this section since this isn't relevant to your issue. The docs are basically saying that exactly once guarantees currently only apply on the subscribe side, and if the publish side code is weird (because the client publishes duplicates or Pub/Sub servers end up in a weird state), then you will have duplicate deliveries. But again, I don't think that's what's happening here.
Lastly
In short, we see messages being redelivered when they shouldn't be. Here are our parameters: 10 PubSub client goroutines per pod 200 outstanding messages
Can you decrease ReceiveSettings.NumGoroutine to 1? There isn't much benefit to setting NumGoroutine to be higher than 1 since this configures the number of streams (which can each process 10 MB/s) if you're only processing 200 messages simultaneously.
Apologies for delay in responding to this issue. Also, really appreciate you attaching the trace screenshots since this is always helpful.
Hi @hongalex,
Thanks for looking into it!
If I'm reading the trace correctly in the previous screenshot, the message was nacked after 1m14s in the first message delivery. This will cause the second delivery as expected. I think you address this too by saying you nacked the message because of loss of ownership (I'm exactly sure what this means but I don't think is relevant).
I think I wasn't clear, apologies for that. We really need exactly-once delivery, so we run a locking mechanism of sort using a Redis. The nack seen here is the consequence of the message redelivered, not the cause, or at least that's what we believe. We have logs on our side saying the (first) container processing this message detected it was no longer the registered processor for this message (because another, second container preempted the lock) and so it nacked it.
We have reasons to believe the redeliver happened before the nack, since the nack is basically triggered by the message being redelivered. In fact this mechanism was implemented on our side specifically to workaround this issue. And that's where we are lost...
Thanks for explaining that the default deadline isn't used, and the part about the publishing code, I was a bit confused indeed.
Can you decrease ReceiveSettings.NumGoroutine to 1? There isn't much benefit to setting NumGoroutine to be higher than 1 since this configures the number of streams (which can each process 10 MB/s) if you're only processing 200 messages simultaneously.
We can certainly do that, yes! I'm not sure if it'll help for our redelivery issu though, but we will do!
Ah I see. Yeah, if you have code specifically around nacking only when you detect another processor, then that makes sense.
If we go and look at the modAck trace itself, it asks for 600s seconds extension, as expected.... Since modAck status don't appear in the spans it's hard to say if the modAck failures we see in our metrics are related, but that wouldn't change much as the default deadline hasn't passed yet.
Yeah this is a bug that I'll need to fix (propagating modack errors to the modack spans, and same for ack/nacks).
In the meantime, if you have a support contract, it would be very helpful to open a ticket there so we can look at the backend metrics and behaviors to see if the Pub/Sub service actually got your modack, since the redeliveries in this case pretty much guarantees that message is expiring (not being modacked).
Hi @hongalex,
sorry for the delay! Unfortunately, we don't have a support contract. I understand the likely root cause would be that the modAck got lost? We could double check our networking metrics again to see if there is something wrong there, although everything is happening in GCP here so that seems unlikely.
Thanks for your help!
Yeah I'm not entirely sure what might be causing the receipt modacks fail. It's possible that the message is already expired by the time that the message was pulled by the client library. This is possible when the message is stuck in the grpc buffer prior to client library seeing it. This is where reducing NumGoroutines to 1 could help.
It's possible that the message is already expired by the time that the message was pulled by the client library. This is possible when the message is stuck in the grpc buffer prior to client library seeing it
I understand this is different from the "concurrency control" buffer which is visible in traces? (traces show this one is not an issue)
Yeah so there are several buffers that messages can get stuck in from the client library perspective. The entire flow goes something like
Pub/Sub Server -> gRPC stream buffer -> client library reads from stream -> client library waits until space is available for message (concurrency control buffer) -> client library invokes user callback
The gRPC buffer process can't really be traced at this time since we don't have visibility into the gRPC layer and we only trace messages as soon as the client library becomes aware of them.
From the perspective of the Pub/Sub server, the message lease / ack deadline starts as soon as the server is able to write the message to the stream, even if the client library hasn't seen it yet. In practice, the number of messages in the grpc buffer should be small.
Closing due to staleness, but if you are still experiencing this issue, consider opening a support ticket so we can look into this issue more from the backend.